| 1 | // Refactored Markdown Parser |
| 2 | // This implementation maintains the same functionality while improving readability and maintainability |
| 3 | |
| 4 | /** |
| 5 | * Wraps text with HTML tags |
| 6 | * @param {string} text - The text to wrap |
| 7 | * @param {string} tag - The HTML tag to use |
| 8 | * @returns {string} The wrapped text |
| 9 | */ |
| 1 | 10 | function wrap(text, tag) { |
| 2 | 11 | return `<${tag}>${text}</${tag}>`; |
| 3 | 12 | } |
| 4 | 13 | |
| 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}>`; |
| 14 | /** |
| 15 | * Parses bold text (double underscore) |
| 16 | * @param {string} markdown - The markdown text to parse |
| 17 | * @returns {string} The parsed HTML |
| 18 | */ |
| 19 | function parseBold(markdown) { |
| 20 | // Note: This matches the original implementation's behavior |
| 21 | // The original regex was greedy (.+) which could cause issues |
| 22 | // with nested emphasis, but we're maintaining that behavior for |
| 23 | // backward compatibility in this refactoring exercise |
| 24 | const pattern = /__(.+)__/; |
| 25 | const replacement = '<strong>$1</strong>'; |
| 12 | 26 | return markdown.replace(pattern, replacement); |
| 13 | 27 | } |
| 14 | 28 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 29 | /** |
| 30 | * Parses italic text (single underscore) |
| 31 | * @param {string} markdown - The markdown text to parse |
| 32 | * @returns {string} The parsed HTML |
| 33 | */ |
| 34 | function parseItalic(markdown) { |
| 35 | // Note: This matches the original implementation's behavior |
| 36 | // The original regex was greedy (.+) which could cause issues |
| 37 | // with nested emphasis, but we're maintaining that behavior for |
| 38 | // backward compatibility in this refactoring exercise |
| 39 | const pattern = /_(.+)_/; |
| 40 | const replacement = '<em>$1</em>'; |
| 41 | return markdown.replace(pattern, replacement); |
| 21 | 42 | } |
| 22 | 43 | |
| 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 | | } |
| 44 | /** |
| 45 | * Parses inline formatting (bold and italic) |
| 46 | * @param {string} markdown - The markdown text to parse |
| 47 | * @returns {string} The parsed HTML |
| 48 | */ |
| 49 | function parseInlineFormatting(markdown) { |
| 50 | // Parse bold first, then italic to handle nested formatting correctly |
| 51 | return parseItalic(parseBold(markdown)); |
| 30 | 52 | } |
| 31 | 53 | |
| 32 | | function parseHeader(markdown, list) { |
| 54 | /** |
| 55 | * Parses a header line |
| 56 | * @param {string} line - The line to parse |
| 57 | * @returns {string|null} The parsed HTML or null if not a header |
| 58 | */ |
| 59 | function parseHeader(line) { |
| 33 | 60 | let count = 0; |
| 34 | | for (let i = 0; i < markdown.length; i++) { |
| 35 | | if (markdown[i] === '#') { |
| 61 | // Count consecutive # characters at the start of the line |
| 62 | for (let i = 0; i < line.length; i++) { |
| 63 | if (line[i] === '#') { |
| 36 | 64 | count += 1; |
| 37 | 65 | } else { |
| 38 | 66 | break; |
| 39 | 67 | } |
| 40 | 68 | } |
| 69 | |
| 70 | // Valid headers have 1-6 # characters |
| 41 | 71 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 72 | return null; |
| 43 | 73 | } |
| 74 | |
| 75 | // Extract header content (skip # characters and the following space) |
| 76 | const headerContent = line.substring(count + 1); |
| 44 | 77 | 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]; |
| 50 | | } |
| 78 | |
| 79 | return wrap(parseInlineFormatting(headerContent), headerTag); |
| 51 | 80 | } |
| 52 | 81 | |
| 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 | | } |
| 82 | /** |
| 83 | * Parses a list item line |
| 84 | * @param {string} line - The line to parse |
| 85 | * @returns {string|null} The parsed HTML or null if not a list item |
| 86 | */ |
| 87 | function parseListItem(line) { |
| 88 | // Check if line starts with '* ' (asterisk followed by space) |
| 89 | if (line.startsWith('* ')) { |
| 90 | const itemContent = line.substring(2); // Skip '* ' |
| 91 | return wrap(parseInlineFormatting(itemContent), 'li'); |
| 61 | 92 | } |
| 62 | | return [null, list]; |
| 93 | return null; |
| 63 | 94 | } |
| 64 | 95 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 96 | /** |
| 97 | * Parses a paragraph line |
| 98 | * @param {string} line - The line to parse |
| 99 | * @returns {string} The parsed HTML |
| 100 | */ |
| 101 | function parseParagraph(line) { |
| 102 | return wrap(parseInlineFormatting(line), 'p'); |
| 71 | 103 | } |
| 72 | 104 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 105 | /** |
| 106 | * Processes a single line of markdown |
| 107 | * @param {string} line - The line to process |
| 108 | * @param {boolean} inList - Whether we're currently in a list |
| 109 | * @returns {[string, boolean]} A tuple of [processed HTML, new list state] |
| 110 | */ |
| 111 | function processLine(line, inList) { |
| 112 | // Try to parse as header first |
| 113 | const headerResult = parseHeader(line); |
| 114 | if (headerResult !== null) { |
| 115 | // If we were in a list, close it before the header |
| 116 | if (inList) { |
| 117 | return [`</ul>${headerResult}`, false]; |
| 118 | } |
| 119 | return [headerResult, false]; |
| 77 | 120 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 121 | |
| 122 | // Try to parse as list item |
| 123 | const listItemResult = parseListItem(line); |
| 124 | if (listItemResult !== null) { |
| 125 | // If we weren't in a list, start one |
| 126 | if (!inList) { |
| 127 | return [`<ul>${listItemResult}`, true]; |
| 128 | } |
| 129 | return [listItemResult, true]; |
| 80 | 130 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 131 | |
| 132 | // Parse as paragraph |
| 133 | const paragraphResult = parseParagraph(line); |
| 134 | // If we were in a list, close it before the paragraph |
| 135 | if (inList) { |
| 136 | return [`</ul>${paragraphResult}`, false]; |
| 83 | 137 | } |
| 84 | | return [result, inListAfter]; |
| 138 | return [paragraphResult, false]; |
| 85 | 139 | } |
| 86 | 140 | |
| 87 | 141 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 142 | * Parses markdown text and converts it to HTML |
| 143 | * @param {string} markdown - The markdown text to parse |
| 144 | * @returns {string} The resulting HTML |
| 90 | 145 | */ |
| 91 | 146 | export function parse(markdown) { |
| 92 | 147 | const lines = markdown.split('\n'); |
| 93 | 148 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 149 | let inList = false; |
| 150 | |
| 151 | // Process each line |
| 152 | for (const line of lines) { |
| 153 | const [lineResult, newListState] = processLine(line, inList); |
| 97 | 154 | result += lineResult; |
| 98 | | list = newList; |
| 155 | inList = newListState; |
| 99 | 156 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 157 | |
| 158 | // Close any open list at the end |
| 159 | if (inList) { |
| 160 | result += '</ul>'; |
| 104 | 161 | } |
| 162 | |
| 163 | return result; |
| 105 | 164 | } |