diff options
author | Nathan Epstein <nate2@umbc.edu> | 2024-05-28 00:41:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-28 14:41:14 +1000 |
commit | c41767154eb82aa3fe7568fc816c3402b78eae94 (patch) | |
tree | 41193b80f150d4e2ce1c2ba2ebd3339cad4e4925 /examples/server | |
parent | 74b239b3d5f067470d7ef5e26e2e059720572e32 (diff) |
Markdownish code block fix (#7571)
* markdownish codeblock fix
* updating regexes
Diffstat (limited to 'examples/server')
-rw-r--r-- | examples/server/public/index.html | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/examples/server/public/index.html b/examples/server/public/index.html index 2961999f..095c4a92 100644 --- a/examples/server/public/index.html +++ b/examples/server/public/index.html @@ -877,19 +877,30 @@ // poor mans markdown replacement const Markdownish = (params) => { - const md = params.text - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/(^|\n)#{1,6} ([^\n]*)(?=([^`]*`[^`]*`)*[^`]*$)/g, '$1<h3>$2</h3>') - .replace(/\*\*(.*?)\*\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>') - .replace(/__(.*?)__(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>') - .replace(/\*(.*?)\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>') - .replace(/_(.*?)_(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>') - .replace(/```.*?\n([\s\S]*?)```/g, '<pre><code>$1</code></pre>') - .replace(/`(.*?)`/g, '<code>$1</code>') - .replace(/\n/gim, '<br />'); - return html`<span dangerouslySetInnerHTML=${{ __html: md }} />`; + const chunks = params.text.split('```'); + + for (let i = 0; i < chunks.length; i++) { + if (i % 2 === 0) { // outside code block + chunks[i] = chunks[i] + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/(^|\n)#{1,6} ([^\n]*)(?=([^`]*`[^`]*`)*[^`]*$)/g, '$1<h3>$2</h3>') + .replace(/\*\*(.*?)\*\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>') + .replace(/__(.*?)__(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>') + .replace(/\*(.*?)\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>') + .replace(/_(.*?)_(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>') + .replace(/```.*?\n([\s\S]*?)```/g, '<pre><code>$1</code></pre>') + .replace(/`(.*?)`/g, '<code>$1</code>') + .replace(/\n/gim, '<br />'); + } else { // inside code block + chunks[i] = `<pre><code>${chunks[i]}</code></pre>`; + } + } + + const restoredText = chunks.join(''); + + return html`<span dangerouslySetInnerHTML=${{ __html: restoredText }} />`; }; const ModelGenerationInfo = (params) => { @@ -903,6 +914,7 @@ ` } + // simple popover impl const Popover = (props) => { const isOpen = useSignal(false); @@ -1054,4 +1066,3 @@ </body> </html> - |