| 1 | // REFACTORING: Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 6 | // REFACTORING: Helper function to check if text starts with a specific HTML tag |
| 5 | 7 | function isTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 11 | // REFACTORING: Generic parser for emphasis (bold/italic) - renamed from 'parser' for clarity |
| 12 | function parseEmphasis(markdown, delimiter, tag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 14 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // REFACTORING: Parse bold text (renamed from parse__ for clarity) |
| 19 | function parseBold(markdown) { |
| 20 | return parseEmphasis(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 23 | // REFACTORING: Parse italic text (renamed from parse_ for clarity) |
| 24 | function parseItalic(markdown) { |
| 25 | return parseEmphasis(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 28 | // REFACTORING: Parse text content with emphasis, handling list context |
| 29 | function parseTextContent(markdown, isInsideList) { |
| 30 | const parsedText = parseItalic(parseBold(markdown)); |
| 31 | if (isInsideList) { |
| 26 | 32 | return parsedText; |
| 27 | 33 | } else { |
| 28 | 34 | return wrap(parsedText, 'p'); |
| 29 | 35 | } |
| 30 | 36 | } |
| 31 | 37 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 38 | // REFACTORING: Parse header lines (1-6 levels) with better variable names and logic |
| 39 | function parseHeader(markdown, isInsideList) { |
| 40 | let headerLevel = 0; |
| 41 | |
| 42 | // Count consecutive # characters at the start |
| 34 | 43 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 44 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 45 | headerLevel += 1; |
| 37 | 46 | } else { |
| 38 | 47 | break; |
| 39 | 48 | } |
| 40 | 49 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 50 | |
| 51 | // Valid headers are 1-6 levels |
| 52 | if (headerLevel === 0 || headerLevel > 6) { |
| 53 | return [null, isInsideList]; |
| 43 | 54 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 55 | |
| 56 | const headerTag = `h${headerLevel}`; |
| 57 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 58 | const headerHtml = wrap(headerContent, headerTag); |
| 59 | |
| 60 | // If we were inside a list, close it before the header |
| 61 | if (isInsideList) { |
| 47 | 62 | return [`</ul>${headerHtml}`, false]; |
| 48 | 63 | } else { |
| 49 | 64 | return [headerHtml, false]; |
| 50 | 65 | } |
| 51 | 66 | } |
| 52 | 67 | |
| 53 | | function parseLineItem(markdown, list) { |
| 68 | // REFACTORING: Parse list items starting with * - improved clarity and variable names |
| 69 | function parseListItem(markdown, isInsideList) { |
| 54 | 70 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 71 | const itemContent = markdown.substring(2).trim(); |
| 72 | const listItemHtml = wrap(parseTextContent(itemContent, true), 'li'); |
| 73 | |
| 74 | if (isInsideList) { |
| 75 | return [listItemHtml, true]; |
| 58 | 76 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 77 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 78 | } |
| 61 | 79 | } |
| 62 | | return [null, list]; |
| 80 | return [null, isInsideList]; |
| 63 | 81 | } |
| 64 | 82 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 83 | // REFACTORING: Parse regular paragraphs - handles list context properly |
| 84 | function parseParagraph(markdown, isInsideList) { |
| 85 | if (!isInsideList) { |
| 86 | return [parseTextContent(markdown, false), false]; |
| 68 | 87 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 88 | return [`</ul>${parseTextContent(markdown, false)}`, false]; |
| 70 | 89 | } |
| 71 | 90 | } |
| 72 | 91 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 92 | // REFACTORING: Main line parsing logic - much clearer flow with early returns |
| 93 | function parseLine(markdown, isInsideList) { |
| 94 | // Try to parse as header first |
| 95 | let [result, newInsideList] = parseHeader(markdown, isInsideList); |
| 96 | if (result !== null) { |
| 97 | return [result, newInsideList]; |
| 77 | 98 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 99 | |
| 100 | // Try to parse as list item |
| 101 | [result, newInsideList] = parseListItem(markdown, isInsideList); |
| 102 | if (result !== null) { |
| 103 | return [result, newInsideList]; |
| 80 | 104 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 105 | |
| 106 | // Default to paragraph |
| 107 | [result, newInsideList] = parseParagraph(markdown, isInsideList); |
| 108 | if (result !== null) { |
| 109 | return [result, newInsideList]; |
| 83 | 110 | } |
| 84 | | return [result, inListAfter]; |
| 111 | |
| 112 | // This should never happen with valid markdown |
| 113 | throw new Error('Unable to parse markdown line'); |
| 85 | 114 | } |
| 86 | 115 | |
| 87 | 116 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 117 | * REFACTORING: Main markdown parser function - improved variable names and logic |
| 118 | * @param {string} markdown - The markdown string to parse |
| 119 | * @returns {string} - The resulting HTML |
| 90 | 120 | */ |
| 91 | 121 | export function parse(markdown) { |
| 92 | 122 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 123 | let htmlResult = ''; |
| 124 | let isInsideList = false; |
| 125 | |
| 126 | // Process each line of markdown |
| 95 | 127 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 128 | const [lineHtml, newInsideList] = parseLine(lines[i], isInsideList); |
| 129 | htmlResult += lineHtml; |
| 130 | isInsideList = newInsideList; |
| 99 | 131 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 132 | |
| 133 | // Close any unclosed list |
| 134 | if (isInsideList) { |
| 135 | return htmlResult + '</ul>'; |
| 102 | 136 | } else { |
| 103 | | return result; |
| 137 | return htmlResult; |
| 104 | 138 | } |
| 105 | 139 | } |