Skip to content

Commit

Permalink
fix: process markdown for hint (StaticJsCMS#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman authored Oct 27, 2023
1 parent c5f54e1 commit 8113d9d
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ collections:
- label: Body
name: body
widget: markdown
hint: Main content goes here.
hint: "*Main* __content__ __*goes*__ [here](example.com)."
- name: faq
label: FAQ
folder: _faqs
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"react-dom": "18.2.0",
"react-frame-component": "5.2.6",
"react-is": "18.2.0",
"react-markdown": "8.0.7",
"react-polyglot": "0.7.2",
"react-redux": "8.0.5",
"react-router-dom": "6.10.0",
Expand All @@ -151,6 +152,7 @@
"react-virtualized-auto-sizer": "1.0.15",
"react-waypoint": "10.3.0",
"react-window": "1.8.9",
"remark-gfm": "4.0.0",
"remark-html": "15.0.2",
"remark-mdx": "2.3.0",
"remark-parse": "10.0.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/__mocks__/react-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

import type { FC, PropsWithChildren } from 'react';

const ReactMarkdown: FC<PropsWithChildren> = ({ children }) => {
return React.createElement('div', {}, [children]);
};

export default ReactMarkdown;
12 changes: 9 additions & 3 deletions packages/core/src/components/common/field/Hint.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
.CMS_Hint_root {
@apply w-full
flex
text-xs
italic;
text-xs;

&:not(.CMS_Hint_error) {
&.CMS_Hint_disabled {
Expand All @@ -27,6 +25,14 @@
}
}

.CMS_Hint_link {
color: inherit;

&:hover {
@apply underline;
}
}

.CMS_Hint_cursor-pointer {
@apply cursor-pointer;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/components/common/field/Hint.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import gfm from 'remark-gfm';

import useCursor from '@staticcms/core/lib/hooks/useCursor';
import classNames from '@staticcms/core/lib/util/classNames.util';
Expand All @@ -16,6 +18,7 @@ export const classes = generateClassNames('Hint', [
'cursor-text',
'cursor-default',
'error',
'link',
]);

export interface HintProps {
Expand Down Expand Up @@ -51,7 +54,18 @@ const Hint: FC<HintProps> = ({
className,
)}
>
{children}
<ReactMarkdown
remarkPlugins={[gfm]}
allowedElements={['a', 'strong', 'em', 'del']}
unwrapDisallowed={true}
components={{
a: ({ node: _node, ...props }) => (
<a {...props} target="_blank" rel="noopener noreferrer" className={classes.link} />
),
}}
>
{children}
</ReactMarkdown>
</div>
);
};
Expand Down
Loading

0 comments on commit 8113d9d

Please sign in to comment.