| 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 | |
| 6 | // Helper function to check if text starts with a specific tag |
| 5 | 7 | function isTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 11 | // Generic parser for inline formatting elements |
| 9 | 12 | function parser(markdown, delimiter, tag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 14 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 18 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 19 | function parseBold(markdown) { |
| 16 | 20 | return parser(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 23 | // Parse italic text (_text_ -> <em>text</em>) |
| 24 | function parseItalic(markdown) { |
| 20 | 25 | return parser(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 28 | // Parse inline formatting (bold and italic) |
| 29 | function parseInlineFormatting(markdown) { |
| 30 | return parseItalic(parseBold(markdown)); |
| 31 | } |
| 32 | |
| 33 | // Parse text with appropriate wrapper tags |
| 34 | function parseText(markdown, isListItem = false) { |
| 35 | const parsedText = parseInlineFormatting(markdown); |
| 36 | if (isListItem) { |
| 26 | 37 | return parsedText; |
| 27 | 38 | } else { |
| 28 | 39 | return wrap(parsedText, 'p'); |
| 29 | 40 | } |
| 30 | 41 | } |
| 31 | 42 | |
| 32 | | function parseHeader(markdown, list) { |
| 43 | // Parse header elements (# Header -> <h1>Header</h1>) |
| 44 | function parseHeader(markdown, currentlyInList) { |
| 33 | 45 | let count = 0; |
| 34 | 46 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 47 | if (markdown[i] === '#') { |
| 38 | 50 | break; |
| 39 | 51 | } |
| 40 | 52 | } |
| 53 | |
| 54 | // Headers must be between 1 and 6 # characters |
| 41 | 55 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 56 | return [null, currentlyInList]; |
| 43 | 57 | } |
| 58 | |
| 44 | 59 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 60 | const headerContent = markdown.substring(count + 1); |
| 61 | const headerHtml = wrap(headerContent, headerTag); |
| 62 | |
| 63 | // If we were in a list, close it before the header |
| 64 | if (currentlyInList) { |
| 47 | 65 | return [`</ul>${headerHtml}`, false]; |
| 48 | 66 | } else { |
| 49 | 67 | return [headerHtml, false]; |
| 50 | 68 | } |
| 51 | 69 | } |
| 52 | 70 | |
| 53 | | function parseLineItem(markdown, list) { |
| 71 | // Parse list items (* item -> <ul><li>item</li></ul>) |
| 72 | function parseLineItem(markdown, currentlyInList) { |
| 54 | 73 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 74 | const itemContent = markdown.substring(2); // Skip '* ' prefix |
| 75 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 76 | |
| 77 | if (currentlyInList) { |
| 78 | // Continue existing list |
| 57 | 79 | return [innerHtml, true]; |
| 58 | 80 | } else { |
| 81 | // Start new list |
| 59 | 82 | return [`<ul>${innerHtml}`, true]; |
| 60 | 83 | } |
| 61 | 84 | } |
| 62 | | return [null, list]; |
| 85 | return [null, currentlyInList]; |
| 63 | 86 | } |
| 64 | 87 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 88 | // Parse paragraph elements |
| 89 | function parseParagraph(markdown, currentlyInList) { |
| 90 | if (currentlyInList) { |
| 91 | // Close list before starting paragraph |
| 69 | 92 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 93 | } else { |
| 94 | return [parseText(markdown, false), false]; |
| 70 | 95 | } |
| 71 | 96 | } |
| 72 | 97 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 98 | // Parse a single line of markdown |
| 99 | function parseLine(markdown, currentlyInList) { |
| 100 | // Try to parse as header |
| 101 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 102 | |
| 103 | // If not a header, try to parse as list item |
| 75 | 104 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 105 | [result, newListState] = parseLineItem(markdown, currentlyInList); |
| 77 | 106 | } |
| 107 | |
| 108 | // If not a list item, parse as paragraph |
| 78 | 109 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 110 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 111 | } |
| 112 | |
| 113 | // This should never happen with valid input |
| 81 | 114 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 115 | throw new Error('Unable to parse line: ' + markdown); |
| 83 | 116 | } |
| 84 | | return [result, inListAfter]; |
| 117 | |
| 118 | return [result, newListState]; |
| 85 | 119 | } |
| 86 | 120 | |
| 87 | 121 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 122 | * Parse markdown text and convert it to HTML |
| 123 | * @param {string} markdown - The markdown text to parse |
| 124 | * @returns {string} - The resulting HTML |
| 90 | 125 | */ |
| 91 | 126 | export function parse(markdown) { |
| 92 | 127 | 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; |
| 128 | let htmlResult = ''; |
| 129 | let inList = false; |
| 130 | |
| 131 | for (const line of lines) { |
| 132 | let [lineHtml, newListState] = parseLine(line, inList); |
| 133 | htmlResult += lineHtml; |
| 134 | inList = newListState; |
| 99 | 135 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 136 | |
| 137 | // Close any open list at the end |
| 138 | if (inList) { |
| 139 | htmlResult += '</ul>'; |
| 104 | 140 | } |
| 141 | |
| 142 | return htmlResult; |
| 105 | 143 | } |