| 6 | 7 | return text.startsWith(`<${tag}>`); |
| 7 | 8 | } |
| 8 | 9 | |
| 10 | // Generic parser for inline formatting elements |
| 9 | 11 | function parser(markdown, delimiter, tag) { |
| 10 | 12 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 13 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 14 | return markdown.replace(pattern, replacement); |
| 13 | 15 | } |
| 14 | 16 | |
| 15 | | function parse__(markdown) { |
| 17 | // Parse bold text (__text__) |
| 18 | function parseBold(markdown) { |
| 16 | 19 | return parser(markdown, '__', 'strong'); |
| 17 | 20 | } |
| 18 | 21 | |
| 19 | | function parse_(markdown) { |
| 22 | // Parse italic text (_text_) |
| 23 | function parseItalic(markdown) { |
| 20 | 24 | return parser(markdown, '_', 'em'); |
| 21 | 25 | } |
| 22 | 26 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | // Parse inline formatting (bold and italic) and wrap in paragraph tags if needed |
| 28 | function parseInlineFormatting(markdown, isListItem = false) { |
| 29 | // First parse bold, then italic |
| 30 | const formattedText = parseItalic(parseBold(markdown)); |
| 31 | |
| 32 | // If this is part of a list item, don't wrap in paragraph tags |
| 33 | if (isListItem) { |
| 34 | return formattedText; |
| 27 | 35 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 36 | return wrap(formattedText, 'p'); |
| 29 | 37 | } |
| 30 | 38 | } |
| 31 | 39 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 40 | // Parse header elements (# Header) |
| 41 | function parseHeader(markdown, currentlyInList) { |
| 42 | // Count the number of # characters at the beginning |
| 43 | let headerLevel = 0; |
| 34 | 44 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 45 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 46 | headerLevel += 1; |
| 37 | 47 | } else { |
| 38 | 48 | break; |
| 39 | 49 | } |
| 40 | 50 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 51 | |
| 52 | // Headers must be between 1 and 6 # characters |
| 53 | if (headerLevel === 0 || headerLevel > 6) { |
| 54 | return [null, currentlyInList]; |
| 43 | 55 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 56 | |
| 57 | // Create the appropriate header tag (h1, h2, etc.) |
| 58 | const headerTag = `h${headerLevel}`; |
| 59 | const headerContent = markdown.substring(headerLevel + 1); // Skip # and space |
| 60 | const headerHtml = wrap(headerContent, headerTag); |
| 61 | |
| 62 | // If we were in a list, close it before the header |
| 63 | if (currentlyInList) { |
| 47 | 64 | return [`</ul>${headerHtml}`, false]; |
| 48 | 65 | } else { |
| 49 | 66 | return [headerHtml, false]; |
| 50 | 67 | } |
| 51 | 68 | } |
| 52 | 69 | |
| 53 | | function parseLineItem(markdown, list) { |
| 70 | // Parse list items (* item) |
| 71 | function parseListItem(markdown, currentlyInList) { |
| 54 | 72 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 73 | // Parse the content of the list item with inline formatting |
| 74 | const itemContent = markdown.substring(2); // Skip "* " |
| 75 | const formattedContent = wrap(parseInlineFormatting(itemContent, true), 'li'); |
| 76 | |
| 77 | // If we're already in a list, just add the item |
| 78 | // Otherwise, start a new list |
| 79 | if (currentlyInList) { |
| 80 | return [formattedContent, true]; |
| 58 | 81 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 82 | return [`<ul>${formattedContent}`, 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]; |
| 88 | // Parse paragraph elements (default case) |
| 89 | function parseParagraph(markdown, currentlyInList) { |
| 90 | // If we're not in a list, wrap in paragraph tags |
| 91 | // If we were in a list, close it first |
| 92 | if (!currentlyInList) { |
| 93 | return [parseInlineFormatting(markdown, false), false]; |
| 68 | 94 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 95 | return [`</ul>${parseInlineFormatting(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 to parse as header first |
| 102 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 103 | |
| 104 | // If not a header, try as list item |
| 75 | 105 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 106 | [result, newListState] = parseListItem(markdown, currentlyInList); |
| 77 | 107 | } |
| 108 | |
| 109 | // If not a list item, treat 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 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) { |
| 128 | // Split the markdown into individual lines |
| 92 | 129 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 130 | let htmlResult = ''; |
| 131 | let inList = false; |
| 132 | |
| 133 | // Process each line |
| 95 | 134 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 135 | let [lineHtml, newListState] = parseLine(lines[i], inList); |
| 136 | htmlResult += lineHtml; |
| 137 | inList = newListState; |
| 99 | 138 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 139 | |
| 140 | // If we ended while still in a list, close it |
| 141 | if (inList) { |
| 142 | return htmlResult + '</ul>'; |
| 102 | 143 | } else { |
| 103 | | return result; |
| 144 | return htmlResult; |
| 104 | 145 | } |
| 105 | 146 | } |