66 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-04-14 18:56:29 -05:00
import styles from './text.module.css'
2021-06-27 17:28:30 -05:00
import ReactMarkdown from 'react-markdown'
import gfm from 'remark-gfm'
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
/* Use `…/dist/cjs/…` if youre not in ESM! */
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
2021-07-15 14:56:23 -05:00
import mention from '../lib/remark-mention'
2022-02-03 16:01:42 -06:00
import remarkDirective from 'remark-directive'
import { visit } from 'unist-util-visit'
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 = {}
}
})
}
}
2021-04-14 18:56:29 -05:00
export default function Text ({ nofollow, children }) {
2021-04-14 18:56:29 -05:00
return (
2021-06-27 17:28:30 -05:00
<div className={styles.text}>
<ReactMarkdown
components={{
h1: 'h6',
h2: 'h6',
h3: 'h6',
h4: 'h6',
h5: 'h6',
h6: 'h6',
table: ({ node, ...props }) =>
<div className='table-responsive'>
<table className='table table-bordered table-sm' {...props} />
</div>,
code ({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '')
return !inline
? (
<SyntaxHighlighter showLineNumbers style={atomDark} language={match && match[1]} PreTag='div' {...props}>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>
)
: (
<code className={className} style={atomDark} {...props}>
{children}
</code>
)
},
a: ({ node, ...props }) => <a target='_blank' rel={nofollow ? 'nofollow' : null} {...props} />
2021-06-27 17:28:30 -05:00
}}
2022-02-03 16:01:42 -06:00
remarkPlugins={[gfm, mention, remarkDirective, myRemarkPlugin]}
2021-06-27 17:28:30 -05:00
>
{children}
</ReactMarkdown>
</div>
2021-04-14 18:56:29 -05:00
)
}