From f92d854336e63514f9a5bd798bf5fcfcb6ad7835 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Tue, 22 Mar 2022 22:18:07 -0700 Subject: [PATCH] client/server: linkify headers and transform html entities in markdown --- client/lib/render-markdown.tsx | 67 +++++++++++++++++++++++------- server/src/lib/render-markdown.tsx | 67 +++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 28 deletions(-) diff --git a/client/lib/render-markdown.tsx b/client/lib/render-markdown.tsx index e3c30843..7919c5c2 100644 --- a/client/lib/render-markdown.tsx +++ b/client/lib/render-markdown.tsx @@ -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 = '' + const endCode = '' + const combinedRegex = new RegExp(`${code}|${endCode}|${quot}|${apos}|${amp}|${nbsp}|${lt}|${gt}`, 'g') -// return renderToStaticMarkup( -// //@ts-ignore -// -// -// {text} -// -// -// ) -// } + 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)} + + + ) +} -// 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() // // ) // } -// return `${text}` + +// // dirty hack +// // if text contains elements, render as html +// return `${convertHtmlEntities(text)}` // } + renderer.image = function (href, _, text) { return `${text}` } diff --git a/server/src/lib/render-markdown.tsx b/server/src/lib/render-markdown.tsx index e3c30843..7919c5c2 100644 --- a/server/src/lib/render-markdown.tsx +++ b/server/src/lib/render-markdown.tsx @@ -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 = '' + const endCode = '' + const combinedRegex = new RegExp(`${code}|${endCode}|${quot}|${apos}|${amp}|${nbsp}|${lt}|${gt}`, 'g') -// return renderToStaticMarkup( -// //@ts-ignore -// -// -// {text} -// -// -// ) -// } + 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)} + + + ) +} -// 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() // // ) // } -// return `${text}` + +// // dirty hack +// // if text contains elements, render as html +// return `${convertHtmlEntities(text)}` // } + renderer.image = function (href, _, text) { return `${text}` }