| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 1 | // REFACTORING NOTES: |
| 2 | // - Renamed functions for clarity: wrap -> wrapInTag, parser -> replaceWithTags |
| 3 | // - Consolidated inline formatting logic into applyInlineFormatting |
| 4 | // - Improved variable naming and added comments |
| 5 | // - Maintained exact same behavior and processing order |
| 4 | 6 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | /** |
| 8 | * Wraps text in HTML tags |
| 9 | * @param {string} text - The text to wrap |
| 10 | * @param {string} tag - The HTML tag name |
| 11 | * @returns {string} The wrapped text |
| 12 | */ |
| 13 | function wrapInTag(text, tag) { |
| 14 | return `<${tag}>${text}</${tag}>`; |
| 7 | 15 | } |
| 8 | 16 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 17 | /** |
| 18 | * Replaces text between delimiters with HTML tags |
| 19 | * @param {string} text - The text to process |
| 20 | * @param {string} delimiter - The delimiter (e.g., '__', '_') |
| 21 | * @param {string} tag - The HTML tag to use |
| 22 | * @returns {string} The processed text |
| 23 | */ |
| 24 | function replaceWithTags(text, delimiter, tag) { |
| 10 | 25 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 26 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 13 | | } |
| 14 | | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 27 | return text.replace(pattern, replacement); |
| 17 | 28 | } |
| 18 | 29 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 30 | /** |
| 31 | * Applies inline formatting (bold and italic) to text |
| 32 | * Note: Processing order matters - bold (__) is processed before italic (_) |
| 33 | * @param {string} text - The text to format |
| 34 | * @returns {string} The formatted text |
| 35 | */ |
| 36 | function applyInlineFormatting(text) { |
| 37 | // Process bold formatting first (__text__ -> <strong>text</strong>) |
| 38 | const withBold = replaceWithTags(text, '__', 'strong'); |
| 39 | // Then process italic formatting (_text_ -> <em>text</em>) |
| 40 | const withItalic = replaceWithTags(withBold, '_', 'em'); |
| 41 | return withItalic; |
| 21 | 42 | } |
| 22 | 43 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 44 | /** |
| 45 | * Processes text content with inline formatting |
| 46 | * @param {string} text - The text to process |
| 47 | * @param {boolean} isListItem - Whether this text is part of a list item |
| 48 | * @returns {string} The processed text |
| 49 | */ |
| 50 | function processText(text, isListItem) { |
| 51 | const formattedText = applyInlineFormatting(text); |
| 52 | |
| 53 | if (isListItem) { |
| 54 | return formattedText; |
| 27 | 55 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 56 | return wrapInTag(formattedText, 'p'); |
| 29 | 57 | } |
| 30 | 58 | } |
| 31 | 59 | |
| 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; |
| 60 | /** |
| 61 | * Parses header lines (starting with # characters) |
| 62 | * @param {string} line - The markdown line to parse |
| 63 | * @param {boolean} isInList - Whether we're currently inside a list |
| 64 | * @returns {Array} [htmlContent, stillInList] or [null, isInList] if not a header |
| 65 | */ |
| 66 | function parseHeader(line, isInList) { |
| 67 | // Count consecutive # characters at the start |
| 68 | let headerLevel = 0; |
| 69 | for (let i = 0; i < line.length; i++) { |
| 70 | if (line[i] === '#') { |
| 71 | headerLevel += 1; |
| 37 | 72 | } else { |
| 38 | 73 | break; |
| 39 | 74 | } |
| 40 | 75 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 76 | |
| 77 | // Valid headers have 1-6 # characters |
| 78 | if (headerLevel === 0 || headerLevel > 6) { |
| 79 | return [null, isInList]; |
| 43 | 80 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 81 | |
| 82 | const headerTag = `h${headerLevel}`; |
| 83 | const headerContent = line.substring(headerLevel + 1).trim(); |
| 84 | const headerHtml = wrapInTag(headerContent, headerTag); |
| 85 | |
| 86 | // If we were in a list, close it before the header |
| 87 | if (isInList) { |
| 47 | 88 | return [`</ul>${headerHtml}`, false]; |
| 48 | 89 | } else { |
| 49 | 90 | return [headerHtml, false]; |
| 50 | 91 | } |
| 51 | 92 | } |
| 52 | 93 | |
| 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]; |
| 94 | /** |
| 95 | * Parses list item lines (starting with *) |
| 96 | * @param {string} line - The markdown line to parse |
| 97 | * @param {boolean} isInList - Whether we're currently inside a list |
| 98 | * @returns {Array} [htmlContent, stillInList] or [null, isInList] if not a list item |
| 99 | */ |
| 100 | function parseListItem(line, isInList) { |
| 101 | if (line.startsWith('*')) { |
| 102 | const itemContent = line.substring(2); // Skip '* ' prefix |
| 103 | const formattedContent = processText(itemContent, true); // true = isListItem |
| 104 | const listItemHtml = wrapInTag(formattedContent, 'li'); |
| 105 | |
| 106 | if (isInList) { |
| 107 | // Continue existing list |
| 108 | return [listItemHtml, true]; |
| 58 | 109 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 110 | // Start new list |
| 111 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 112 | } |
| 61 | 113 | } |
| 62 | | return [null, list]; |
| 114 | return [null, isInList]; |
| 63 | 115 | } |
| 64 | 116 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 117 | /** |
| 118 | * Parses paragraph lines (regular text) |
| 119 | * @param {string} line - The markdown line to parse |
| 120 | * @param {boolean} isInList - Whether we're currently inside a list |
| 121 | * @returns {Array} [htmlContent, stillInList] |
| 122 | */ |
| 123 | function parseParagraph(line, isInList) { |
| 124 | if (!isInList) { |
| 125 | // Not in a list, process as regular paragraph |
| 126 | return [processText(line, false), false]; |
| 68 | 127 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 128 | // Was in a list, close it and start paragraph |
| 129 | return [`</ul>${processText(line, false)}`, false]; |
| 70 | 130 | } |
| 71 | 131 | } |
| 72 | 132 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 133 | /** |
| 134 | * Parses a single line of markdown by trying different parsers in order |
| 135 | * Order: headers -> list items -> paragraphs |
| 136 | * @param {string} line - The markdown line to parse |
| 137 | * @param {boolean} isInList - Whether we're currently inside a list |
| 138 | * @returns {Array} [htmlContent, stillInList] |
| 139 | */ |
| 140 | function parseLine(line, isInList) { |
| 141 | // Try to parse as header first |
| 142 | let [htmlContent, stillInList] = parseHeader(line, isInList); |
| 143 | |
| 144 | // If not a header, try to parse as list item |
| 145 | if (htmlContent === null) { |
| 146 | [htmlContent, stillInList] = parseListItem(line, isInList); |
| 77 | 147 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 148 | |
| 149 | // If not a list item, parse as paragraph |
| 150 | if (htmlContent === null) { |
| 151 | [htmlContent, stillInList] = parseParagraph(line, isInList); |
| 80 | 152 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 153 | |
| 154 | // This should never happen as parseParagraph always returns a result |
| 155 | if (htmlContent === null) { |
| 156 | throw new Error('Unable to parse line: ' + line); |
| 83 | 157 | } |
| 84 | | return [result, inListAfter]; |
| 158 | |
| 159 | return [htmlContent, stillInList]; |
| 85 | 160 | } |
| 86 | 161 | |
| 87 | 162 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 163 | * Main markdown parser function |
| 164 | * Converts markdown text to HTML |
| 165 | * @param {string} markdown - The markdown text to parse |
| 166 | * @returns {string} The HTML representation |
| 90 | 167 | */ |
| 91 | 168 | export function parse(markdown) { |
| 92 | 169 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 170 | let htmlResult = ''; |
| 171 | let isInList = false; |
| 172 | |
| 173 | // Process each line |
| 95 | 174 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 175 | const [lineHtml, stillInList] = parseLine(lines[i], isInList); |
| 176 | htmlResult += lineHtml; |
| 177 | isInList = stillInList; |
| 99 | 178 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 179 | |
| 180 | // Close any open list at the end |
| 181 | if (isInList) { |
| 182 | return htmlResult + '</ul>'; |
| 102 | 183 | } else { |
| 103 | | return result; |
| 184 | return htmlResult; |
| 104 | 185 | } |
| 105 | 186 | } |