2022-03-23 19:28:39 -04:00
|
|
|
import { marked, Lexer } from 'marked'
|
2022-03-22 23:27:39 -04:00
|
|
|
import Highlight, { defaultProps, Language, } from 'prism-react-renderer'
|
2022-03-22 20:37:21 -04:00
|
|
|
import { renderToStaticMarkup } from 'react-dom/server'
|
2022-03-22 23:27:39 -04:00
|
|
|
|
2022-03-23 19:42:56 -04:00
|
|
|
// // image sizes. DDoS Safe?
|
|
|
|
// const imageSizeLink = /^!?\[((?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f()\\]*\)|[^\s\x00-\x1f()\\])*?(?:\s+=(?:[\w%]+)?x(?:[\w%]+)?)?)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
|
|
|
|
// //@ts-ignore
|
|
|
|
// Lexer.rules.inline.normal.link = imageSizeLink;
|
|
|
|
// //@ts-ignore
|
|
|
|
// Lexer.rules.inline.gfm.link = imageSizeLink;
|
|
|
|
// //@ts-ignore
|
|
|
|
// Lexer.rules.inline.breaks.link = imageSizeLink;
|
2022-03-23 19:28:39 -04:00
|
|
|
|
2022-03-22 20:37:21 -04:00
|
|
|
//@ts-ignore
|
|
|
|
delete defaultProps.theme
|
|
|
|
// import linkStyles from '../components/link/link.module.css'
|
|
|
|
|
|
|
|
const renderer = new marked.Renderer()
|
|
|
|
|
2022-03-23 01:18:07 -04:00
|
|
|
renderer.heading = (text, level, _, slugger) => {
|
|
|
|
const id = slugger.slug(text)
|
|
|
|
const Component = `h${level}`
|
|
|
|
|
|
|
|
return renderToStaticMarkup(
|
|
|
|
//@ts-ignore
|
|
|
|
<Component>
|
2022-03-23 02:16:34 -04:00
|
|
|
<a href={`#${id}`} id={id} style={{ color: "inherit" }} dangerouslySetInnerHTML={{ __html: (text) }} >
|
2022-03-23 01:18:07 -04:00
|
|
|
</a>
|
|
|
|
</Component>
|
|
|
|
)
|
|
|
|
}
|
2022-03-23 01:05:26 -04:00
|
|
|
|
|
|
|
// renderer.link = (href, _, text) => {
|
|
|
|
// const isHrefLocal = href?.startsWith('/') || href?.startsWith('#')
|
|
|
|
// if (isHrefLocal) {
|
|
|
|
// return renderToStaticMarkup(
|
|
|
|
// <a href={href || ''}>
|
|
|
|
// {text}
|
|
|
|
// </a>
|
|
|
|
// )
|
|
|
|
// }
|
2022-03-23 01:18:07 -04:00
|
|
|
|
|
|
|
// // dirty hack
|
|
|
|
// // if text contains elements, render as html
|
2022-03-23 02:16:34 -04:00
|
|
|
// return <a href={href || ""} target="_blank" rel="noopener noreferrer" dangerouslySetInnerHTML={{ __html: convertHtmlEntities(text) }} ></a>
|
2022-03-23 01:05:26 -04:00
|
|
|
// }
|
2022-03-22 20:37:21 -04:00
|
|
|
|
2022-03-23 01:18:07 -04:00
|
|
|
|
2022-03-22 20:37:21 -04:00
|
|
|
renderer.image = function (href, _, text) {
|
|
|
|
return `<Image loading="lazy" src="${href}" alt="${text}" layout="fill" />`
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer.checkbox = () => ''
|
|
|
|
renderer.listitem = (text, task, checked) => {
|
|
|
|
if (task) {
|
|
|
|
return `<li class="reset"><span class="check">​<input type="checkbox" disabled ${checked ? 'checked' : ''
|
|
|
|
} /></span><span>${text}</span></li>`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<li>${text}</li>`
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:21:46 -04:00
|
|
|
// renderer.code = (code: string, language: string) => {
|
|
|
|
// return renderToStaticMarkup(
|
|
|
|
// <pre>
|
|
|
|
// {/* {title && <code>{title} </code>} */}
|
|
|
|
// {/* {language && title && <code style={{}}> {language} </code>} */}
|
|
|
|
// <Code
|
|
|
|
// language={language}
|
|
|
|
// // title={title}
|
|
|
|
// code={code}
|
|
|
|
// // highlight={highlight}
|
|
|
|
// />
|
|
|
|
// </pre>
|
|
|
|
// )
|
|
|
|
// }
|
2022-03-22 20:37:21 -04:00
|
|
|
|
|
|
|
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 (
|
|
|
|
<>
|
2022-03-23 21:21:46 -04:00
|
|
|
<code {...props} dangerouslySetInnerHTML={{ __html: code }} />
|
2022-03-22 20:37:21 -04:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<>
|
|
|
|
<Highlight {...defaultProps} code={code.trim()} language={language as Language} >
|
|
|
|
{({ className, style, tokens, getLineProps, getTokenProps }) => (
|
|
|
|
<code className={className} style={{ ...style }}>
|
|
|
|
{
|
|
|
|
tokens.map((line, i) => (
|
|
|
|
<div
|
|
|
|
key={i}
|
|
|
|
{...getLineProps({ line, key: i })}
|
|
|
|
style={
|
2022-03-23 00:18:26 -04:00
|
|
|
//@ts-ignore
|
2022-03-22 20:37:21 -04:00
|
|
|
highlightedLines.includes((i + 1).toString())
|
|
|
|
? {
|
|
|
|
background: 'var(--highlight)',
|
|
|
|
margin: '0 -1rem',
|
|
|
|
padding: '0 1rem',
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
line.map((token, key) => (
|
|
|
|
<span key={key} {...getTokenProps({ token, key })} />
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</code>
|
|
|
|
)}
|
|
|
|
</Highlight>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|