| 29 | 33 | } |
| 30 | 34 | } |
| 31 | 35 | |
| 36 | // Refactored: Improved parseHeader function with clearer logic and comments |
| 32 | 37 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 38 | // Count consecutive # characters at the start of the line |
| 39 | let headerLevel = 0; |
| 34 | 40 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 41 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 42 | headerLevel += 1; |
| 37 | 43 | } else { |
| 38 | 44 | break; |
| 39 | 45 | } |
| 40 | 46 | } |
| 41 | | if (count === 0 || count > 6) { |
| 47 | |
| 48 | // Valid headers must have 1-6 # characters |
| 49 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 50 | return [null, list]; |
| 43 | 51 | } |
| 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 | | } |
| 52 | |
| 53 | // Extract header content (skip the space after #) |
| 54 | const headerContent = markdown.substring(headerLevel + 1); |
| 55 | const headerTag = `h${headerLevel}`; |
| 56 | const headerHtml = wrap(headerContent, headerTag); |
| 57 | |
| 58 | // Close any open list when transitioning to header |
| 59 | const inListAfter = false; |
| 60 | const resultHtml = list ? `</ul>${headerHtml}` : headerHtml; |
| 61 | |
| 62 | return [resultHtml, inListAfter]; |
| 51 | 63 | } |
| 52 | 64 | |
| 65 | // Refactored: Improved parseLineItem function with clearer logic and comments |
| 53 | 66 | function parseLineItem(markdown, list) { |
| 67 | // Check if this line starts a list item |
| 54 | 68 | 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 | | } |
| 69 | // Extract the list item content (skip '* ') |
| 70 | const itemContent = markdown.substring(2); |
| 71 | // Parse the content and wrap it in li tags |
| 72 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 73 | |
| 74 | // If we're already in a list, just add the item |
| 75 | // Otherwise, start a new list |
| 76 | const inListAfter = true; |
| 77 | const resultHtml = list ? listItemHtml : `<ul>${listItemHtml}`; |
| 78 | |
| 79 | return [resultHtml, inListAfter]; |
| 61 | 80 | } |
| 81 | |
| 82 | // Not a list item |
| 62 | 83 | return [null, list]; |
| 63 | 84 | } |
| 64 | 85 | |
| 86 | // Refactored: Improved parseParagraph function with clearer logic and comments |
| 65 | 87 | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 88 | // Parse the text as a paragraph |
| 89 | const paragraphHtml = parseText(markdown, false); |
| 90 | |
| 91 | // If we're in a list, close it first |
| 92 | // Otherwise, just return the paragraph |
| 93 | const inListAfter = false; |
| 94 | const resultHtml = list ? `</ul>${paragraphHtml}` : paragraphHtml; |
| 95 | |
| 96 | return [resultHtml, inListAfter]; |
| 71 | 97 | } |
| 72 | 98 | |
| 99 | // Refactored: Simplified parseLine function with explicit parsing order |
| 73 | 100 | function parseLine(markdown, list) { |
| 101 | // Try parsing as header first |
| 74 | 102 | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 103 | if (result !== null) { |
| 104 | return [result, inListAfter]; |
| 77 | 105 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 106 | |
| 107 | // Try parsing as list item |
| 108 | [result, inListAfter] = parseLineItem(markdown, list); |
| 109 | if (result !== null) { |
| 110 | return [result, inListAfter]; |
| 83 | 111 | } |
| 112 | |
| 113 | // Default to paragraph parsing |
| 114 | [result, inListAfter] = parseParagraph(markdown, list); |
| 84 | 115 | return [result, inListAfter]; |
| 85 | 116 | } |
| 86 | 117 | |
| 87 | 118 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 119 | * Parse Markdown text and convert it to HTML |
| 120 | * @param {string} markdown - The Markdown text to parse |
| 121 | * @returns {string} - The resulting HTML |
| 90 | 122 | */ |
| 91 | 123 | export function parse(markdown) { |
| 124 | // Split the markdown into individual lines |
| 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 individually |
| 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, newListState] = parseLine(lines[i], isInList); |
| 132 | htmlResult += lineHtml; |
| 133 | isInList = newListState; |
| 99 | 134 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 135 | |
| 136 | // Close any open list at the end of the document |
| 137 | if (isInList) { |
| 138 | return htmlResult + '</ul>'; |
| 102 | 139 | } else { |
| 103 | | return result; |
| 140 | return htmlResult; |
| 104 | 141 | } |
| 105 | 142 | } |