| 12 | 12 | return markdown.replace(pattern, replacement); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | | function parse__(markdown) { |
| 15 | // Parse bold text (surrounded by double underscores) |
| 16 | function parseBold(markdown) { |
| 16 | 17 | return parser(markdown, '__', 'strong'); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | // Parse italic text (surrounded by single underscores) |
| 21 | function parseItalic(markdown) { |
| 20 | 22 | return parser(markdown, '_', 'em'); |
| 21 | 23 | } |
| 22 | 24 | |
| 25 | // Parse inline formatting (bold and italic) and wrap in paragraph tags if not in a list |
| 23 | 26 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 27 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 28 | if (list) { |
| 26 | 29 | return parsedText; |
| 27 | 30 | } else { |
| 29 | 32 | } |
| 30 | 33 | } |
| 31 | 34 | |
| 35 | // Parse markdown headers (lines starting with # characters) |
| 36 | // Returns [html, inList] tuple where html is the generated HTML or null if not a header |
| 32 | 37 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 38 | // Count leading # characters to determine header level |
| 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 | // Not a header if no # found or too many # (max h6) |
| 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); |
| 52 | |
| 53 | const headerTag = `h${headerLevel}`; |
| 54 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 55 | const headerHtml = wrap(headerContent, headerTag); |
| 56 | |
| 57 | // Close any open list before adding header |
| 46 | 58 | if (list) { |
| 47 | 59 | return [`</ul>${headerHtml}`, false]; |
| 48 | 60 | } else { |
| 50 | 62 | } |
| 51 | 63 | } |
| 52 | 64 | |
| 65 | // Parse list items (lines starting with *) |
| 66 | // Returns [html, inList] tuple where html is the generated HTML or null if not a list item |
| 53 | 67 | function parseLineItem(markdown, list) { |
| 54 | 68 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 69 | // Remove the '* ' prefix and parse the remaining text as a list item |
| 70 | const itemContent = markdown.substring(2); |
| 71 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 72 | |
| 56 | 73 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 74 | // Already in a list, just add the list item |
| 75 | return [listItemHtml, true]; |
| 58 | 76 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 77 | // Starting a new list, wrap in ul tags |
| 78 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 79 | } |
| 61 | 80 | } |
| 62 | 81 | return [null, list]; |
| 63 | 82 | } |
| 64 | 83 | |
| 84 | // Parse regular paragraphs (fallback for lines that aren't headers or list items) |
| 85 | // Always closes any open list before creating a paragraph |
| 65 | 86 | function parseParagraph(markdown, list) { |
| 66 | 87 | if (!list) { |
| 67 | 88 | return [parseText(markdown, false), false]; |
| 70 | 91 | } |
| 71 | 92 | } |
| 72 | 93 | |
| 94 | // Parse a single line of markdown by trying different parsers in order |
| 95 | // Returns [html, inList] tuple with the generated HTML and whether we're in a list |
| 73 | 96 | function parseLine(markdown, list) { |
| 97 | // Try parsing as header first |
| 74 | 98 | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 99 | if (result !== null) { |
| 100 | return [result, inListAfter]; |
| 77 | 101 | } |
| 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'); |
| 102 | |
| 103 | // Try parsing as list item |
| 104 | [result, inListAfter] = parseLineItem(markdown, list); |
| 105 | if (result !== null) { |
| 106 | return [result, inListAfter]; |
| 83 | 107 | } |
| 108 | |
| 109 | // Fallback to paragraph parsing |
| 110 | [result, inListAfter] = parseParagraph(markdown, list); |
| 84 | 111 | return [result, inListAfter]; |
| 112 | |
| 113 | // Note: The original code had an error throw here, but it should never be reached |
| 114 | // since parseParagraph always returns a result. This has been removed for clarity. |
| 85 | 115 | } |
| 86 | 116 | |
| 87 | 117 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 118 | * Parse markdown text and convert it to HTML |
| 119 | * @param {string} markdown - The markdown text to parse |
| 120 | * @returns {string} - The resulting HTML |
| 90 | 121 | */ |
| 91 | 122 | export function parse(markdown) { |
| 92 | 123 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 124 | let html = ''; |
| 125 | let inList = false; |
| 126 | |
| 127 | // Process each line of the markdown text |
| 95 | 128 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 129 | const [lineHtml, newListState] = parseLine(lines[i], inList); |
| 130 | html += lineHtml; |
| 131 | inList = newListState; |
| 99 | 132 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 133 | |
| 134 | // Close any open list at the end of the document |
| 135 | if (inList) { |
| 136 | return html + '</ul>'; |
| 102 | 137 | } else { |
| 103 | | return result; |
| 138 | return html; |
| 104 | 139 | } |
| 105 | 140 | } |