| 1 | /** |
| 2 | * Refactored Markdown Parser |
| 3 | * |
| 4 | * This refactored version improves readability and maintainability while |
| 5 | * preserving all existing functionality. |
| 6 | */ |
| 7 | |
| 8 | // Helper function to wrap text in HTML tags |
| 1 | 9 | function wrap(text, tag) { |
| 2 | 10 | return `<${tag}>${text}</${tag}>`; |
| 3 | 11 | } |
| 4 | 12 | |
| 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}>`; |
| 13 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 14 | function parseBold(markdown) { |
| 15 | const pattern = /__(.+)__/; |
| 16 | const replacement = '<strong>$1</strong>'; |
| 12 | 17 | return markdown.replace(pattern, replacement); |
| 13 | 18 | } |
| 14 | 19 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 20 | // Parse italic text (_text_ -> <em>text</em>) |
| 21 | function parseItalic(markdown) { |
| 22 | const pattern = /_(.+)_/; |
| 23 | const replacement = '<em>$1</em>'; |
| 24 | return markdown.replace(pattern, replacement); |
| 17 | 25 | } |
| 18 | 26 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 27 | // Parse inline formatting (bold and italic) |
| 28 | function parseInlineFormatting(markdown) { |
| 29 | return parseItalic(parseBold(markdown)); |
| 21 | 30 | } |
| 22 | 31 | |
| 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 | | } |
| 32 | // Parse text content, optionally wrapping in paragraph tags |
| 33 | function parseText(markdown, inList = false) { |
| 34 | const formattedText = parseInlineFormatting(markdown); |
| 35 | return inList ? formattedText : wrap(formattedText, 'p'); |
| 30 | 36 | } |
| 31 | 37 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 38 | // Parse header (# Header -> <h1>Header</h1>) |
| 39 | function parseHeader(markdown, currentlyInList) { |
| 40 | // Count leading # characters |
| 41 | let headerLevel = 0; |
| 34 | 42 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 43 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 44 | headerLevel++; |
| 37 | 45 | } else { |
| 38 | 46 | break; |
| 39 | 47 | } |
| 40 | 48 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 49 | |
| 50 | // Valid headers have 1-6 # characters |
| 51 | if (headerLevel === 0 || headerLevel > 6) { |
| 52 | return [null, currentlyInList]; |
| 43 | 53 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 54 | |
| 55 | // Extract header content (skip # characters and space) |
| 56 | const headerContent = markdown.substring(headerLevel + 1); |
| 57 | const headerTag = `h${headerLevel}`; |
| 58 | const headerHtml = wrap(headerContent, headerTag); |
| 59 | |
| 60 | // If we were in a list, close it before the header |
| 61 | if (currentlyInList) { |
| 47 | 62 | return [`</ul>${headerHtml}`, false]; |
| 48 | | } else { |
| 49 | | return [headerHtml, false]; |
| 50 | 63 | } |
| 64 | |
| 65 | return [headerHtml, false]; |
| 51 | 66 | } |
| 52 | 67 | |
| 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 | | } |
| 68 | // Parse list items (* item -> <ul><li>item</li>) |
| 69 | function parseListItem(markdown, currentlyInList) { |
| 70 | if (!markdown.startsWith('*')) { |
| 71 | return [null, currentlyInList]; |
| 72 | } |
| 73 | |
| 74 | // Extract list item content (skip '* ' prefix) |
| 75 | const itemContent = markdown.substring(2); |
| 76 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 77 | |
| 78 | // If we're already in a list, just add the list item |
| 79 | // Otherwise, start a new list |
| 80 | if (currentlyInList) { |
| 81 | return [listItemHtml, true]; |
| 82 | } else { |
| 83 | return [`<ul>${listItemHtml}`, true]; |
| 61 | 84 | } |
| 62 | | return [null, list]; |
| 63 | 85 | } |
| 64 | 86 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 87 | // Parse paragraph content |
| 88 | function parseParagraph(markdown, currentlyInList) { |
| 89 | // If we were in a list, close it before the paragraph |
| 90 | if (currentlyInList) { |
| 69 | 91 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 92 | } |
| 93 | |
| 94 | return [parseText(markdown, false), false]; |
| 71 | 95 | } |
| 72 | 96 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 97 | // Parse a single line of markdown |
| 98 | function parseLine(markdown, currentlyInList) { |
| 99 | // Try parsing as header first |
| 100 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 101 | |
| 102 | // If not a header, try parsing as list item |
| 75 | 103 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 104 | [result, newListState] = parseListItem(markdown, currentlyInList); |
| 77 | 105 | } |
| 106 | |
| 107 | // If not a list item, parse as paragraph |
| 78 | 108 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 109 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 110 | } |
| 111 | |
| 112 | // This should never happen with valid input |
| 81 | 113 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 114 | throw new Error('Invalid markdown line'); |
| 83 | 115 | } |
| 84 | | return [result, inListAfter]; |
| 116 | |
| 117 | return [result, newListState]; |
| 85 | 118 | } |
| 86 | 119 | |
| 87 | 120 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 121 | * Parse markdown text and convert to HTML |
| 122 | * @param {string} markdown - The markdown text to parse |
| 123 | * @returns {string} - The resulting HTML |
| 90 | 124 | */ |
| 91 | 125 | export function parse(markdown) { |
| 92 | 126 | 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; |
| 127 | let htmlResult = ''; |
| 128 | let inList = false; |
| 129 | |
| 130 | // Process each line |
| 131 | for (const line of lines) { |
| 132 | const [lineHtml, newListState] = parseLine(line, inList); |
| 133 | htmlResult += lineHtml; |
| 134 | inList = newListState; |
| 99 | 135 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 136 | |
| 137 | // Close any open list at the end |
| 138 | if (inList) { |
| 139 | htmlResult += '</ul>'; |
| 104 | 140 | } |
| 141 | |
| 142 | return htmlResult; |
| 105 | 143 | } |