mirror of
https://github.com/System-End/site.git
synced 2026-04-19 22:05:11 +00:00
feat: add syntax highlighting with react-syntax-highlighter
This commit is contained in:
parent
7e2b957be4
commit
22f2a8622f
1 changed files with 25 additions and 2 deletions
|
|
@ -4,6 +4,9 @@ import rehypeRaw from 'rehype-raw'
|
|||
import rehypeSanitize from 'rehype-sanitize'
|
||||
import style from './readme-renderer.module.css'
|
||||
|
||||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
|
||||
import { solarizedlight } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
||||
|
||||
const ReadmeRenderer = ({ markdown }) => {
|
||||
return (
|
||||
<ReactMarkdown
|
||||
|
|
@ -11,9 +14,29 @@ const ReadmeRenderer = ({ markdown }) => {
|
|||
remarkPlugins={[remarkGfm]}
|
||||
rehypePlugins={[rehypeRaw, rehypeSanitize]}
|
||||
linkTarget={'_blank'}
|
||||
>
|
||||
components={{
|
||||
code({ node, inline, className, children, ...props }) {
|
||||
const match = /language-(\w+)/.exec(className || '')
|
||||
|
||||
return !inline && match ? (
|
||||
<SyntaxHighlighter
|
||||
style={solarizedlight}
|
||||
PreTag="div"
|
||||
language={match[1]}
|
||||
{...props}
|
||||
>
|
||||
{String(children).replace(/\n$/, '')}
|
||||
</SyntaxHighlighter>
|
||||
) : (
|
||||
<code className={className} {...props}>
|
||||
{children}
|
||||
</code>
|
||||
)
|
||||
}
|
||||
}}
|
||||
>
|
||||
{markdown}
|
||||
</ReactMarkdown>
|
||||
)
|
||||
}
|
||||
export default ReadmeRenderer
|
||||
export default ReadmeRenderer
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue