Skip to content

Commit

Permalink
fix: Fix code language causing breaks (#406).
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 13, 2022
1 parent ae6c9ff commit 71a4312
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions core/src/components/TextArea/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import rehypePrism from 'rehype-prism-plus';
import { IProps } from '../../Editor';
import { EditorContext } from '../../Context';

function html2Escape(sHtml: string) {
return sHtml
.replace(/```(tsx?|jsx?|html|xml)(.*)\s+([\s\S]*?)(\s.+)?```/g, (str: string) => {
return str.replace(
/[<&"]/g,
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' } as Record<string, string>)[c]),
);
})
.replace(
/[<&"]/g,
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' } as Record<string, string>)[c]),
);
}

export interface MarkdownProps extends IProps, React.HTMLAttributes<HTMLPreElement> {}

export default function Markdown(props: MarkdownProps) {
Expand All @@ -16,34 +30,28 @@ export default function Markdown(props: MarkdownProps) {
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function html2Escape(sHtml: string) {
return sHtml
.replace(/```(tsx?|jsx?|html|xml)(.*)\s+([\s\S]*?)(\s.+)?```/g, (str: string) => {
return str.replace(
/[<&"]/g,
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' } as Record<string, string>)[c]),
);
})
.replace(
/[<&"]/g,
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' } as Record<string, string>)[c]),
);
}

return useMemo(() => {
if (!markdown) {
return <pre children={markdown || ''} ref={preRef} className={`${prefixCls}-text-pre wmde-markdown-color`} />;
return <pre ref={preRef} className={`${prefixCls}-text-pre wmde-markdown-color`} />;
}
let mdStr = `<pre class="language-markdown ${prefixCls}-text-pre wmde-markdown-color"><code class="language-markdown">${html2Escape(
markdown,
)}\n</code></pre>`;

if (highlightEnable) {
mdStr = rehype().data('settings', { fragment: true }).use(rehypePrism, { ignoreMissing: true }).processSync(mdStr)
.value as string;
try {
mdStr = rehype()
.data('settings', { fragment: true })
.use(rehypePrism, { ignoreMissing: false })
.processSync(mdStr)
.toString();
} catch (error) {}
}

return React.createElement('div', {
className: 'wmde-markdown-color',
dangerouslySetInnerHTML: { __html: mdStr },
dangerouslySetInnerHTML: { __html: mdStr || '' },
});
}, [markdown, preRef, prefixCls]);
}

1 comment on commit 71a4312

@jaywcjlove
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.