@@ -337,19 +337,72 @@ function generatePreviewHTML(doc: any): string {
337337}
338338
339339function convertMarkdownToHTML ( text : string ) : string {
340- let html = text ;
341-
342- html = html . replace ( / ^ # # # ( .+ ) $ / gm, '<h3 class="text-xl font-bold mt-4 mb-2">$1</h3>' ) ;
343- html = html . replace ( / ^ # # ( .+ ) $ / gm, '<h2 class="text-2xl font-bold mt-6 mb-3">$1</h2>' ) ;
344- html = html . replace ( / ^ # ( .+ ) $ / gm, '<h1 class="text-3xl font-bold mt-8 mb-4">$1</h1>' ) ;
345-
340+ const lines = text . split ( / \r ? \n / ) ;
341+ let html = '' ;
342+ let paragraph : string [ ] = [ ] ;
343+ let listItems : string [ ] = [ ] ;
344+
345+ const flushParagraph = ( ) => {
346+ if ( paragraph . length > 0 ) {
347+ html += `<p class="my-2">${ renderInlineMarkdown ( paragraph . join ( ' ' ) ) } </p>` ;
348+ paragraph = [ ] ;
349+ }
350+ } ;
351+
352+ const flushList = ( ) => {
353+ if ( listItems . length > 0 ) {
354+ html += `<ul class="list-disc pl-6 my-2">${ listItems
355+ . map ( ( item ) => `<li>${ renderInlineMarkdown ( item ) } </li>` )
356+ . join ( '' ) } </ul>`;
357+ listItems = [ ] ;
358+ }
359+ } ;
360+
361+ for ( const rawLine of lines ) {
362+ const line = rawLine . trim ( ) ;
363+ if ( ! line ) {
364+ flushParagraph ( ) ;
365+ flushList ( ) ;
366+ continue ;
367+ }
368+
369+ const headingMatch = / ^ ( # { 1 , 3 } ) \s + ( .+ ) $ / . exec ( line ) ;
370+ if ( headingMatch ) {
371+ flushParagraph ( ) ;
372+ flushList ( ) ;
373+ const level = headingMatch [ 1 ] . length ;
374+ const size =
375+ level === 1 ? 'text-3xl' : level === 2 ? 'text-2xl' : 'text-xl' ;
376+ html += `<h${ level } class="${ size } font-bold mt-6 mb-3">${ renderInlineMarkdown (
377+ headingMatch [ 2 ]
378+ ) } </h${ level } >`;
379+ continue ;
380+ }
381+
382+ const listMatch = / ^ [ - * ] \s + ( .+ ) $ / . exec ( line ) ;
383+ if ( listMatch ) {
384+ flushParagraph ( ) ;
385+ listItems . push ( listMatch [ 1 ] ) ;
386+ continue ;
387+ }
388+
389+ if ( listItems . length > 0 ) {
390+ flushList ( ) ;
391+ }
392+ paragraph . push ( line ) ;
393+ }
394+
395+ flushParagraph ( ) ;
396+ flushList ( ) ;
397+
398+ return html ;
399+ }
400+
401+ function renderInlineMarkdown ( text : string ) : string {
402+ let html = escapeHTML ( text ) ;
346403 html = html . replace ( / \* \* ( .+ ?) \* \* / g, '<strong>$1</strong>' ) ;
347404 html = html . replace ( / \* ( .+ ?) \* / g, '<em>$1</em>' ) ;
348405 html = html . replace ( / ` ( .+ ?) ` / g, '<code class="bg-gray-800 px-1 rounded">$1</code>' ) ;
349-
350- html = html . replace ( / ^ - ( .+ ) $ / gm, '<li>$1</li>' ) ;
351- html = html . replace ( / ( < l i > [ \s \S ] * < \/ l i > ) / , '<ul class="list-disc pl-6 my-2">$1</ul>' ) ;
352-
353406 return html ;
354407}
355408
0 commit comments