| 1 | /** |
| 2 | * Refactored Markdown parser |
| 3 | * |
| 4 | * This refactored version maintains the exact same functionality as the original |
| 5 | * but with improved readability and maintainability. |
| 6 | * |
| 7 | * Key improvements: |
| 8 | * - Clearer separation of concerns with dedicated functions for each element type |
| 9 | * - More intuitive list handling logic |
| 10 | * - Better inline formatting processing |
| 11 | * - Cleaner code structure with explicit state management |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Wraps text with HTML tags |
| 16 | * @param {string} text - Text to wrap |
| 17 | * @param {string} tag - HTML tag to use |
| 18 | * @returns {string} Wrapped HTML |
| 19 | */ |
| 1 | 20 | function wrap(text, tag) { |
| 2 | 21 | return `<${tag}>${text}</${tag}>`; |
| 3 | 22 | } |
| 4 | 23 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 24 | /** |
| 25 | * Processes bold formatting (__text__ -> <strong>text</strong>) |
| 26 | * @param {string} markdown - Markdown text |
| 27 | * @returns {string} HTML with bold formatting applied |
| 28 | */ |
| 29 | function parseBold(markdown) { |
| 30 | // Use greedy matching (.+) to match original behavior exactly |
| 31 | return markdown.replace(/__(.+)__/, '<strong>$1</strong>'); |
| 13 | 32 | } |
| 14 | 33 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 34 | /** |
| 35 | * Processes italic formatting (_text_ -> <em>text</em>) |
| 36 | * @param {string} markdown - Markdown text |
| 37 | * @returns {string} HTML with italic formatting applied |
| 38 | */ |
| 39 | function parseItalic(markdown) { |
| 40 | // Only replace the first occurrence to match original behavior |
| 41 | return markdown.replace(/_(.+)_/, '<em>$1</em>'); |
| 17 | 42 | } |
| 18 | 43 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 44 | /** |
| 45 | * Processes inline formatting (bold and italic) |
| 46 | * @param {string} markdown - Markdown text |
| 47 | * @returns {string} HTML with inline formatting applied |
| 48 | */ |
| 49 | function processInlineFormatting(markdown) { |
| 50 | // Process bold first, then italic to handle nested formatting correctly |
| 51 | return parseItalic(parseBold(markdown)); |
| 21 | 52 | } |
| 22 | 53 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 54 | /** |
| 55 | * Processes paragraph text (wraps in <p> tags unless in a list) |
| 56 | * @param {string} markdown - Markdown text |
| 57 | * @param {boolean} inList - Whether we're currently in a list |
| 58 | * @returns {string} HTML paragraph or plain text |
| 59 | */ |
| 60 | function processParagraphText(markdown, inList) { |
| 61 | const formattedText = processInlineFormatting(markdown); |
| 62 | return inList ? formattedText : wrap(formattedText, 'p'); |
| 30 | 63 | } |
| 31 | 64 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 34 | | for (let i = 0; i < markdown.length; i++) { |
| 35 | | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 37 | | } else { |
| 38 | | break; |
| 39 | | } |
| 40 | | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 65 | /** |
| 66 | * Parses header markdown (# Header -> <h1>Header</h1>) |
| 67 | * @param {string} markdown - Line of markdown text |
| 68 | * @returns {Object|null} Parsed header object or null if not a header |
| 69 | */ |
| 70 | function parseHeader(markdown) { |
| 71 | // Count leading # characters |
| 72 | let level = 0; |
| 73 | while (level < markdown.length && markdown[level] === '#') { |
| 74 | level++; |
| 43 | 75 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 47 | | return [`</ul>${headerHtml}`, false]; |
| 48 | | } else { |
| 49 | | return [headerHtml, false]; |
| 76 | |
| 77 | // Valid headers are 1-6 # characters |
| 78 | if (level === 0 || level > 6) { |
| 79 | return null; |
| 50 | 80 | } |
| 81 | |
| 82 | // Extract header content (skip spaces after #) |
| 83 | const content = markdown.substring(level).trimStart(); |
| 84 | const headerTag = `h${level}`; |
| 85 | |
| 86 | return { |
| 87 | html: wrap(content, headerTag), |
| 88 | closesList: true |
| 89 | }; |
| 51 | 90 | } |
| 52 | 91 | |
| 53 | | function parseLineItem(markdown, list) { |
| 54 | | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 58 | | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 60 | | } |
| 92 | /** |
| 93 | * Parses list item markdown (* item -> <li>item</li>) |
| 94 | * @param {string} markdown - Line of markdown text |
| 95 | * @param {boolean} currentlyInList - Whether we're already in a list |
| 96 | * @returns {Object|null} Parsed list item object or null if not a list item |
| 97 | */ |
| 98 | function parseListItem(markdown, currentlyInList) { |
| 99 | if (!markdown.startsWith('* ')) { |
| 100 | return null; |
| 61 | 101 | } |
| 62 | | return [null, list]; |
| 102 | |
| 103 | // Extract list item content |
| 104 | const content = markdown.substring(2); |
| 105 | const formattedContent = processParagraphText(content, true); |
| 106 | const listItemHtml = wrap(formattedContent, 'li'); |
| 107 | |
| 108 | return { |
| 109 | html: listItemHtml, |
| 110 | opensList: !currentlyInList, |
| 111 | inList: true |
| 112 | }; |
| 63 | 113 | } |
| 64 | 114 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 115 | /** |
| 116 | * Parses paragraph markdown (regular text) |
| 117 | * @param {string} markdown - Line of markdown text |
| 118 | * @param {boolean} currentlyInList - Whether we're already in a list |
| 119 | * @returns {Object} Parsed paragraph object |
| 120 | */ |
| 121 | function parseParagraph(markdown, currentlyInList) { |
| 122 | const paragraphHtml = processParagraphText(markdown, false); |
| 123 | |
| 124 | return { |
| 125 | html: paragraphHtml, |
| 126 | closesList: currentlyInList |
| 127 | }; |
| 71 | 128 | } |
| 72 | 129 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 130 | /** |
| 131 | * Parses a single line of markdown |
| 132 | * @param {string} line - Line of markdown text |
| 133 | * @param {boolean} currentlyInList - Whether we're currently in a list |
| 134 | * @returns {Object} Parsed line result |
| 135 | */ |
| 136 | function parseLine(line, currentlyInList) { |
| 137 | // Try to parse as header |
| 138 | let result = parseHeader(line); |
| 139 | if (result) { |
| 140 | return result; |
| 80 | 141 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 142 | |
| 143 | // Try to parse as list item |
| 144 | result = parseListItem(line, currentlyInList); |
| 145 | if (result) { |
| 146 | return result; |
| 83 | 147 | } |
| 84 | | return [result, inListAfter]; |
| 148 | |
| 149 | // Default to paragraph |
| 150 | return parseParagraph(line, currentlyInList); |
| 85 | 151 | } |
| 86 | 152 | |
| 87 | 153 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 154 | * Main markdown parser function |
| 155 | * @param {string} markdown - Markdown text to parse |
| 156 | * @returns {string} Generated HTML |
| 90 | 157 | */ |
| 91 | 158 | export function parse(markdown) { |
| 92 | 159 | 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; |
| 160 | let html = ''; |
| 161 | let inList = false; |
| 162 | |
| 163 | for (const line of lines) { |
| 164 | const result = parseLine(line, inList); |
| 165 | |
| 166 | // Handle list opening |
| 167 | if (result.opensList) { |
| 168 | html += '<ul>'; |
| 169 | } |
| 170 | |
| 171 | // Handle list closing |
| 172 | if (result.closesList) { |
| 173 | if (inList) { |
| 174 | html += '</ul>'; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Add the parsed content |
| 179 | html += result.html; |
| 180 | |
| 181 | // Update list state |
| 182 | inList = result.inList || false; |
| 99 | 183 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 184 | |
| 185 | // Close any remaining open list |
| 186 | if (inList) { |
| 187 | html += '</ul>'; |
| 104 | 188 | } |
| 189 | |
| 190 | return html; |
| 105 | 191 | } |