| 1 | | function wrap(text, tag) { |
| 1 | // REFACTORING NOTES: |
| 2 | // 1. Improved function naming for clarity |
| 3 | // 2. Separated concerns better |
| 4 | // 3. Added comprehensive comments |
| 5 | // 4. Simplified control flow |
| 6 | // 5. Made state management more explicit |
| 7 | |
| 8 | /** |
| 9 | * Wraps text in HTML tags |
| 10 | * @param {string} text - The text to wrap |
| 11 | * @param {string} tag - The HTML tag name |
| 12 | * @returns {string} The wrapped text |
| 13 | */ |
| 14 | function wrapTextInTag(text, tag) { |
| 2 | 15 | return `<${tag}>${text}</${tag}>`; |
| 3 | 16 | } |
| 4 | 17 | |
| 5 | | function isTag(text, tag) { |
| 18 | /** |
| 19 | * Checks if text starts with a specific HTML tag |
| 20 | * @param {string} text - The text to check |
| 21 | * @param {string} tag - The tag to look for |
| 22 | * @returns {boolean} Whether the text starts with the tag |
| 23 | */ |
| 24 | function startsWithTag(text, tag) { |
| 6 | 25 | return text.startsWith(`<${tag}>`); |
| 7 | 26 | } |
| 8 | 27 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 28 | /** |
| 29 | * Generic parser for markdown delimiters (like * for emphasis, __ for bold) |
| 30 | * @param {string} markdown - The markdown text to parse |
| 31 | * @param {string} delimiter - The delimiter pattern |
| 32 | * @param {string} htmlTag - The HTML tag to replace with |
| 33 | * @returns {string} The parsed HTML |
| 34 | */ |
| 35 | function parseMarkdownDelimiter(markdown, delimiter, htmlTag) { |
| 10 | 36 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 37 | const replacement = `<${htmlTag}>$1</${htmlTag}>`; |
| 12 | 38 | return markdown.replace(pattern, replacement); |
| 13 | 39 | } |
| 14 | 40 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 41 | /** |
| 42 | * Parses bold text (using __ delimiter) |
| 43 | * @param {string} markdown - The markdown text to parse |
| 44 | * @returns {string} The parsed HTML with bold tags |
| 45 | */ |
| 46 | function parseBoldText(markdown) { |
| 47 | return parseMarkdownDelimiter(markdown, '__', 'strong'); |
| 17 | 48 | } |
| 18 | 49 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 50 | /** |
| 51 | * Parses italic text (using _ delimiter) |
| 52 | * @param {string} markdown - The markdown text to parse |
| 53 | * @returns {string} The parsed HTML with emphasis tags |
| 54 | */ |
| 55 | function parseItalicText(markdown) { |
| 56 | return parseMarkdownDelimiter(markdown, '_', 'em'); |
| 21 | 57 | } |
| 22 | 58 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 59 | /** |
| 60 | * Parses text formatting (bold and italic) and optionally wraps in paragraph tags |
| 61 | * @param {string} markdown - The markdown text to parse |
| 62 | * @param {boolean} isListItem - Whether this text is part of a list item |
| 63 | * @returns {string} The parsed HTML |
| 64 | */ |
| 65 | function parseTextFormatting(markdown, isListItem) { |
| 66 | // Apply bold formatting first, then italic (order matters for nested formatting) |
| 67 | const parsedText = parseItalicText(parseBoldText(markdown)); |
| 68 | |
| 69 | if (isListItem) { |
| 26 | 70 | return parsedText; |
| 27 | 71 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 72 | return wrapTextInTag(parsedText, 'p'); |
| 29 | 73 | } |
| 30 | 74 | } |
| 31 | 75 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 76 | /** |
| 77 | * Parses markdown headers (lines starting with #) |
| 78 | * @param {string} markdown - The markdown line to parse |
| 79 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 80 | * @returns {Array} [parsedHtml, stillInList] - The parsed HTML and updated list state |
| 81 | */ |
| 82 | function parseHeader(markdown, currentlyInList) { |
| 83 | // Count consecutive # characters at the start |
| 84 | let headerLevel = 0; |
| 34 | 85 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 86 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 87 | headerLevel += 1; |
| 37 | 88 | } else { |
| 38 | 89 | break; |
| 39 | 90 | } |
| 40 | 91 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 92 | |
| 93 | // Valid headers have 1-6 # characters |
| 94 | if (headerLevel === 0 || headerLevel > 6) { |
| 95 | return [null, currentlyInList]; |
| 43 | 96 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 97 | |
| 98 | const headerTag = `h${headerLevel}`; |
| 99 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 100 | const headerHtml = wrapTextInTag(headerContent, headerTag); |
| 101 | |
| 102 | // If we were in a list, close it before the header |
| 103 | if (currentlyInList) { |
| 47 | 104 | return [`</ul>${headerHtml}`, false]; |
| 48 | 105 | } else { |
| 49 | 106 | return [headerHtml, false]; |
| 50 | 107 | } |
| 51 | 108 | } |
| 52 | 109 | |
| 53 | | function parseLineItem(markdown, list) { |
| 110 | /** |
| 111 | * Parses markdown list items (lines starting with *) |
| 112 | * @param {string} markdown - The markdown line to parse |
| 113 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 114 | * @returns {Array} [parsedHtml, stillInList] - The parsed HTML and updated list state |
| 115 | */ |
| 116 | function parseListItem(markdown, currentlyInList) { |
| 54 | 117 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 118 | const listItemContent = markdown.substring(2).trim(); // Remove '* ' prefix |
| 119 | const formattedContent = parseTextFormatting(listItemContent, true); |
| 120 | const listItemHtml = wrapTextInTag(formattedContent, 'li'); |
| 121 | |
| 122 | if (currentlyInList) { |
| 123 | // Continue existing list |
| 124 | return [listItemHtml, true]; |
| 58 | 125 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 126 | // Start new list |
| 127 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 128 | } |
| 61 | 129 | } |
| 62 | | return [null, list]; |
| 130 | return [null, currentlyInList]; |
| 63 | 131 | } |
| 64 | 132 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 133 | /** |
| 134 | * Parses regular paragraphs (fallback for lines that aren't headers or list items) |
| 135 | * @param {string} markdown - The markdown line to parse |
| 136 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 137 | * @returns {Array} [parsedHtml, stillInList] - The parsed HTML and updated list state |
| 138 | */ |
| 139 | function parseParagraph(markdown, currentlyInList) { |
| 140 | if (!currentlyInList) { |
| 141 | // Regular paragraph when not in a list |
| 142 | return [parseTextFormatting(markdown, false), false]; |
| 68 | 143 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 144 | // Close list and start new paragraph |
| 145 | return [`</ul>${parseTextFormatting(markdown, false)}`, false]; |
| 70 | 146 | } |
| 71 | 147 | } |
| 72 | 148 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 149 | /** |
| 150 | * Parses a single line of markdown by trying different parsers in order |
| 151 | * @param {string} markdown - The markdown line to parse |
| 152 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 153 | * @returns {Array} [parsedHtml, stillInList] - The parsed HTML and updated list state |
| 154 | */ |
| 155 | function parseMarkdownLine(markdown, currentlyInList) { |
| 156 | // Try parsing as header first (highest priority) |
| 157 | let [parsedHtml, inListState] = parseHeader(markdown, currentlyInList); |
| 158 | |
| 159 | // If not a header, try parsing as list item |
| 160 | if (parsedHtml === null) { |
| 161 | [parsedHtml, inListState] = parseListItem(markdown, currentlyInList); |
| 77 | 162 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 163 | |
| 164 | // If not header or list item, parse as paragraph (fallback) |
| 165 | if (parsedHtml === null) { |
| 166 | [parsedHtml, inListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 167 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 168 | |
| 169 | // This should never happen since parseParagraph always returns a result |
| 170 | if (parsedHtml === null) { |
| 171 | throw new Error('Failed to parse markdown line'); |
| 83 | 172 | } |
| 84 | | return [result, inListAfter]; |
| 173 | |
| 174 | return [parsedHtml, inListState]; |
| 85 | 175 | } |
| 86 | 176 | |
| 87 | 177 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 178 | * Main markdown parser function - converts markdown to HTML |
| 179 | * Processes the input line by line, maintaining list state across lines |
| 180 | * @param {string} markdown - The markdown text to parse |
| 181 | * @returns {string} The resulting HTML |
| 90 | 182 | */ |
| 91 | 183 | export function parse(markdown) { |
| 92 | 184 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 185 | let htmlResult = ''; |
| 186 | let currentlyInList = false; |
| 187 | |
| 188 | // Process each line, maintaining list state |
| 95 | 189 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 190 | const [lineHtml, stillInList] = parseMarkdownLine(lines[i], currentlyInList); |
| 191 | htmlResult += lineHtml; |
| 192 | currentlyInList = stillInList; |
| 99 | 193 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 194 | |
| 195 | // Close any unclosed list at the end |
| 196 | if (currentlyInList) { |
| 197 | return htmlResult + '</ul>'; |
| 102 | 198 | } else { |
| 103 | | return result; |
| 199 | return htmlResult; |
| 104 | 200 | } |
| 105 | 201 | } |