| 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) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 11 | // Generic parser for inline markdown elements (bold, italic, etc.) |
| 12 | function parseInlineMarkdown(markdown, delimiter, htmlTag) { |
| 13 | const pattern = new RegExp(`${delimiter}(.+?)${delimiter}`); |
| 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 (using __ 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 (using _ 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) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 28 | // Parse inline formatting (bold and italic) within text |
| 29 | function parseInlineFormatting(markdown) { |
| 30 | // Parse bold first, then italic to avoid conflicts |
| 31 | return parseItalicText(parseBoldText(markdown)); |
| 30 | 32 | } |
| 31 | 33 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 34 | // Parse text content with inline formatting, optionally wrapping in paragraph tags |
| 35 | function parseTextContent(markdown, skipParagraphWrap = false) { |
| 36 | const parsedText = parseInlineFormatting(markdown); |
| 37 | return skipParagraphWrap ? parsedText : wrapTextInTag(parsedText, 'p'); |
| 38 | } |
| 39 | |
| 40 | // Parse header markdown (#, ##, ###, etc.) and convert to HTML headers (h1, h2, h3, etc.) |
| 41 | function parseHeader(markdown, currentlyInList) { |
| 42 | // Count consecutive # characters at the beginning of the line |
| 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 | // Valid headers have 1-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 | // Extract header content (text after the # characters and space) |
| 58 | const headerContent = markdown.substring(headerLevel + 1); |
| 59 | const headerTag = `h${headerLevel}`; |
| 60 | const headerHtml = wrapTextInTag(headerContent, headerTag); |
| 61 | |
| 62 | // If we were in a list, close the list before starting 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) { |
| 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 | | } |
| 70 | // Parse list items (starting with *) and handle list container logic |
| 71 | function parseListItem(markdown, currentlyInList) { |
| 72 | if (!markdown.startsWith('*')) { |
| 73 | return [null, currentlyInList]; |
| 74 | } |
| 75 | |
| 76 | // Extract list item content (text after '* ') |
| 77 | const listItemContent = markdown.substring(2); |
| 78 | // Parse inline formatting within the list item, but don't wrap in paragraph tags |
| 79 | const parsedListItemContent = parseTextContent(listItemContent, true); |
| 80 | const listItemHtml = wrapTextInTag(parsedListItemContent, 'li'); |
| 81 | |
| 82 | if (currentlyInList) { |
| 83 | // Already in a list, just add the list item |
| 84 | return [listItemHtml, true]; |
| 85 | } else { |
| 86 | // Not in a list, start a new list container |
| 87 | return [`<ul>${listItemHtml}`, true]; |
| 61 | 88 | } |
| 62 | | return [null, list]; |
| 63 | 89 | } |
| 64 | 90 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 91 | // Parse regular paragraphs (non-header, non-list content) |
| 92 | function parseParagraph(markdown, currentlyInList) { |
| 93 | const paragraphHtml = parseTextContent(markdown, false); |
| 94 | |
| 95 | if (currentlyInList) { |
| 96 | // Close the list before starting the paragraph |
| 97 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 98 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 99 | // Just return the paragraph |
| 100 | return [paragraphHtml, false]; |
| 70 | 101 | } |
| 71 | 102 | } |
| 72 | 103 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 104 | // Parse a single line of markdown, determining what type of element it is |
| 105 | // Returns [htmlResult, stillInList] tuple |
| 106 | function parseLine(markdown, currentlyInList) { |
| 107 | // Try to parse as header first (highest priority) |
| 108 | let [htmlResult, inListAfter] = parseHeader(markdown, currentlyInList); |
| 109 | |
| 110 | // If not a header, try to parse as list item |
| 111 | if (htmlResult === null) { |
| 112 | [htmlResult, inListAfter] = parseListItem(markdown, currentlyInList); |
| 77 | 113 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 114 | |
| 115 | // If not a list item, parse as paragraph (fallback) |
| 116 | if (htmlResult === null) { |
| 117 | [htmlResult, inListAfter] = parseParagraph(markdown, currentlyInList); |
| 80 | 118 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 119 | |
| 120 | // This should never happen as paragraph parsing always succeeds |
| 121 | if (htmlResult === null) { |
| 122 | throw new Error('Failed to parse markdown line'); |
| 83 | 123 | } |
| 84 | | return [result, inListAfter]; |
| 124 | |
| 125 | return [htmlResult, inListAfter]; |
| 85 | 126 | } |
| 86 | 127 | |
| 87 | 128 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 129 | * Main markdown parser function |
| 130 | * @param {string} markdown - The markdown string to parse |
| 131 | * @returns {string} - The equivalent HTML string |
| 90 | 132 | */ |
| 91 | 133 | export function parse(markdown) { |
| 92 | 134 | 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; |
| 135 | let htmlResult = ''; |
| 136 | let currentlyInList = false; |
| 137 | |
| 138 | // Process each line of the markdown |
| 139 | for (const line of lines) { |
| 140 | const [lineHtml, stillInList] = parseLine(line, currentlyInList); |
| 141 | htmlResult += lineHtml; |
| 142 | currentlyInList = stillInList; |
| 99 | 143 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 144 | |
| 145 | // If we ended while still in a list, close it |
| 146 | if (currentlyInList) { |
| 147 | htmlResult += '</ul>'; |
| 104 | 148 | } |
| 149 | |
| 150 | return htmlResult; |
| 105 | 151 | } |