| 1 | // ===== UTILITY FUNCTIONS ===== |
| 2 | |
| 3 | /** |
| 4 | * Wraps text in an HTML tag |
| 5 | * @param {string} text - The text to wrap |
| 6 | * @param {string} tag - The HTML tag name |
| 7 | * @returns {string} The wrapped text |
| 8 | */ |
| 1 | 9 | function wrap(text, tag) { |
| 2 | 10 | return `<${tag}>${text}</${tag}>`; |
| 3 | 11 | } |
| 4 | 12 | |
| 13 | /** |
| 14 | * Checks if text starts with a specific HTML tag |
| 15 | * @param {string} text - The text to check |
| 16 | * @param {string} tag - The tag to look for |
| 17 | * @returns {boolean} True if text starts with the tag |
| 18 | */ |
| 5 | 19 | function isTag(text, tag) { |
| 6 | 20 | return text.startsWith(`<${tag}>`); |
| 7 | 21 | } |
| 8 | 22 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 23 | // ===== TEXT FORMATTING FUNCTIONS ===== |
| 24 | |
| 25 | /** |
| 26 | * Generic parser for markdown formatting using delimiters |
| 27 | * @param {string} markdown - The markdown text to parse |
| 28 | * @param {string} delimiter - The delimiter to search for |
| 29 | * @param {string} tag - The HTML tag to replace with |
| 30 | * @returns {string} The formatted HTML |
| 31 | */ |
| 32 | function parseWithDelimiter(markdown, delimiter, tag) { |
| 10 | 33 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 34 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 35 | return markdown.replace(pattern, replacement); |
| 13 | 36 | } |
| 14 | 37 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 38 | /** |
| 39 | * Parses bold text using __ delimiters |
| 40 | * @param {string} markdown - The markdown text |
| 41 | * @returns {string} HTML with <strong> tags |
| 42 | */ |
| 43 | function parseBold(markdown) { |
| 44 | return parseWithDelimiter(markdown, '__', 'strong'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Parses italic text using _ delimiters |
| 49 | * @param {string} markdown - The markdown text |
| 50 | * @returns {string} HTML with <em> tags |
| 51 | */ |
| 52 | function parseItalic(markdown) { |
| 53 | return parseWithDelimiter(markdown, '_', 'em'); |
| 17 | 54 | } |
| 18 | 55 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 56 | /** |
| 57 | * Applies all text formatting (bold and italic) to markdown text |
| 58 | * @param {string} markdown - The markdown text to format |
| 59 | * @returns {string} The formatted HTML text |
| 60 | */ |
| 61 | function parseTextFormatting(markdown) { |
| 62 | // Apply bold formatting first, then italic formatting |
| 63 | return parseItalic(parseBold(markdown)); |
| 21 | 64 | } |
| 22 | 65 | |
| 66 | /** |
| 67 | * Parses text content (handles inline formatting) |
| 68 | * @param {string} markdown - The markdown text |
| 69 | * @param {boolean} list - Whether this is part of a list |
| 70 | * @returns {string} The parsed HTML |
| 71 | */ |
| 23 | 72 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 73 | const parsedText = parseTextFormatting(markdown); |
| 25 | 74 | if (list) { |
| 26 | 75 | return parsedText; |
| 27 | 76 | } else { |
| 29 | 78 | } |
| 30 | 79 | } |
| 31 | 80 | |
| 81 | // ===== LINE TYPE PARSERS ===== |
| 82 | |
| 83 | /** |
| 84 | * Parses header lines (starting with # characters) |
| 85 | * @param {string} markdown - The markdown line to parse |
| 86 | * @param {boolean} list - Whether we are currently in a list |
| 87 | * @returns {Array} [html, inListAfter] - The parsed HTML and list state |
| 88 | */ |
| 32 | 89 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 90 | // Count consecutive # characters at the start |
| 91 | let headerLevel = 0; |
| 34 | 92 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 93 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 94 | headerLevel += 1; |
| 37 | 95 | } else { |
| 38 | 96 | break; |
| 39 | 97 | } |
| 40 | 98 | } |
| 41 | | if (count === 0 || count > 6) { |
| 99 | |
| 100 | // Valid headers have 1-6 # characters |
| 101 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 102 | return [null, list]; |
| 43 | 103 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 104 | |
| 105 | const headerTag = `h${headerLevel}`; |
| 106 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 107 | const headerHtml = wrap(headerContent, headerTag); |
| 108 | |
| 109 | // If we were in a list, close it before the header |
| 46 | 110 | if (list) { |
| 47 | 111 | return [`</ul>${headerHtml}`, false]; |
| 48 | 112 | } else { |
| 50 | 114 | } |
| 51 | 115 | } |
| 52 | 116 | |
| 117 | /** |
| 118 | * Parses list items (starting with *) |
| 119 | * @param {string} markdown - The markdown line to parse |
| 120 | * @param {boolean} list - Whether we are currently in a list |
| 121 | * @returns {Array} [html, inListAfter] - The parsed HTML and list state |
| 122 | */ |
| 53 | 123 | function parseLineItem(markdown, list) { |
| 54 | 124 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 125 | // Remove the '* ' prefix and parse the remaining text |
| 126 | const itemContent = markdown.substring(2).trim(); |
| 127 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 128 | |
| 56 | 129 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 130 | // Continue existing list |
| 131 | return [listItemHtml, true]; |
| 58 | 132 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 133 | // Start a new list |
| 134 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 135 | } |
| 61 | 136 | } |
| 62 | 137 | return [null, list]; |
| 63 | 138 | } |
| 64 | 139 | |
| 140 | /** |
| 141 | * Parses regular paragraph text |
| 142 | * @param {string} markdown - The markdown line to parse |
| 143 | * @param {boolean} list - Whether we are currently in a list |
| 144 | * @returns {Array} [html, inListAfter] - The parsed HTML and list state |
| 145 | */ |
| 65 | 146 | function parseParagraph(markdown, list) { |
| 147 | const paragraphHtml = parseText(markdown, false); |
| 148 | |
| 66 | 149 | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 150 | // Not in a list, just return the paragraph |
| 151 | return [paragraphHtml, false]; |
| 68 | 152 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 153 | // Close the list and start the paragraph |
| 154 | return [`</ul>${paragraphHtml}`, false]; |
| 70 | 155 | } |
| 71 | 156 | } |
| 72 | 157 | |
| 158 | /** |
| 159 | * Determines the type of markdown line and delegates to appropriate parser |
| 160 | * @param {string} markdown - The markdown line to parse |
| 161 | * @param {boolean} list - Whether we are currently in a list |
| 162 | * @returns {Array} [html, inListAfter] - The parsed HTML and list state |
| 163 | */ |
| 73 | 164 | function parseLine(markdown, list) { |
| 165 | // Try to parse as header first |
| 74 | 166 | let [result, inListAfter] = parseHeader(markdown, list); |
| 167 | |
| 168 | // If not a header, try to parse as list item |
| 75 | 169 | if (result === null) { |
| 76 | 170 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 171 | } |
| 172 | |
| 173 | // If not a list item, parse as paragraph |
| 78 | 174 | if (result === null) { |
| 79 | 175 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 176 | } |
| 177 | |
| 178 | // This should never happen as one of the parsers should always succeed |
| 81 | 179 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 180 | throw new Error('Unable to parse markdown line'); |
| 83 | 181 | } |
| 182 | |
| 84 | 183 | return [result, inListAfter]; |
| 85 | 184 | } |
| 86 | 185 | |
| 87 | 186 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 187 | * Main entry point for parsing markdown to HTML |
| 188 | * @param {string} markdown - The complete markdown text to parse |
| 189 | * @returns {string} The resulting HTML |
| 90 | 190 | */ |
| 91 | 191 | export function parse(markdown) { |
| 192 | // Split the markdown into individual lines |
| 92 | 193 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 194 | let htmlResult = ''; |
| 195 | let inList = false; |
| 196 | |
| 197 | // Process each line individually |
| 95 | 198 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 199 | const [lineHtml, newListState] = parseLine(lines[i], inList); |
| 200 | htmlResult += lineHtml; |
| 201 | inList = newListState; |
| 99 | 202 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 203 | |
| 204 | // Close any open list at the end |
| 205 | if (inList) { |
| 206 | return htmlResult + '</ul>'; |
| 102 | 207 | } else { |
| 103 | | return result; |
| 208 | return htmlResult; |
| 104 | 209 | } |
| 105 | 210 | } |