| 1 | // Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 6 | // Unused function - could be removed if not needed elsewhere |
| 5 | 7 | function isTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 11 | // Generic parser for text formatting (bold, italic, etc.) |
| 9 | 12 | function parser(markdown, delimiter, tag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 14 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 18 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 19 | function parseBold(markdown) { |
| 16 | 20 | return parser(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 23 | // Parse italic text (_text_ -> <em>text</em>) |
| 24 | function parseItalic(markdown) { |
| 20 | 25 | return parser(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 28 | // Parse formatted text and wrap in paragraph tags if not in a list |
| 29 | function parseFormattedText(markdown, inList) { |
| 30 | // Apply text formatting (bold, italic) |
| 31 | const formattedText = parseItalic(parseBold(markdown)); |
| 32 | |
| 33 | // If we're in a list, don't wrap in paragraph tags |
| 34 | if (inList) { |
| 35 | return formattedText; |
| 27 | 36 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 37 | return wrap(formattedText, 'p'); |
| 29 | 38 | } |
| 30 | 39 | } |
| 31 | 40 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 41 | // Parse header markdown (# Header -> <h1>Header</h1>) |
| 42 | function parseHeader(markdown, currentlyInList) { |
| 43 | // Count consecutive # characters at the beginning |
| 44 | let headerLevel = 0; |
| 34 | 45 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 46 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 47 | headerLevel++; |
| 37 | 48 | } else { |
| 38 | 49 | break; |
| 39 | 50 | } |
| 40 | 51 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 52 | |
| 53 | // Valid headers have 1-6 # characters |
| 54 | if (headerLevel === 0 || headerLevel > 6) { |
| 55 | return [null, currentlyInList]; |
| 43 | 56 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 57 | |
| 58 | // Create the header tag (h1, h2, etc.) |
| 59 | const headerTag = `h${headerLevel}`; |
| 60 | |
| 61 | // Extract header text (skip # characters and the following space) |
| 62 | const headerText = markdown.substring(headerLevel + 1); |
| 63 | const headerHtml = wrap(headerText, headerTag); |
| 64 | |
| 65 | // If we were in a list, close it before the header |
| 66 | if (currentlyInList) { |
| 47 | 67 | return [`</ul>${headerHtml}`, false]; |
| 48 | 68 | } else { |
| 49 | 69 | return [headerHtml, false]; |
| 50 | 70 | } |
| 51 | 71 | } |
| 52 | 72 | |
| 53 | | function parseLineItem(markdown, list) { |
| 73 | // Parse list item markdown (* item -> <li>item</li>) |
| 74 | function parseListItem(markdown, currentlyInList) { |
| 75 | // Check if line starts with * |
| 54 | 76 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 77 | // Extract list item text (skip '* ' prefix) |
| 78 | const itemText = markdown.substring(2); |
| 79 | |
| 80 | // Parse formatted text without paragraph wrapping (since it's in a list) |
| 81 | const formattedItemText = parseFormattedText(itemText, true); |
| 82 | const listItemHtml = wrap(formattedItemText, 'li'); |
| 83 | |
| 84 | // If we're already in a list, just add the list item |
| 85 | // Otherwise, start a new list |
| 86 | if (currentlyInList) { |
| 87 | return [listItemHtml, true]; |
| 58 | 88 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 89 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 90 | } |
| 61 | 91 | } |
| 62 | | return [null, list]; |
| 92 | |
| 93 | return [null, currentlyInList]; |
| 63 | 94 | } |
| 64 | 95 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 96 | // Parse paragraph markdown (plain text -> <p>text</p>) |
| 97 | function parseParagraph(markdown, currentlyInList) { |
| 98 | // Parse formatted text and wrap in paragraph tags |
| 99 | const paragraphHtml = parseFormattedText(markdown, false); |
| 100 | |
| 101 | // If we were in a list, close it before the paragraph |
| 102 | if (currentlyInList) { |
| 103 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 104 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 105 | return [paragraphHtml, false]; |
| 70 | 106 | } |
| 71 | 107 | } |
| 72 | 108 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 109 | // Parse a single line of markdown |
| 110 | function parseLine(markdown, currentlyInList) { |
| 111 | // Handle empty lines - they should not create paragraphs |
| 112 | if (markdown.trim() === '') { |
| 113 | // If we were in a list, close it |
| 114 | if (currentlyInList) { |
| 115 | return ['</ul>', false]; |
| 116 | } |
| 117 | // Otherwise, return empty result |
| 118 | return ['', false]; |
| 119 | } |
| 120 | |
| 121 | // Try parsing as header first |
| 122 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 123 | |
| 124 | // If not a header, try parsing as list item |
| 75 | 125 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 126 | [result, newListState] = parseListItem(markdown, currentlyInList); |
| 77 | 127 | } |
| 128 | |
| 129 | // If not a list item, parse as paragraph |
| 78 | 130 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 131 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 132 | } |
| 133 | |
| 134 | // This should never happen with valid input |
| 81 | 135 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 136 | throw new Error('Invalid markdown line'); |
| 83 | 137 | } |
| 84 | | return [result, inListAfter]; |
| 138 | |
| 139 | return [result, newListState]; |
| 85 | 140 | } |
| 86 | 141 | |
| 87 | 142 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 143 | * Parse markdown text and convert it to HTML |
| 144 | * @param {string} markdown - The markdown text to parse |
| 145 | * @returns {string} - The resulting HTML |
| 90 | 146 | */ |
| 91 | 147 | export function parse(markdown) { |
| 148 | // Split markdown into lines |
| 92 | 149 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 150 | let htmlResult = ''; |
| 151 | let inList = false; |
| 152 | |
| 153 | // Process each line |
| 154 | for (const line of lines) { |
| 155 | const [lineHtml, newListState] = parseLine(line, inList); |
| 156 | htmlResult += lineHtml; |
| 157 | inList = newListState; |
| 99 | 158 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 159 | |
| 160 | // Close any open list at the end |
| 161 | if (inList) { |
| 162 | htmlResult += '</ul>'; |
| 104 | 163 | } |
| 164 | |
| 165 | return htmlResult; |
| 105 | 166 | } |