| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 10 | // Escape special regex characters in delimiter |
| 11 | const escapedDelimiter = delimiter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 12 | const pattern = new RegExp(`${escapedDelimiter}(.+)${escapedDelimiter}`); |
| 11 | 13 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 14 | return markdown.replace(pattern, replacement); |
| 13 | 15 | } |
| 14 | 16 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | // Refactored: Renamed parse__ to parseBold for clarity (using correct markdown syntax) |
| 18 | function parseBold(markdown) { |
| 19 | return parser(markdown, '**', 'strong'); |
| 17 | 20 | } |
| 18 | 21 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 22 | // Refactored: Renamed parse_ to parseItalic for clarity (using correct markdown syntax) |
| 23 | function parseItalic(markdown) { |
| 24 | return parser(markdown, '*', 'em'); |
| 21 | 25 | } |
| 22 | 26 | |
| 27 | // Refactored: Updated to use descriptive function names |
| 23 | 28 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 29 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 30 | if (list) { |
| 26 | 31 | return parsedText; |
| 27 | 32 | } else { |
| 29 | 34 | } |
| 30 | 35 | } |
| 31 | 36 | |
| 37 | // Refactored: Improved header parsing with clearer logic and comments |
| 32 | 38 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 39 | // Count consecutive # characters at the start |
| 40 | let headerLevel = 0; |
| 34 | 41 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 42 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 43 | headerLevel += 1; |
| 37 | 44 | } else { |
| 38 | 45 | break; |
| 39 | 46 | } |
| 40 | 47 | } |
| 41 | | if (count === 0 || count > 6) { |
| 48 | |
| 49 | // Valid headers have 1-6 # characters |
| 50 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 51 | return [null, list]; |
| 43 | 52 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 53 | |
| 54 | const headerTag = `h${headerLevel}`; |
| 55 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 56 | const headerHtml = wrap(headerContent, headerTag); |
| 57 | |
| 58 | // Close any open list before adding header |
| 46 | 59 | if (list) { |
| 47 | 60 | return [`</ul>${headerHtml}`, false]; |
| 48 | 61 | } else { |
| 50 | 63 | } |
| 51 | 64 | } |
| 52 | 65 | |
| 66 | // Refactored: Improved list item parsing with clearer logic |
| 53 | 67 | function parseLineItem(markdown, list) { |
| 54 | 68 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 69 | const listItemContent = markdown.substring(2).trim(); |
| 70 | const parsedContent = parseText(listItemContent, true); // true indicates we're in a list |
| 71 | const listItemHtml = wrap(parsedContent, 'li'); |
| 72 | |
| 56 | 73 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 74 | // Continue existing list |
| 75 | return [listItemHtml, true]; |
| 58 | 76 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 77 | // Start new list |
| 78 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 79 | } |
| 61 | 80 | } |
| 62 | 81 | return [null, list]; |
| 63 | 82 | } |
| 64 | 83 | |
| 84 | // Refactored: Improved paragraph parsing with clearer logic |
| 65 | 85 | function parseParagraph(markdown, list) { |
| 66 | 86 | if (!list) { |
| 87 | // No open list, create paragraph normally |
| 67 | 88 | return [parseText(markdown, false), false]; |
| 68 | 89 | } else { |
| 90 | // Close open list before creating paragraph |
| 69 | 91 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 92 | } |
| 71 | 93 | } |
| 72 | 94 | |
| 95 | // Refactored: Improved parsing logic with clearer control flow |
| 73 | 96 | function parseLine(markdown, list) { |
| 97 | // Try parsing as header first |
| 74 | 98 | let [result, inListAfter] = parseHeader(markdown, list); |
| 99 | |
| 100 | // If not a header, try parsing as list item |
| 75 | 101 | if (result === null) { |
| 76 | 102 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 103 | } |
| 104 | |
| 105 | // If neither header nor list item, treat as paragraph |
| 78 | 106 | if (result === null) { |
| 79 | 107 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 108 | } |
| 109 | |
| 110 | // This should never happen with valid input |
| 81 | 111 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 112 | throw new Error('Invalid markdown line'); |
| 83 | 113 | } |
| 114 | |
| 84 | 115 | return [result, inListAfter]; |
| 85 | 116 | } |
| 86 | 117 | |
| 87 | 118 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 119 | * Parses markdown text and converts it to HTML |
| 120 | * @param {string} markdown - The markdown text to parse |
| 121 | * @returns {string} - The resulting HTML |
| 90 | 122 | */ |
| 123 | // Refactored: Improved main parsing function with clearer logic |
| 91 | 124 | export function parse(markdown) { |
| 92 | 125 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 126 | let htmlResult = ''; |
| 127 | let isInList = false; |
| 128 | |
| 129 | // Process each line of markdown |
| 95 | 130 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 131 | const [lineHtml, newInListState] = parseLine(lines[i], isInList); |
| 132 | htmlResult += lineHtml; |
| 133 | isInList = newInListState; |
| 99 | 134 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 135 | |
| 136 | // Close any unclosed list at the end |
| 137 | if (isInList) { |
| 138 | return htmlResult + '</ul>'; |
| 102 | 139 | } else { |
| 103 | | return result; |
| 140 | return htmlResult; |
| 104 | 141 | } |
| 105 | 142 | } |