import styles from './text.module.css' import ReactMarkdown from 'react-markdown' import gfm from 'remark-gfm' import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' /* Use `…/dist/cjs/…` if you’re not in ESM! */ import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism' import mention from '../lib/remark-mention' import sub from '../lib/remark-sub' import remarkDirective from 'remark-directive' import { visit } from 'unist-util-visit' import reactStringReplace from 'react-string-replace' function myRemarkPlugin () { return (tree) => { visit(tree, (node) => { if ( node.type === 'textDirective' || node.type === 'leafDirective' ) { if (node.name !== 'high') return const data = node.data || (node.data = {}) data.hName = 'mark' data.hProperties = {} } }) } } export default function Text ({ nofollow, children }) { // all the reactStringReplace calls are to facilitate search highlighting return (
, code ({ node, inline, className, children, ...props }) { const match = /language-(\w+)/.exec(className || '') return !inline ? ( {reactStringReplace(String(children).replace(/\n$/, ''), /:high\[([^\]]+)\]/g, (match, i) => { return match }).join('')} ) : ( {reactStringReplace(String(children), /:high\[([^\]]+)\]/g, (match, i) => { return {match} })} ) }, a: ({ node, href, children, ...props }) => { children = children?.map(e => typeof e === 'string' ? reactStringReplace(e, /:high\[([^\]]+)\]/g, (match, i) => { return {match} }) : e) return ( /* eslint-disable-next-line */ { return match }).join('')} {...props} > {children} ) } }} remarkPlugins={[gfm, mention, sub, remarkDirective, myRemarkPlugin]} > {children} ) }