(markdown: string)
| 65 | * // Returns: '<p><strong>bold</strong> and <em>italic</em></p>' |
| 66 | */ |
| 67 | export const markdownToHtmlBasic = (markdown: string): string => { |
| 68 | if (!markdown) { |
| 69 | return ''; |
| 70 | } |
| 71 | |
| 72 | const lines = markdown.split(/\r?\n/); |
| 73 | const htmlParts: string[] = []; |
| 74 | let listType: 'ul' | 'ol' | null = null; |
| 75 | let listItems: string[] = []; |
| 76 | let isInCodeBlock = false; |
| 77 | let codeBlockLines: string[] = []; |
| 78 | let hasRenderedBlock = false; |
| 79 | let pendingBlankLines = 0; |
| 80 | |
| 81 | const flushList = () => { |
| 82 | if (!listType || listItems.length === 0) { |
| 83 | listType = null; |
| 84 | listItems = []; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | htmlParts.push(`<${listType}>${listItems.join('')}</${listType}>`); |
| 89 | hasRenderedBlock = true; |
| 90 | listType = null; |
| 91 | listItems = []; |
| 92 | }; |
| 93 | |
| 94 | const flushPendingEmptyParagraphs = () => { |
| 95 | if (!hasRenderedBlock || pendingBlankLines === 0) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | for (let i = 1; i < pendingBlankLines; i += 1) { |
| 100 | htmlParts.push('<p></p>'); |
| 101 | } |
| 102 | |
| 103 | pendingBlankLines = 0; |
| 104 | }; |
| 105 | |
| 106 | lines.forEach((line) => { |
| 107 | const trimmed = line.trim(); |
| 108 | |
| 109 | if (trimmed.startsWith('```')) { |
| 110 | if (isInCodeBlock) { |
| 111 | flushPendingEmptyParagraphs(); |
| 112 | const codeContent = escapeHtml(codeBlockLines.join('\n')); |
| 113 | htmlParts.push(`<pre><code>${codeContent}</code></pre>`); |
| 114 | codeBlockLines = []; |
| 115 | isInCodeBlock = false; |
| 116 | hasRenderedBlock = true; |
| 117 | } else { |
| 118 | flushList(); |
| 119 | flushPendingEmptyParagraphs(); |
| 120 | isInCodeBlock = true; |
| 121 | } |
| 122 | return; |
| 123 | } |
| 124 |
no test coverage detected