react-code-dataset / react-spring /docs /scripts /mdx /rehype-highlight-word.js
Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame
509 Bytes
/**
* Credit to https://ped.ro/writing/code-blocks-but-better
*/
import { toHtml } from 'hast-util-to-html'
import { unified } from 'unified'
import parse from 'rehype-parse'
const CALLOUT = /__(.*?)__/g
export default function (code) {
const html = toHtml(code)
const result = html.replace(
CALLOUT,
(_, text) => `<span class="highlight-word">${text}</span>`
)
const hast = unified()
.use(parse, { emitParseErrors: true, fragment: true })
.parse(result)
return hast.children
}