| 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 (bold, italic) |
| 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 content, optionally wrapping in paragraph 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) { |
| 33 | | let count = 0; |
| 43 | // Parse header markdown (# Header -> <h1>Header</h1>) |
| 44 | function parseHeader(markdown, currentlyInList) { |
| 45 | // Count consecutive # characters at the beginning |
| 46 | let hashCount = 0; |
| 34 | 47 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 48 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 49 | hashCount += 1; |
| 37 | 50 | } else { |
| 38 | 51 | break; |
| 39 | 52 | } |
| 40 | 53 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 54 | |
| 55 | // Headers must have 1-6 # characters |
| 56 | if (hashCount === 0 || hashCount > 6) { |
| 57 | return [null, currentlyInList]; |
| 43 | 58 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 59 | |
| 60 | const headerTag = `h${hashCount}`; |
| 61 | const headerContent = markdown.substring(hashCount + 1); |
| 62 | const headerHtml = wrap(headerContent, headerTag); |
| 63 | |
| 64 | // If we were in a list, close it before the header |
| 65 | if (currentlyInList) { |
| 47 | 66 | return [`</ul>${headerHtml}`, false]; |
| 48 | 67 | } else { |
| 49 | 68 | return [headerHtml, false]; |
| 50 | 69 | } |
| 51 | 70 | } |
| 52 | 71 | |
| 53 | | function parseLineItem(markdown, list) { |
| 72 | // Parse list items (* item -> <ul><li>item</li></ul>) |
| 73 | function parseLineItem(markdown, currentlyInList) { |
| 54 | 74 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 75 | const itemContent = markdown.substring(2); // Skip '* ' |
| 76 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 77 | |
| 78 | if (currentlyInList) { |
| 79 | // Continue existing list |
| 57 | 80 | return [innerHtml, true]; |
| 58 | 81 | } else { |
| 82 | // Start new list |
| 59 | 83 | return [`<ul>${innerHtml}`, true]; |
| 60 | 84 | } |
| 61 | 85 | } |
| 62 | | return [null, list]; |
| 86 | return [null, currentlyInList]; |
| 63 | 87 | } |
| 64 | 88 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 89 | // Parse paragraph content |
| 90 | function parseParagraph(markdown, currentlyInList) { |
| 91 | if (currentlyInList) { |
| 92 | // Close list before starting paragraph |
| 69 | 93 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 94 | } else { |
| 95 | return [parseText(markdown, false), false]; |
| 70 | 96 | } |
| 71 | 97 | } |
| 72 | 98 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 99 | // Parse a single line of markdown |
| 100 | function parseLine(markdown, currentlyInList) { |
| 101 | // Try parsing as header first |
| 102 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 103 | |
| 104 | // If not a header, try parsing as list item |
| 75 | 105 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 106 | [result, newListState] = parseLineItem(markdown, currentlyInList); |
| 77 | 107 | } |
| 108 | |
| 109 | // If neither header nor list item, parse as paragraph |
| 78 | 110 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 111 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 112 | } |
| 113 | |
| 114 | // This should never happen with valid input |
| 81 | 115 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 116 | throw new Error('Unable to parse line: ' + markdown); |
| 83 | 117 | } |
| 84 | | return [result, inListAfter]; |
| 118 | |
| 119 | return [result, newListState]; |
| 85 | 120 | } |
| 86 | 121 | |
| 87 | 122 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 123 | * Parse Markdown text and convert it to HTML |
| 124 | * @param {string} markdown - The Markdown text to parse |
| 125 | * @returns {string} - The resulting HTML |
| 90 | 126 | */ |
| 91 | 127 | export function parse(markdown) { |
| 92 | 128 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 129 | let htmlResult = ''; |
| 130 | let isInList = false; |
| 131 | |
| 95 | 132 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 133 | let [lineHtml, newListState] = parseLine(lines[i], isInList); |
| 134 | htmlResult += lineHtml; |
| 135 | isInList = newListState; |
| 99 | 136 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 137 | |
| 138 | // Close any open list at the end |
| 139 | if (isInList) { |
| 140 | return htmlResult + '</ul>'; |
| 102 | 141 | } else { |
| 103 | | return result; |
| 142 | return htmlResult; |
| 104 | 143 | } |
| 105 | 144 | } |