| 1 | // Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 6 | // Parse bold text (double underscores) |
| 7 | function parseBold(markdown) { |
| 8 | const pattern = /__(.+)__/g; |
| 9 | const replacement = '<strong>$1</strong>'; |
| 12 | 10 | return markdown.replace(pattern, replacement); |
| 13 | 11 | } |
| 14 | 12 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 13 | // Parse italic text (single underscores) |
| 14 | function parseItalic(markdown) { |
| 15 | const pattern = /_(.+)_/g; |
| 16 | const replacement = '<em>$1</em>'; |
| 17 | return markdown.replace(pattern, replacement); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 20 | // Parse inline formatting (bold and italic) |
| 21 | function parseInlineFormatting(markdown) { |
| 22 | return parseItalic(parseBold(markdown)); |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 25 | // Parse text with appropriate wrapper tags |
| 26 | function parseText(markdown, isListItem = false) { |
| 27 | const parsedText = parseInlineFormatting(markdown); |
| 28 | // List items don't get wrapped in <p> tags |
| 29 | return isListItem ? parsedText : wrap(parsedText, 'p'); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 32 | // Parse header markdown (#, ##, ###, etc.) |
| 33 | function parseHeader(markdown, currentlyInList) { |
| 34 | // Count consecutive # characters at the beginning |
| 35 | let headerLevel = 0; |
| 34 | 36 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 37 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 38 | headerLevel++; |
| 37 | 39 | } else { |
| 38 | 40 | break; |
| 39 | 41 | } |
| 40 | 42 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 43 | |
| 44 | // Valid headers are h1-h6 (1-6 # characters) |
| 45 | if (headerLevel === 0 || headerLevel > 6) { |
| 46 | return [null, currentlyInList]; |
| 43 | 47 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 48 | |
| 49 | // Extract header content (skip # characters and the following space) |
| 50 | const headerContent = markdown.substring(headerLevel + 1); |
| 51 | const headerTag = `h${headerLevel}`; |
| 52 | const headerHtml = wrap(headerContent, headerTag); |
| 53 | |
| 54 | // If we were in a list, close it before the header |
| 55 | if (currentlyInList) { |
| 47 | 56 | return [`</ul>${headerHtml}`, false]; |
| 48 | 57 | } else { |
| 49 | 58 | return [headerHtml, false]; |
| 50 | 59 | } |
| 51 | 60 | } |
| 52 | 61 | |
| 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]; |
| 58 | | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 60 | | } |
| 62 | // Parse list items (lines starting with *) |
| 63 | function parseListItem(markdown, currentlyInList) { |
| 64 | if (!markdown.startsWith('*')) { |
| 65 | return [null, currentlyInList]; |
| 66 | } |
| 67 | |
| 68 | // Extract list item content (skip '* ' prefix) |
| 69 | const itemContent = markdown.substring(2); |
| 70 | const itemHtml = wrap(parseText(itemContent, true), 'li'); |
| 71 | |
| 72 | // If we're already in a list, just add the item |
| 73 | // Otherwise, start a new list |
| 74 | if (currentlyInList) { |
| 75 | return [itemHtml, true]; |
| 76 | } else { |
| 77 | return [`<ul>${itemHtml}`, true]; |
| 61 | 78 | } |
| 62 | | return [null, list]; |
| 63 | 79 | } |
| 64 | 80 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 81 | // Parse paragraph (default case when no other syntax matches) |
| 82 | function parseParagraph(markdown, currentlyInList) { |
| 83 | const paragraphHtml = parseText(markdown, false); |
| 84 | |
| 85 | // If we were in a list, close it before the paragraph |
| 86 | if (currentlyInList) { |
| 87 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 88 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 89 | return [paragraphHtml, false]; |
| 70 | 90 | } |
| 71 | 91 | } |
| 72 | 92 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 93 | // Parse a single line of markdown |
| 94 | function parseLine(markdown, currentlyInList) { |
| 95 | // Try to parse as header first |
| 96 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 97 | if (result !== null) { |
| 98 | return [result, newListState]; |
| 77 | 99 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 100 | |
| 101 | // Try to parse as list item |
| 102 | [result, newListState] = parseListItem(markdown, currentlyInList); |
| 103 | if (result !== null) { |
| 104 | return [result, newListState]; |
| 80 | 105 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 106 | |
| 107 | // Default to paragraph |
| 108 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 109 | if (result !== null) { |
| 110 | return [result, newListState]; |
| 83 | 111 | } |
| 84 | | return [result, inListAfter]; |
| 112 | |
| 113 | // This should never happen with valid input |
| 114 | throw new Error('Unable to parse line'); |
| 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; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 124 | let htmlResult = ''; |
| 125 | let inList = false; |
| 126 | |
| 127 | // Process each line |
| 128 | for (const line of lines) { |
| 129 | const [lineHtml, newListState] = parseLine(line, inList); |
| 130 | htmlResult += lineHtml; |
| 131 | inList = newListState; |
| 99 | 132 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 133 | |
| 134 | // Close any open list at the end |
| 135 | if (inList) { |
| 136 | htmlResult += '</ul>'; |
| 104 | 137 | } |
| 138 | |
| 139 | return htmlResult; |
| 105 | 140 | } |