| 1 | | function wrap(text, tag) { |
| 1 | // Helper function to wrap text in HTML tags |
| 2 | function wrapTextInTag(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 5 | | function isTag(text, tag) { |
| 6 | // Helper function to check if text starts with a specific HTML tag |
| 7 | function startsWithTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 11 | // Generic parser for inline markdown elements (bold, italic, etc.) |
| 12 | function parseInlineMarkdown(markdown, delimiter, htmlTag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 14 | const replacement = `<${htmlTag}>$1</${htmlTag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // Parse bold text (__ delimiter) |
| 19 | function parseBoldText(markdown) { |
| 20 | return parseInlineMarkdown(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 23 | // Parse italic text (_ delimiter) |
| 24 | function parseItalicText(markdown) { |
| 25 | return parseInlineMarkdown(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) within text |
| 29 | function parseInlineFormatting(markdown, isListItem) { |
| 30 | const parsedText = parseItalicText(parseBoldText(markdown)); |
| 31 | if (isListItem) { |
| 26 | 32 | return parsedText; |
| 27 | 33 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 34 | return wrapTextInTag(parsedText, 'p'); |
| 29 | 35 | } |
| 30 | 36 | } |
| 31 | 37 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 38 | // Parse header markdown (#, ##, ###, etc.) |
| 39 | function parseHeader(markdown, isInList) { |
| 40 | // Count consecutive # characters at the beginning |
| 41 | let headerLevel = 0; |
| 34 | 42 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 43 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 44 | headerLevel += 1; |
| 37 | 45 | } else { |
| 38 | 46 | break; |
| 39 | 47 | } |
| 40 | 48 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 49 | |
| 50 | // Valid headers have 1-6 # characters |
| 51 | if (headerLevel === 0 || headerLevel > 6) { |
| 52 | return [null, isInList]; |
| 43 | 53 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 54 | |
| 55 | // Create the header tag (h1, h2, h3, etc.) |
| 56 | const headerTag = `h${headerLevel}`; |
| 57 | const headerContent = markdown.substring(headerLevel + 1); |
| 58 | const headerHtml = wrapTextInTag(headerContent, headerTag); |
| 59 | |
| 60 | // If we were in a list, close it before starting the header |
| 61 | if (isInList) { |
| 47 | 62 | return [`</ul>${headerHtml}`, false]; |
| 48 | 63 | } else { |
| 49 | 64 | return [headerHtml, false]; |
| 50 | 65 | } |
| 51 | 66 | } |
| 52 | 67 | |
| 53 | | function parseLineItem(markdown, list) { |
| 68 | // Parse list items (starting with *) |
| 69 | function parseListItem(markdown, isInList) { |
| 54 | 70 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 71 | // Extract the content after the '* ' prefix |
| 72 | const listItemContent = markdown.substring(2); |
| 73 | const formattedContent = parseInlineFormatting(listItemContent, true); |
| 74 | const listItemHtml = wrapTextInTag(formattedContent, 'li'); |
| 75 | |
| 76 | if (isInList) { |
| 77 | // Continue existing list |
| 78 | return [listItemHtml, true]; |
| 58 | 79 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 80 | // Start a new list |
| 81 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 82 | } |
| 61 | 83 | } |
| 62 | | return [null, list]; |
| 84 | return [null, isInList]; |
| 63 | 85 | } |
| 64 | 86 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 87 | // Parse regular paragraphs (default case when no other markdown patterns match) |
| 88 | function parseParagraph(markdown, isInList) { |
| 89 | const paragraphHtml = parseInlineFormatting(markdown, false); |
| 90 | |
| 91 | if (isInList) { |
| 92 | // Close the list and start a paragraph |
| 93 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 94 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 95 | // Just return the paragraph |
| 96 | return [paragraphHtml, false]; |
| 70 | 97 | } |
| 71 | 98 | } |
| 72 | 99 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 100 | // Parse a single line of markdown, determining its type and converting to HTML |
| 101 | function parseLine(markdown, isInList) { |
| 102 | // Try to parse as header first |
| 103 | let [result, newInListState] = parseHeader(markdown, isInList); |
| 104 | |
| 105 | // If not a header, try to parse as list item |
| 75 | 106 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 107 | [result, newInListState] = parseListItem(markdown, isInList); |
| 77 | 108 | } |
| 109 | |
| 110 | // If not a list item, parse as paragraph |
| 78 | 111 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 112 | [result, newInListState] = parseParagraph(markdown, isInList); |
| 80 | 113 | } |
| 114 | |
| 115 | // This should never happen as one of the parsers should always match |
| 81 | 116 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 117 | throw new Error('Unable to parse markdown line'); |
| 83 | 118 | } |
| 84 | | return [result, inListAfter]; |
| 119 | |
| 120 | return [result, newInListState]; |
| 85 | 121 | } |
| 86 | 122 | |
| 87 | 123 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 124 | * Main markdown parser function |
| 125 | * @param {string} markdown - The markdown string to parse |
| 126 | * @returns {string} - The resulting HTML |
| 90 | 127 | */ |
| 91 | 128 | export function parse(markdown) { |
| 92 | 129 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 130 | let htmlResult = ''; |
| 131 | let isInList = false; |
| 132 | |
| 133 | // Process each line of markdown |
| 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 | const [lineHtml, newInListState] = parseLine(lines[i], isInList); |
| 136 | htmlResult += lineHtml; |
| 137 | isInList = newInListState; |
| 99 | 138 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 139 | |
| 140 | // Close any unclosed list at the end |
| 141 | if (isInList) { |
| 142 | htmlResult += '</ul>'; |
| 104 | 143 | } |
| 144 | |
| 145 | return htmlResult; |
| 105 | 146 | } |