import { marked } from 'marked' import Highlight, { defaultProps, Language, } from 'prism-react-renderer' import { renderToStaticMarkup } from 'react-dom/server' //@ts-ignore delete defaultProps.theme // import linkStyles from '../components/link/link.module.css' const renderer = new marked.Renderer() const convertHtmlEntities = (str: string) => { const quot = '"' const apos = ''' const amp = '&' const nbsp = ' ' const lt = '<' const gt = '>' const code = '' const endCode = '' const combinedRegex = new RegExp(`${code}|${endCode}|${quot}|${apos}|${amp}|${nbsp}|${lt}|${gt}`, 'g') return str.replace(combinedRegex, (match) => { switch (match) { case quot: return '"' case apos: return "'" case amp: return '&' case nbsp: return ' ' case lt: return '<' case gt: return '>' case code: case endCode: return '`' default: return match } }) } renderer.heading = (text, level, _, slugger) => { const id = slugger.slug(text) const Component = `h${level}` return renderToStaticMarkup( //@ts-ignore {convertHtmlEntities(text)} ) } // renderer.link = (href, _, text) => { // const isHrefLocal = href?.startsWith('/') || href?.startsWith('#') // if (isHrefLocal) { // return renderToStaticMarkup( // // {text} // // ) // } // // dirty hack // // if text contains elements, render as html // return `${convertHtmlEntities(text)}` // } renderer.image = function (href, _, text) { return `${text}` } renderer.checkbox = () => '' renderer.listitem = (text, task, checked) => { if (task) { return `
  • ${text}
  • ` } return `
  • ${text}
  • ` } renderer.code = (code: string, language: string) => { return renderToStaticMarkup(
                {/* {title && {title} } */}
                {/* {language && title &&  {language} } */}
                
            
    ) } marked.setOptions({ gfm: true, breaks: true, headerIds: true, renderer, }) const markdown = (markdown: string) => marked(markdown) export default markdown const Code = ({ code, language, highlight, title, ...props }: { code: string, language: string, highlight?: string, title?: string, }) => { if (!language) return ( <> ) const highlightedLines = highlight //@ts-ignore ? highlight.split(',').reduce((lines, h) => { if (h.includes('-')) { // Expand ranges like 3-5 into [3,4,5] const [start, end] = h.split('-').map(Number) const x = Array(end - start + 1) .fill(undefined) .map((_, i) => i + start) return [...lines, ...x] } return [...lines, Number(h)] }, []) : '' // https://mdxjs.com/guides/syntax-harkedighlighting#all-together return ( <> {({ className, style, tokens, getLineProps, getTokenProps }) => ( { tokens.map((line, i) => (
    { line.map((token, key) => ( )) }
    ))}
    )}
    ) }