| 1 | // Helper functions for HTML tag wrapping |
| 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 | // Text formatting parsers |
| 7 | function parseBold(markdown) { |
| 8 | const pattern = new RegExp(`__(.+)__`); |
| 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'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 13 | function parseItalic(markdown) { |
| 14 | const pattern = new RegExp(`_(.+)_`); |
| 15 | const replacement = `<em>$1</em>`; |
| 16 | return markdown.replace(pattern, replacement); |
| 21 | 17 | } |
| 22 | 18 | |
| 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 | | } |
| 19 | function parseTextFormatting(markdown) { |
| 20 | // Apply bold formatting first, then italic |
| 21 | return parseItalic(parseBold(markdown)); |
| 30 | 22 | } |
| 31 | 23 | |
| 32 | | function parseHeader(markdown, list) { |
| 24 | // Line type parsers |
| 25 | function parseHeaderLine(line) { |
| 26 | // Count consecutive # characters at the start of the line |
| 33 | 27 | let count = 0; |
| 34 | | for (let i = 0; i < markdown.length; i++) { |
| 35 | | if (markdown[i] === '#') { |
| 28 | for (let i = 0; i < line.length; i++) { |
| 29 | if (line[i] === '#') { |
| 36 | 30 | count += 1; |
| 37 | 31 | } else { |
| 38 | 32 | break; |
| 39 | 33 | } |
| 40 | 34 | } |
| 35 | |
| 36 | // Valid headers have 1-6 # characters |
| 41 | 37 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 38 | return null; |
| 43 | 39 | } |
| 40 | |
| 44 | 41 | 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 | | } |
| 42 | // Take everything after the # characters (including any space) |
| 43 | const content = line.substring(count + 1); |
| 44 | return wrap(content, headerTag); |
| 51 | 45 | } |
| 52 | 46 | |
| 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 | | } |
| 47 | function parseListItemLine(line) { |
| 48 | // List items start with '* ' (asterisk followed by space) |
| 49 | if (line.startsWith('* ')) { |
| 50 | const content = line.substring(2); |
| 51 | const formattedContent = parseTextFormatting(content); |
| 52 | return wrap(formattedContent, 'li'); |
| 61 | 53 | } |
| 62 | | return [null, list]; |
| 54 | return null; |
| 63 | 55 | } |
| 64 | 56 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 57 | function parseParagraphLine(line) { |
| 58 | // Everything else is treated as a paragraph |
| 59 | const formattedContent = parseTextFormatting(line); |
| 60 | return wrap(formattedContent, 'p'); |
| 71 | 61 | } |
| 72 | 62 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 63 | // Main line parser that determines the type of line and processes it accordingly |
| 64 | function parseLine(line, inList) { |
| 65 | // Try to parse as header |
| 66 | let result = parseHeaderLine(line); |
| 67 | if (result !== null) { |
| 68 | // If we were in a list, close it before the header |
| 69 | return [inList ? `</ul>${result}` : result, false]; |
| 80 | 70 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 71 | |
| 72 | // Try to parse as list item |
| 73 | result = parseListItemLine(line); |
| 74 | if (result !== null) { |
| 75 | // If we weren't in a list, start one; otherwise continue the existing list |
| 76 | return [inList ? result : `<ul>${result}`, true]; |
| 83 | 77 | } |
| 84 | | return [result, inListAfter]; |
| 78 | |
| 79 | // Parse as paragraph (default case) |
| 80 | result = parseParagraphLine(line); |
| 81 | // If we were in a list, close it before the paragraph |
| 82 | return [inList ? `</ul>${result}` : result, false]; |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | 85 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 86 | * Parse Markdown text and convert it to HTML |
| 87 | * @param {string} markdown - The Markdown text to parse |
| 88 | * @returns {string} - The resulting HTML |
| 90 | 89 | */ |
| 91 | 90 | export function parse(markdown) { |
| 91 | // Handle empty input |
| 92 | if (markdown === '') { |
| 93 | return '<p></p>'; |
| 94 | } |
| 95 | |
| 92 | 96 | const lines = markdown.split('\n'); |
| 93 | 97 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 98 | let inList = false; |
| 99 | |
| 100 | for (const line of lines) { |
| 101 | let [lineResult, newListState] = parseLine(line, inList); |
| 97 | 102 | result += lineResult; |
| 98 | | list = newList; |
| 103 | inList = newListState; |
| 99 | 104 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 105 | |
| 106 | // Close any open list at the end |
| 107 | if (inList) { |
| 108 | result += '</ul>'; |
| 104 | 109 | } |
| 110 | |
| 111 | return result; |
| 105 | 112 | } |