1+ import { marked } from "marked"
12import type { PossibleField } from "./fields"
23
34/**
@@ -20,40 +21,14 @@ export function assert(condition: boolean, message?: string): asserts condition
2021 * Converts markdown-style rich text to HTML
2122 */
2223export function richTextToHTML ( cellValue : string ) : string {
23- let html = cellValue
24- . replace ( / ^ # # # # # # ( .* ) $ / gim, "<h6>$1</h6>" ) // H6
25- . replace ( / ^ # # # # # ( .* ) $ / gim, "<h5>$1</h5>" ) // H5
26- . replace ( / ^ # # # # ( .* ) $ / gim, "<h4>$1</h4>" ) // H4
27- . replace ( / ^ # # # ( .* ) $ / gim, "<h3>$1</h3>" ) // H3
28- . replace ( / ^ # # ( .* ) $ / gim, "<h2>$1</h2>" ) // H2
29- . replace ( / ^ # ( .* ) $ / gim, "<h1>$1</h1>" ) // H1
30- . replace ( / ^ > ( .* ) $ / gim, "<blockquote>$1</blockquote>" ) // Blockquote
31- . replace ( / \* \* ( .* ?) \* \* / gim, "<strong>$1</strong>" ) // Bold
32- . replace ( / \* ( .* ?) \* / gim, "<em>$1</em>" ) // Italic
33- . replace ( / ~ ~ ( .* ?) ~ ~ / gim, "<del>$1</del>" ) // Strikethrough
34- . replace ( / ` ( [ ^ ` ] + ) ` / gim, "<code>$1</code>" ) // Inline code
35- . replace ( / ` ` ` ( [ \s \S ] * ?) ` ` ` / gim, "<pre><code>$1</code></pre>" ) // Code block
36- . replace ( / \[ ( .* ?) \] \( ( .* ?) \) / gim, '<a href="$2">$1</a>' ) // Links
37-
38- // Handle unordered and ordered lists separately
39- html = html . replace ( / ^ \s * [ - * ] \s + ( .* ) $ / gim, "<ul><li>$1</li></ul>" )
40- html = html . replace ( / ^ \s * \d + \. \s + ( .* ) $ / gim, "<ol><li>$1</li></ol>" )
41-
42- // Combine consecutive <ul> or <ol> items
43- html = html . replace ( / < \/ u l > \s * < u l > / gim, "" ) . replace ( / < \/ o l > \s * < o l > / gim, "" )
44-
45- // Ensure paragraphs are correctly wrapped
46- html = html
47- . split ( "\n\n" ) // Assume two newlines is a new paragraph
48- . map ( paragraph => {
49- if ( ! / ^ < .* > .* < \/ .* > $ / . test ( paragraph ) ) {
50- return `<p>${ paragraph } </p>`
51- }
52- return paragraph
53- } )
54- . join ( "" )
55-
56- return html
24+ return marked . parse ( cellValue , {
25+ // This is needed for proper typing.
26+ async : false ,
27+ // This adds support for tables and code blocks with language tags (```javascript ... ```).
28+ gfm : true ,
29+ // This ensures single-line line breaks are preserved.
30+ breaks : true ,
31+ } )
5732}
5833
5934// Allowed file types for attachments
0 commit comments