| 12 | 12 | return markdown.replace(pattern, replacement); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | | function parse__(markdown) { |
| 15 | // REFACTORED: Renamed parse__ to parseStrong for clarity |
| 16 | function parseStrong(markdown) { |
| 16 | 17 | return parser(markdown, '__', 'strong'); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | // REFACTORED: Renamed parse_ to parseEmphasis for clarity |
| 21 | function parseEmphasis(markdown) { |
| 20 | 22 | return parser(markdown, '_', 'em'); |
| 21 | 23 | } |
| 22 | 24 | |
| 25 | // REFACTORED: Simplified parseText function with clearer logic |
| 23 | 26 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 27 | // Apply emphasis and strong formatting in order |
| 28 | const parsedText = parseEmphasis(parseStrong(markdown)); |
| 29 | |
| 30 | // If we're inside a list, return the text as-is |
| 31 | // Otherwise, wrap it in a paragraph tag |
| 32 | return list ? parsedText : wrap(parsedText, 'p'); |
| 30 | 33 | } |
| 31 | 34 | |
| 35 | // REFACTORED: Improved readability of header parsing with clearer variable names and comments |
| 32 | 36 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 37 | // Count the number of consecutive '#' characters at the start of the line |
| 38 | let headerLevel = 0; |
| 34 | 39 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 40 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 41 | headerLevel += 1; |
| 37 | 42 | } else { |
| 38 | 43 | break; |
| 39 | 44 | } |
| 40 | 45 | } |
| 41 | | if (count === 0 || count > 6) { |
| 46 | |
| 47 | // Valid headers have 1-6 '#' characters |
| 48 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 49 | return [null, list]; |
| 43 | 50 | } |
| 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]; |
| 50 | | } |
| 51 | |
| 52 | // Create the appropriate header tag (h1-h6) |
| 53 | const headerTag = `h${headerLevel}`; |
| 54 | |
| 55 | // Extract the header text (skip the '#' characters and the following space) |
| 56 | const headerText = markdown.substring(headerLevel + 1); |
| 57 | const headerHtml = wrap(headerText, headerTag); |
| 58 | |
| 59 | // If we were in a list, close it before the header |
| 60 | // Otherwise, just return the header |
| 61 | return list ? [`</ul>${headerHtml}`, false] : [headerHtml, false]; |
| 51 | 62 | } |
| 52 | 63 | |
| 64 | // REFACTORED: Improved list item parsing with clearer logic and variable names |
| 53 | 65 | function parseLineItem(markdown, list) { |
| 66 | // Check if this line starts a list item |
| 54 | 67 | 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 | // Extract the list item content (skip '* ') |
| 69 | const listItemContent = markdown.substring(2); |
| 70 | |
| 71 | // Parse the content and wrap it in li tags |
| 72 | const listItemHtml = wrap(parseText(listItemContent, true), 'li'); |
| 73 | |
| 74 | // If we're already in a list, just add the item |
| 75 | // Otherwise, start a new list with this item |
| 76 | return list ? [listItemHtml, true] : [`<ul>${listItemHtml}`, true]; |
| 61 | 77 | } |
| 78 | |
| 79 | // Not a list item |
| 62 | 80 | return [null, list]; |
| 63 | 81 | } |
| 64 | 82 | |
| 83 | // REFACTORED: Simplified paragraph parsing with clearer conditional logic |
| 65 | 84 | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 85 | // Parse the text content |
| 86 | const parsedText = parseText(markdown, false); |
| 87 | |
| 88 | // If we were in a list, close it before starting the paragraph |
| 89 | // Otherwise, just return the parsed text |
| 90 | return list ? [`</ul>${parsedText}`, false] : [parsedText, false]; |
| 71 | 91 | } |
| 72 | 92 | |
| 93 | // REFACTORED: Simplified line parsing with explicit conditional logic instead of sequential null checks |
| 73 | 94 | function parseLine(markdown, list) { |
| 95 | // Try to parse as a header first |
| 74 | 96 | let [result, inListAfter] = parseHeader(markdown, list); |
| 97 | |
| 98 | // If it's not a header, try to parse as a list item |
| 75 | 99 | if (result === null) { |
| 76 | 100 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 101 | } |
| 102 | |
| 103 | // If it's neither a header nor a list item, parse as a paragraph |
| 78 | 104 | if (result === null) { |
| 79 | 105 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 106 | } |
| 107 | |
| 108 | // This should never happen with valid input |
| 81 | 109 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 110 | throw new Error('Unable to parse line: ' + markdown); |
| 83 | 111 | } |
| 112 | |
| 84 | 113 | return [result, inListAfter]; |
| 85 | 114 | } |
| 86 | 115 | |
| 87 | 116 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 117 | * REFACTORED: Main markdown parser with improved readability |
| 118 | * @param {string} markdown - The markdown string to parse |
| 119 | * @returns {string} - The resulting HTML |
| 90 | 120 | */ |
| 91 | 121 | export function parse(markdown) { |
| 122 | // Split markdown into individual lines |
| 92 | 123 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 124 | let htmlResult = ''; |
| 125 | let isInList = false; |
| 126 | |
| 127 | // Process each line sequentially |
| 95 | 128 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 99 | | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 129 | const currentLine = lines[i]; |
| 130 | |
| 131 | // Parse the current line and get the result plus list state |
| 132 | let [lineHtml, newInListState] = parseLine(currentLine, isInList); |
| 133 | |
| 134 | // Append the HTML for this line |
| 135 | htmlResult += lineHtml; |
| 136 | |
| 137 | // Update the list state for the next iteration |
| 138 | isInList = newInListState; |
| 104 | 139 | } |
| 140 | |
| 141 | // If we ended while still in a list, close it |
| 142 | return isInList ? htmlResult + '</ul>' : htmlResult; |
| 105 | 143 | } |