client/server: linkify headers and transform html entities in markdown
This commit is contained in:
parent
c0c18e5b61
commit
f92d854336
2 changed files with 106 additions and 28 deletions
|
@ -8,21 +8,56 @@ delete defaultProps.theme
|
|||
|
||||
const renderer = new marked.Renderer()
|
||||
|
||||
// renderer.heading = (text, level, _, slugger) => {
|
||||
// const id = slugger.slug(text)
|
||||
// const Component = `h${level}`
|
||||
const convertHtmlEntities = (str: string) => {
|
||||
const quot = '"'
|
||||
const apos = '''
|
||||
const amp = '&'
|
||||
const nbsp = ' '
|
||||
const lt = '<'
|
||||
const gt = '>'
|
||||
const code = '<code>'
|
||||
const endCode = '</code>'
|
||||
const combinedRegex = new RegExp(`${code}|${endCode}|${quot}|${apos}|${amp}|${nbsp}|${lt}|${gt}`, 'g')
|
||||
|
||||
// return renderToStaticMarkup(
|
||||
// //@ts-ignore
|
||||
// <Component>
|
||||
// <a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
||||
// {text}
|
||||
// </a>
|
||||
// </Component>
|
||||
// )
|
||||
// }
|
||||
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
|
||||
<Component>
|
||||
<a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
||||
{convertHtmlEntities(text)}
|
||||
</a>
|
||||
</Component>
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: support elements inside link
|
||||
// renderer.link = (href, _, text) => {
|
||||
// const isHrefLocal = href?.startsWith('/') || href?.startsWith('#')
|
||||
// if (isHrefLocal) {
|
||||
|
@ -32,9 +67,13 @@ const renderer = new marked.Renderer()
|
|||
// </a>
|
||||
// )
|
||||
// }
|
||||
// return `<a href="${href}" target="_blank" rel="noopener noreferrer">${text}</a>`
|
||||
|
||||
// // dirty hack
|
||||
// // if text contains elements, render as html
|
||||
// return `<a href='${href}' target="_blank" rel="noopener noreferrer">${convertHtmlEntities(text)}</a>`
|
||||
// }
|
||||
|
||||
|
||||
renderer.image = function (href, _, text) {
|
||||
return `<Image loading="lazy" src="${href}" alt="${text}" layout="fill" />`
|
||||
}
|
||||
|
|
|
@ -8,21 +8,56 @@ delete defaultProps.theme
|
|||
|
||||
const renderer = new marked.Renderer()
|
||||
|
||||
// renderer.heading = (text, level, _, slugger) => {
|
||||
// const id = slugger.slug(text)
|
||||
// const Component = `h${level}`
|
||||
const convertHtmlEntities = (str: string) => {
|
||||
const quot = '"'
|
||||
const apos = '''
|
||||
const amp = '&'
|
||||
const nbsp = ' '
|
||||
const lt = '<'
|
||||
const gt = '>'
|
||||
const code = '<code>'
|
||||
const endCode = '</code>'
|
||||
const combinedRegex = new RegExp(`${code}|${endCode}|${quot}|${apos}|${amp}|${nbsp}|${lt}|${gt}`, 'g')
|
||||
|
||||
// return renderToStaticMarkup(
|
||||
// //@ts-ignore
|
||||
// <Component>
|
||||
// <a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
||||
// {text}
|
||||
// </a>
|
||||
// </Component>
|
||||
// )
|
||||
// }
|
||||
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
|
||||
<Component>
|
||||
<a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
||||
{convertHtmlEntities(text)}
|
||||
</a>
|
||||
</Component>
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: support elements inside link
|
||||
// renderer.link = (href, _, text) => {
|
||||
// const isHrefLocal = href?.startsWith('/') || href?.startsWith('#')
|
||||
// if (isHrefLocal) {
|
||||
|
@ -32,9 +67,13 @@ const renderer = new marked.Renderer()
|
|||
// </a>
|
||||
// )
|
||||
// }
|
||||
// return `<a href="${href}" target="_blank" rel="noopener noreferrer">${text}</a>`
|
||||
|
||||
// // dirty hack
|
||||
// // if text contains elements, render as html
|
||||
// return `<a href='${href}' target="_blank" rel="noopener noreferrer">${convertHtmlEntities(text)}</a>`
|
||||
// }
|
||||
|
||||
|
||||
renderer.image = function (href, _, text) {
|
||||
return `<Image loading="lazy" src="${href}" alt="${text}" layout="fill" />`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue