client/server: render nested elements in headings
This commit is contained in:
parent
f92d854336
commit
a139acc747
2 changed files with 4 additions and 74 deletions
|
@ -8,40 +8,6 @@ delete defaultProps.theme
|
||||||
|
|
||||||
const renderer = new marked.Renderer()
|
const renderer = new marked.Renderer()
|
||||||
|
|
||||||
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 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) => {
|
renderer.heading = (text, level, _, slugger) => {
|
||||||
const id = slugger.slug(text)
|
const id = slugger.slug(text)
|
||||||
const Component = `h${level}`
|
const Component = `h${level}`
|
||||||
|
@ -51,8 +17,7 @@ renderer.heading = (text, level, _, slugger) => {
|
||||||
return renderToStaticMarkup(
|
return renderToStaticMarkup(
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
<Component>
|
<Component>
|
||||||
<a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
<a href={`#${id}`} id={id} style={{ color: "inherit" }} dangerouslySetInnerHTML={{ __html: (text) }} >
|
||||||
{convertHtmlEntities(text)}
|
|
||||||
</a>
|
</a>
|
||||||
</Component>
|
</Component>
|
||||||
)
|
)
|
||||||
|
@ -70,7 +35,7 @@ renderer.heading = (text, level, _, slugger) => {
|
||||||
|
|
||||||
// // dirty hack
|
// // dirty hack
|
||||||
// // if text contains elements, render as html
|
// // if text contains elements, render as html
|
||||||
// return `<a href='${href}' target="_blank" rel="noopener noreferrer">${convertHtmlEntities(text)}</a>`
|
// return <a href={href || ""} target="_blank" rel="noopener noreferrer" dangerouslySetInnerHTML={{ __html: convertHtmlEntities(text) }} ></a>
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,40 +8,6 @@ delete defaultProps.theme
|
||||||
|
|
||||||
const renderer = new marked.Renderer()
|
const renderer = new marked.Renderer()
|
||||||
|
|
||||||
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 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) => {
|
renderer.heading = (text, level, _, slugger) => {
|
||||||
const id = slugger.slug(text)
|
const id = slugger.slug(text)
|
||||||
const Component = `h${level}`
|
const Component = `h${level}`
|
||||||
|
@ -51,8 +17,7 @@ renderer.heading = (text, level, _, slugger) => {
|
||||||
return renderToStaticMarkup(
|
return renderToStaticMarkup(
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
<Component>
|
<Component>
|
||||||
<a href={`#${id}`} id={id} style={{ color: "inherit" }} >
|
<a href={`#${id}`} id={id} style={{ color: "inherit" }} dangerouslySetInnerHTML={{ __html: (text) }} >
|
||||||
{convertHtmlEntities(text)}
|
|
||||||
</a>
|
</a>
|
||||||
</Component>
|
</Component>
|
||||||
)
|
)
|
||||||
|
@ -70,7 +35,7 @@ renderer.heading = (text, level, _, slugger) => {
|
||||||
|
|
||||||
// // dirty hack
|
// // dirty hack
|
||||||
// // if text contains elements, render as html
|
// // if text contains elements, render as html
|
||||||
// return `<a href='${href}' target="_blank" rel="noopener noreferrer">${convertHtmlEntities(text)}</a>`
|
// return <a href={href || ""} target="_blank" rel="noopener noreferrer" dangerouslySetInnerHTML={{ __html: convertHtmlEntities(text) }} ></a>
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue