1+ module . exports = {
2+ marked : {
3+ parse : jest . fn ( ( text ) => {
4+ if ( ! text ) return '' ;
5+
6+ // Handle lists first
7+ let html = text ;
8+
9+ // Convert unordered lists
10+ const unorderedListRegex = / ^ - ( .+ ) $ / gm;
11+ const unorderedMatches = [ ...text . matchAll ( unorderedListRegex ) ] ;
12+ if ( unorderedMatches . length > 0 ) {
13+ let listItems = '' ;
14+ unorderedMatches . forEach ( match => {
15+ listItems += `<li>${ match [ 1 ] } </li>` ;
16+ } ) ;
17+ html = html . replace ( unorderedListRegex , '' ) ;
18+ html += `<ul>${ listItems } </ul>` ;
19+ }
20+
21+ // Convert ordered lists
22+ const orderedListRegex = / ^ \d + \. ( .+ ) $ / gm;
23+ const orderedMatches = [ ...text . matchAll ( orderedListRegex ) ] ;
24+ if ( orderedMatches . length > 0 ) {
25+ let listItems = '' ;
26+ orderedMatches . forEach ( match => {
27+ listItems += `<li>${ match [ 1 ] } </li>` ;
28+ } ) ;
29+ html = html . replace ( orderedListRegex , '' ) ;
30+ html += `<ol>${ listItems } </ol>` ;
31+ }
32+
33+ // Other conversions
34+ html = html
35+ . replace ( / ^ # # # ( .* $ ) / gim, '<h3>$1</h3>' )
36+ . replace ( / ^ # # ( .* $ ) / gim, '<h2>$1</h2>' )
37+ . replace ( / ^ # ( .* $ ) / gim, '<h1>$1</h1>' )
38+ . replace ( / \* \* ( [ ^ * ] + ) \* \* / g, '<strong>$1</strong>' )
39+ . replace ( / \* ( [ ^ * ] + ) \* / g, '<em>$1</em>' )
40+ . replace ( / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) / g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>' )
41+ . replace ( / ` ( [ ^ ` ] + ) ` / g, '<code>$1</code>' )
42+ . replace ( / ` ` ` j a v a s c r i p t \n ( [ ^ ` ] + ) \n ` ` ` / g, '<pre><code>$1</code></pre>' )
43+ . replace ( / ^ > ( .+ ) $ / gm, '<blockquote>$1</blockquote>' )
44+ . replace ( / ^ - - - $ / gm, '<hr>' ) ;
45+
46+ // Handle tables
47+ if ( text . includes ( '|' ) ) {
48+ const lines = text . split ( '\n' ) ;
49+ let inTable = false ;
50+ let tableHtml = '' ;
51+
52+ lines . forEach ( ( line , index ) => {
53+ if ( line . includes ( '|' ) && ! line . includes ( '---' ) ) {
54+ const cells = line . split ( '|' ) . map ( cell => cell . trim ( ) ) . filter ( cell => cell ) ;
55+ if ( ! inTable ) {
56+ tableHtml = '<table><thead><tr>' ;
57+ cells . forEach ( cell => {
58+ tableHtml += `<th>${ cell } </th>` ;
59+ } ) ;
60+ tableHtml += '</tr></thead><tbody>' ;
61+ inTable = true ;
62+ } else if ( index > 1 ) {
63+ tableHtml += '<tr>' ;
64+ cells . forEach ( cell => {
65+ tableHtml += `<td>${ cell } </td>` ;
66+ } ) ;
67+ tableHtml += '</tr>' ;
68+ }
69+ }
70+ } ) ;
71+
72+ if ( inTable ) {
73+ tableHtml += '</tbody></table>' ;
74+ return tableHtml ;
75+ }
76+ }
77+
78+ // Wrap non-HTML content in paragraphs
79+ const lines = html . split ( '\n' ) . filter ( line => line . trim ( ) ) ;
80+ html = lines . map ( line => {
81+ if ( ! line . match ( / ^ < [ ^ > ] + > / ) ) {
82+ return `<p>${ line } </p>` ;
83+ }
84+ return line ;
85+ } ) . join ( '' ) ;
86+
87+ return html ;
88+ } ) ,
89+ setOptions : jest . fn ( ) ,
90+ } ,
91+ } ;
0 commit comments