| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 4 | | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 1 | /** |
| 2 | * Refactored Markdown Parser |
| 3 | * |
| 4 | * This refactored version improves readability and maintainability while |
| 5 | * preserving all original functionality. |
| 6 | * |
| 7 | * Key improvements: |
| 8 | * - Clearer separation of concerns |
| 9 | * - Better organization of parsing logic |
| 10 | * - More descriptive function and variable names |
| 11 | * - Simplified state management |
| 12 | * - Better code structure and flow |
| 13 | */ |
| 8 | 14 | |
| 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); |
| 15 | /** |
| 16 | * Wraps text with HTML tags |
| 17 | * @param {string} text - Text to wrap |
| 18 | * @param {string} tag - HTML tag to use |
| 19 | * @returns {string} Text wrapped in HTML tags |
| 20 | */ |
| 21 | function wrapText(text, tag) { |
| 22 | return `<${tag}>${text}</${tag}>`; |
| 13 | 23 | } |
| 14 | 24 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 25 | /** |
| 26 | * Parses bold text (marked with __) |
| 27 | * @param {string} markdown - Markdown text to parse |
| 28 | * @returns {string} HTML with bold tags |
| 29 | */ |
| 30 | function parseBold(markdown) { |
| 31 | const pattern = /__(.+?)__/g; |
| 32 | return markdown.replace(pattern, (match, content) => wrapText(content, 'strong')); |
| 17 | 33 | } |
| 18 | 34 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 35 | /** |
| 36 | * Parses italic text (marked with _) |
| 37 | * @param {string} markdown - Markdown text to parse |
| 38 | * @returns {string} HTML with italic tags |
| 39 | */ |
| 40 | function parseItalic(markdown) { |
| 41 | const pattern = /_(.+?)_/g; |
| 42 | return markdown.replace(pattern, (match, content) => wrapText(content, 'em')); |
| 21 | 43 | } |
| 22 | 44 | |
| 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 | | } |
| 45 | /** |
| 46 | * Parses inline formatting (bold and italic) |
| 47 | * @param {string} markdown - Markdown text to parse |
| 48 | * @returns {string} HTML with inline formatting |
| 49 | */ |
| 50 | function parseInlineFormatting(markdown) { |
| 51 | return parseItalic(parseBold(markdown)); |
| 30 | 52 | } |
| 31 | 53 | |
| 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]; |
| 54 | /** |
| 55 | * Parses a header line |
| 56 | * @param {string} line - Line to parse |
| 57 | * @returns {string|null} HTML header or null if not a header |
| 58 | */ |
| 59 | function parseHeader(line) { |
| 60 | // Count leading # characters |
| 61 | let level = 0; |
| 62 | while (level < line.length && line[level] === '#') { |
| 63 | level++; |
| 43 | 64 | } |
| 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]; |
| 65 | |
| 66 | // Valid headers have 1-6 # characters |
| 67 | if (level === 0 || level > 6) { |
| 68 | return null; |
| 50 | 69 | } |
| 70 | |
| 71 | // Extract header content (skip # and space) |
| 72 | const content = line.substring(level + 1); |
| 73 | const headerTag = `h${level}`; |
| 74 | |
| 75 | return wrapText(parseInlineFormatting(content), headerTag); |
| 51 | 76 | } |
| 52 | 77 | |
| 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 | | } |
| 78 | /** |
| 79 | * Parses a list item |
| 80 | * @param {string} line - Line to parse |
| 81 | * @returns {string|null} HTML list item or null if not a list item |
| 82 | */ |
| 83 | function parseListItem(line) { |
| 84 | if (!line.startsWith('*')) { |
| 85 | return null; |
| 61 | 86 | } |
| 62 | | return [null, list]; |
| 87 | |
| 88 | // Extract list item content (skip * and space) |
| 89 | const content = line.substring(2); |
| 90 | return wrapText(parseInlineFormatting(content), 'li'); |
| 63 | 91 | } |
| 64 | 92 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 93 | /** |
| 94 | * Parses a paragraph |
| 95 | * @param {string} line - Line to parse |
| 96 | * @returns {string} HTML paragraph |
| 97 | */ |
| 98 | function parseParagraph(line) { |
| 99 | return wrapText(parseInlineFormatting(line), 'p'); |
| 71 | 100 | } |
| 72 | 101 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 102 | /** |
| 103 | * Parses a single line of markdown |
| 104 | * @param {string} line - Line to parse |
| 105 | * @param {boolean} inList - Whether we're currently in a list |
| 106 | * @returns {[string, boolean]} Parsed HTML and whether we're still in a list |
| 107 | */ |
| 108 | function parseLine(line, inList) { |
| 109 | // Try to parse as header |
| 110 | const header = parseHeader(line); |
| 111 | if (header !== null) { |
| 112 | // If we were in a list, close it before the header |
| 113 | if (inList) { |
| 114 | return [`</ul>${header}`, false]; |
| 115 | } |
| 116 | return [header, false]; |
| 77 | 117 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 118 | |
| 119 | // Try to parse as list item |
| 120 | const listItem = parseListItem(line); |
| 121 | if (listItem !== null) { |
| 122 | if (inList) { |
| 123 | // Continue existing list |
| 124 | return [listItem, true]; |
| 125 | } else { |
| 126 | // Start new list |
| 127 | return [`<ul>${listItem}`, true]; |
| 128 | } |
| 80 | 129 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 130 | |
| 131 | // Parse as paragraph |
| 132 | const paragraph = parseParagraph(line); |
| 133 | if (inList) { |
| 134 | // If we were in a list, close it before the paragraph |
| 135 | return [`</ul>${paragraph}`, false]; |
| 83 | 136 | } |
| 84 | | return [result, inListAfter]; |
| 137 | return [paragraph, false]; |
| 85 | 138 | } |
| 86 | 139 | |
| 87 | 140 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 141 | * Parses markdown text and converts it to HTML |
| 142 | * @param {string} markdown - Markdown text to parse |
| 143 | * @returns {string} HTML representation |
| 90 | 144 | */ |
| 91 | 145 | export function parse(markdown) { |
| 92 | 146 | 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; |
| 147 | let html = ''; |
| 148 | let inList = false; |
| 149 | |
| 150 | for (const line of lines) { |
| 151 | const [lineHtml, newListState] = parseLine(line, inList); |
| 152 | html += lineHtml; |
| 153 | inList = newListState; |
| 99 | 154 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 155 | |
| 156 | // Close any open list at the end |
| 157 | if (inList) { |
| 158 | html += '</ul>'; |
| 104 | 159 | } |
| 160 | |
| 161 | return html; |
| 105 | 162 | } |
| 163 | |
| 164 | |