| 1 | // Helper functions for wrapping 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}>`); |
| 6 | // Parser for inline formatting (__ for bold, _ for italic) |
| 7 | function parseInlineFormatting(markdown) { |
| 8 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 9 | // Use non-greedy matching to handle multiple bold segments |
| 10 | let result = markdown.replace(/__(.+?)__/g, '<strong>$1</strong>'); |
| 11 | // Parse italic text (_text_ -> <em>text</em>) |
| 12 | // Use non-greedy matching and avoid matching within bold text |
| 13 | result = result.replace(/_(.+?)_/g, '<em>$1</em>'); |
| 14 | return result; |
| 7 | 15 | } |
| 8 | 16 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 13 | | } |
| 14 | | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 21 | | } |
| 22 | | |
| 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 | | } |
| 30 | | } |
| 31 | | |
| 32 | | function parseHeader(markdown, list) { |
| 17 | // Parser for different block elements |
| 18 | function parseHeader(markdown) { |
| 19 | // Count leading # characters |
| 33 | 20 | let count = 0; |
| 34 | 21 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 22 | if (markdown[i] === '#') { |
| 38 | 25 | break; |
| 39 | 26 | } |
| 40 | 27 | } |
| 28 | |
| 29 | // Valid headers have 1-6 # characters |
| 41 | 30 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 31 | return null; |
| 43 | 32 | } |
| 33 | |
| 44 | 34 | 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 | | } |
| 35 | const headerContent = markdown.substring(count + 1); |
| 36 | return wrap(headerContent, headerTag); |
| 51 | 37 | } |
| 52 | 38 | |
| 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 | | } |
| 39 | function parseListItem(markdown) { |
| 40 | if (markdown.startsWith('* ')) { |
| 41 | const itemContent = markdown.substring(2); |
| 42 | const formattedContent = parseInlineFormatting(itemContent); |
| 43 | return wrap(formattedContent, 'li'); |
| 61 | 44 | } |
| 62 | | return [null, list]; |
| 45 | return null; |
| 63 | 46 | } |
| 64 | 47 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 48 | function parseParagraph(markdown) { |
| 49 | const formattedContent = parseInlineFormatting(markdown); |
| 50 | return wrap(formattedContent, 'p'); |
| 71 | 51 | } |
| 72 | 52 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 53 | // Main parsing logic |
| 54 | function parseBlockElement(line, inList) { |
| 55 | // Try to parse as header |
| 56 | let result = parseHeader(line); |
| 57 | if (result !== null) { |
| 58 | // If we were in a list, close it before the header |
| 59 | if (inList) { |
| 60 | return { content: `</ul>${result}`, inList: false }; |
| 61 | } |
| 62 | return { content: result, inList: false }; |
| 77 | 63 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 64 | |
| 65 | // Try to parse as list item |
| 66 | result = parseListItem(line); |
| 67 | if (result !== null) { |
| 68 | if (inList) { |
| 69 | // Already in a list, just add the item |
| 70 | return { content: result, inList: true }; |
| 71 | } else { |
| 72 | // Starting a new list |
| 73 | return { content: `<ul>${result}`, inList: true }; |
| 74 | } |
| 80 | 75 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 76 | |
| 77 | // Parse as paragraph |
| 78 | result = parseParagraph(line); |
| 79 | // If we were in a list, close it before the paragraph |
| 80 | if (inList) { |
| 81 | return { content: `</ul>${result}`, inList: false }; |
| 83 | 82 | } |
| 84 | | return [result, inListAfter]; |
| 83 | return { content: result, inList: false }; |
| 85 | 84 | } |
| 86 | 85 | |
| 87 | 86 | /** |