| 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 = 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'); |
| 13 | // Parse italic text (single underscores) |
| 14 | function parseItalic(markdown) { |
| 15 | const pattern = new RegExp('_(.+)_'); |
| 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) { |
| 25 | // Parse text with appropriate wrapper tags |
| 26 | function parseText(markdown, inList) { |
| 27 | const parsedText = parseInlineFormatting(markdown); |
| 28 | if (inList) { |
| 26 | 29 | return parsedText; |
| 27 | 30 | } else { |
| 28 | 31 | return wrap(parsedText, 'p'); |
| 29 | 32 | } |
| 30 | 33 | } |
| 31 | 34 | |
| 32 | | function parseHeader(markdown, list) { |
| 35 | // Parse header elements (#, ##, ###, etc.) |
| 36 | function parseHeader(markdown, inList) { |
| 33 | 37 | let count = 0; |
| 34 | 38 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 39 | if (markdown[i] === '#') { |
| 38 | 42 | break; |
| 39 | 43 | } |
| 40 | 44 | } |
| 45 | |
| 46 | // Headers must be between 1 and 6 # characters |
| 41 | 47 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 48 | return [null, inList]; |
| 43 | 49 | } |
| 50 | |
| 44 | 51 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 52 | const headerContent = markdown.substring(count + 1); |
| 53 | const headerHtml = wrap(headerContent, headerTag); |
| 54 | |
| 55 | // If we were in a list, close it before the header |
| 56 | if (inList) { |
| 47 | 57 | return [`</ul>${headerHtml}`, false]; |
| 48 | 58 | } else { |
| 49 | 59 | return [headerHtml, false]; |
| 50 | 60 | } |
| 51 | 61 | } |
| 52 | 62 | |
| 53 | | function parseLineItem(markdown, list) { |
| 63 | // Parse list items (lines starting with *) |
| 64 | function parseListItem(markdown, inList) { |
| 54 | 65 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 66 | const itemContent = markdown.substring(2); // Skip "* " |
| 67 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 68 | |
| 69 | if (inList) { |
| 70 | // Continue existing list |
| 57 | 71 | return [innerHtml, true]; |
| 58 | 72 | } else { |
| 73 | // Start new list |
| 59 | 74 | return [`<ul>${innerHtml}`, true]; |
| 60 | 75 | } |
| 61 | 76 | } |
| 62 | | return [null, list]; |
| 77 | return [null, inList]; |
| 63 | 78 | } |
| 64 | 79 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 80 | // Parse paragraph elements (default case) |
| 81 | function parseParagraph(markdown, inList) { |
| 82 | if (inList) { |
| 83 | // If we were in a list, close it before the paragraph |
| 69 | 84 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 85 | } else { |
| 86 | return [parseText(markdown, false), false]; |
| 70 | 87 | } |
| 71 | 88 | } |
| 72 | 89 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 90 | // Parse a single line of markdown |
| 91 | function parseLine(markdown, inList) { |
| 92 | // Try to parse as header first |
| 93 | let [result, newList] = parseHeader(markdown, inList); |
| 94 | |
| 95 | // If not a header, try to parse as list item |
| 75 | 96 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 97 | [result, newList] = parseListItem(markdown, inList); |
| 77 | 98 | } |
| 99 | |
| 100 | // If not a list item, parse as paragraph |
| 78 | 101 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 102 | [result, newList] = parseParagraph(markdown, inList); |
| 80 | 103 | } |
| 104 | |
| 105 | // This should never happen with valid input |
| 81 | 106 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 107 | throw new Error('Unable to parse line: ' + markdown); |
| 83 | 108 | } |
| 84 | | return [result, inListAfter]; |
| 109 | |
| 110 | return [result, newList]; |
| 85 | 111 | } |
| 86 | 112 | |
| 87 | 113 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 114 | * Parse markdown text and convert to HTML |
| 115 | * @param {string} markdown - The markdown text to parse |
| 116 | * @returns {string} - The resulting HTML |
| 90 | 117 | */ |
| 91 | 118 | export function parse(markdown) { |
| 92 | 119 | const lines = markdown.split('\n'); |
| 93 | 120 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 121 | let inList = false; |
| 122 | |
| 123 | for (const line of lines) { |
| 124 | let [lineResult, newList] = parseLine(line, inList); |
| 97 | 125 | result += lineResult; |
| 98 | | list = newList; |
| 126 | inList = newList; |
| 99 | 127 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 128 | |
| 129 | // Close any open list at the end |
| 130 | if (inList) { |
| 131 | result += '</ul>'; |
| 104 | 132 | } |
| 133 | |
| 134 | return result; |
| 105 | 135 | } |