| 1 | /** |
| 2 | * Wraps text with HTML tags |
| 3 | * @param {string} text - Text to wrap |
| 4 | * @param {string} tag - HTML tag to use |
| 5 | * @returns {string} - Text wrapped in HTML tags |
| 6 | */ |
| 1 | 7 | function wrap(text, tag) { |
| 2 | 8 | return `<${tag}>${text}</${tag}>`; |
| 3 | 9 | } |
| 4 | 10 | |
| 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}>`; |
| 11 | /** |
| 12 | * Parses bold text (__) to <strong> tags |
| 13 | * @param {string} markdown - Markdown text to parse |
| 14 | * @returns {string} - HTML with bold text |
| 15 | */ |
| 16 | function parseBold(markdown) { |
| 17 | const pattern = new RegExp(`__([^_]+)__`); |
| 18 | const replacement = `<strong>$1</strong>`; |
| 12 | 19 | return markdown.replace(pattern, replacement); |
| 13 | 20 | } |
| 14 | 21 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 22 | /** |
| 23 | * Parses italic text (_) to <em> tags |
| 24 | * @param {string} markdown - Markdown text to parse |
| 25 | * @returns {string} - HTML with italic text |
| 26 | */ |
| 27 | function parseItalic(markdown) { |
| 28 | // Use negative lookahead/lookbehind to avoid matching underscores inside bold markers |
| 29 | const pattern = new RegExp(`_([^_]+)_`); |
| 30 | const replacement = `<em>$1</em>`; |
| 31 | return markdown.replace(pattern, replacement); |
| 21 | 32 | } |
| 22 | 33 | |
| 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 | | } |
| 34 | /** |
| 35 | * Parses inline formatting (__ for bold, _ for italic) |
| 36 | * @param {string} markdown - Markdown text to parse |
| 37 | * @returns {string} - HTML with inline formatting |
| 38 | */ |
| 39 | function parseInlineFormatting(markdown) { |
| 40 | // Parse bold first, then italic to handle nested formatting correctly |
| 41 | return parseItalic(parseBold(markdown)); |
| 30 | 42 | } |
| 31 | 43 | |
| 32 | | function parseHeader(markdown, list) { |
| 44 | /** |
| 45 | * Parses header markdown (# ## ### etc.) to HTML headers |
| 46 | * @param {string} markdown - Markdown line to parse |
| 47 | * @returns {string|null} - HTML header or null if not a header |
| 48 | */ |
| 49 | function parseHeader(markdown) { |
| 33 | 50 | let count = 0; |
| 34 | 51 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 52 | if (markdown[i] === '#') { |
| 38 | 55 | break; |
| 39 | 56 | } |
| 40 | 57 | } |
| 58 | |
| 59 | // Valid headers have 1-6 # characters |
| 41 | 60 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 61 | return null; |
| 43 | 62 | } |
| 63 | |
| 44 | 64 | 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 | | } |
| 65 | const headerContent = markdown.substring(count + 1); |
| 66 | return wrap(parseInlineFormatting(headerContent), headerTag); |
| 51 | 67 | } |
| 52 | 68 | |
| 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 | | } |
| 69 | /** |
| 70 | * Parses a list item (* item) to HTML list item |
| 71 | * @param {string} markdown - Markdown line to parse |
| 72 | * @returns {string|null} - HTML list item or null if not a list item |
| 73 | */ |
| 74 | function parseListItem(markdown) { |
| 75 | if (markdown.startsWith('* ')) { |
| 76 | const itemContent = markdown.substring(2); |
| 77 | return wrap(parseInlineFormatting(itemContent), 'li'); |
| 61 | 78 | } |
| 62 | | return [null, list]; |
| 79 | return null; |
| 63 | 80 | } |
| 64 | 81 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 82 | /** |
| 83 | * Parses a paragraph to HTML paragraph |
| 84 | * @param {string} markdown - Markdown line to parse |
| 85 | * @returns {string} - HTML paragraph |
| 86 | */ |
| 87 | function parseParagraph(markdown) { |
| 88 | return wrap(parseInlineFormatting(markdown), 'p'); |
| 71 | 89 | } |
| 72 | 90 | |
| 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); |
| 91 | /** |
| 92 | * Parses a single line of markdown based on its type |
| 93 | * @param {string} line - Line of markdown to parse |
| 94 | * @param {boolean} inList - Whether we're currently in a list |
| 95 | * @returns {[string, boolean]} - [HTML result, whether we're now in a list] |
| 96 | */ |
| 97 | function parseLine(line, inList) { |
| 98 | // Try to parse as header |
| 99 | const headerResult = parseHeader(line); |
| 100 | if (headerResult !== null) { |
| 101 | // If we were in a list, close it first |
| 102 | const result = inList ? `</ul>${headerResult}` : headerResult; |
| 103 | return [result, false]; // Headers end lists |
| 80 | 104 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 105 | |
| 106 | // Try to parse as list item |
| 107 | const listItemResult = parseListItem(line); |
| 108 | if (listItemResult !== null) { |
| 109 | // If we weren't in a list, start one |
| 110 | if (!inList) { |
| 111 | return [`<ul>${listItemResult}`, true]; |
| 112 | } else { |
| 113 | return [listItemResult, true]; |
| 114 | } |
| 83 | 115 | } |
| 84 | | return [result, inListAfter]; |
| 116 | |
| 117 | // Parse as paragraph |
| 118 | const paragraphResult = parseParagraph(line); |
| 119 | // If we were in a list, close it first |
| 120 | const result = inList ? `</ul>${paragraphResult}` : paragraphResult; |
| 121 | return [result, false]; // Paragraphs end lists |
| 85 | 122 | } |
| 86 | 123 | |
| 87 | 124 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 125 | * Main function to parse markdown text to HTML |
| 126 | * @param {string} markdown - Markdown text to parse |
| 127 | * @returns {string} - HTML representation of the markdown |
| 90 | 128 | */ |
| 91 | 129 | export function parse(markdown) { |
| 92 | 130 | const lines = markdown.split('\n'); |
| 93 | 131 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 132 | let inList = false; |
| 133 | |
| 134 | for (const line of lines) { |
| 135 | const [lineResult, newInList] = parseLine(line, inList); |
| 97 | 136 | result += lineResult; |
| 98 | | list = newList; |
| 137 | inList = newInList; |
| 99 | 138 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 139 | |
| 140 | // Close any open list at the end |
| 141 | if (inList) { |
| 142 | result += '</ul>'; |
| 104 | 143 | } |
| 144 | |
| 145 | return result; |
| 105 | 146 | } |