| 1 | | function wrap(text, tag) { |
| 1 | // Utility 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 | // 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 delimiter-based markdown (bold, italic, etc.) |
| 12 | function parseDelimitedText(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 parseDelimitedText(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 parseDelimitedText(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 | return parseItalicText(parseBoldText(markdown)); |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 34 | | for (let i = 0; i < markdown.length; i++) { |
| 35 | | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 37 | | } else { |
| 38 | | break; |
| 39 | | } |
| 40 | | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 43 | | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 47 | | return [`</ul>${headerHtml}`, false]; |
| 48 | | } else { |
| 49 | | return [headerHtml, false]; |
| 50 | | } |
| 33 | // Parse text content with optional paragraph wrapping |
| 34 | function parseTextContent(markdown, isListItem = false) { |
| 35 | const formattedText = parseInlineFormatting(markdown); |
| 36 | return isListItem ? formattedText : wrapTextInTag(formattedText, 'p'); |
| 51 | 37 | } |
| 52 | 38 | |
| 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 | | } |
| 39 | // Parse header lines (starting with # characters) |
| 40 | function parseHeaderLine(markdown, isCurrentlyInList) { |
| 41 | // Count consecutive # characters at the start |
| 42 | const headerLevel = markdown.match(/^#+/)?.[0].length || 0; |
| 43 | |
| 44 | // Return null if not a valid header (no # or more than 6) |
| 45 | if (headerLevel === 0 || headerLevel > 6) { |
| 46 | return { html: null, isInList: isCurrentlyInList }; |
| 61 | 47 | } |
| 62 | | return [null, list]; |
| 48 | |
| 49 | // Extract header content (after # and space) |
| 50 | const headerContent = markdown.substring(headerLevel + 1); |
| 51 | const headerHtml = wrapTextInTag(headerContent, `h${headerLevel}`); |
| 52 | |
| 53 | // Close list if we were in one |
| 54 | const listClosingTag = isCurrentlyInList ? '</ul>' : ''; |
| 55 | |
| 56 | return { |
| 57 | html: `${listClosingTag}${headerHtml}`, |
| 58 | isInList: false |
| 59 | }; |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 62 | // Parse list items (starting with *) |
| 63 | function parseListItem(markdown, isCurrentlyInList) { |
| 64 | // Check if this is a list item |
| 65 | if (!markdown.startsWith('*')) { |
| 66 | return { html: null, isInList: isCurrentlyInList }; |
| 70 | 67 | } |
| 68 | |
| 69 | // Extract list item content (after * and space) |
| 70 | const itemContent = markdown.substring(2); |
| 71 | const listItemHtml = wrapTextInTag(parseTextContent(itemContent, true), 'li'); |
| 72 | |
| 73 | // Open list if we weren't in one |
| 74 | const listOpeningTag = isCurrentlyInList ? '' : '<ul>'; |
| 75 | |
| 76 | return { |
| 77 | html: `${listOpeningTag}${listItemHtml}`, |
| 78 | isInList: true |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | // Parse regular paragraphs |
| 83 | function parseParagraph(markdown, isCurrentlyInList) { |
| 84 | const paragraphHtml = parseTextContent(markdown, false); |
| 85 | |
| 86 | // Close list if we were in one |
| 87 | const listClosingTag = isCurrentlyInList ? '</ul>' : ''; |
| 88 | |
| 89 | return { |
| 90 | html: `${listClosingTag}${paragraphHtml}`, |
| 91 | isInList: false |
| 92 | }; |
| 71 | 93 | } |
| 72 | 94 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 95 | // Parse a single line of markdown, determining its type and converting to HTML |
| 96 | function parseMarkdownLine(markdown, isCurrentlyInList) { |
| 97 | // Try to parse as header first |
| 98 | let parseResult = parseHeaderLine(markdown, isCurrentlyInList); |
| 99 | |
| 100 | // If not a header, try to parse as list item |
| 101 | if (parseResult.html === null) { |
| 102 | parseResult = parseListItem(markdown, isCurrentlyInList); |
| 77 | 103 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 104 | |
| 105 | // If not a list item, parse as paragraph |
| 106 | if (parseResult.html === null) { |
| 107 | parseResult = parseParagraph(markdown, isCurrentlyInList); |
| 80 | 108 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 109 | |
| 110 | // At this point, we should have a valid result |
| 111 | if (parseResult.html === null) { |
| 112 | throw new Error('Unable to parse markdown line'); |
| 83 | 113 | } |
| 84 | | return [result, inListAfter]; |
| 114 | |
| 115 | return parseResult; |
| 85 | 116 | } |
| 86 | 117 | |
| 87 | 118 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 119 | * Parse markdown text and convert it to HTML |
| 120 | * @param {string} markdown - The markdown text to parse |
| 121 | * @returns {string} - The resulting HTML |
| 90 | 122 | */ |
| 91 | 123 | export function parse(markdown) { |
| 92 | 124 | 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; |
| 125 | let htmlResult = ''; |
| 126 | let isCurrentlyInList = false; |
| 127 | |
| 128 | // Process each line of markdown |
| 129 | for (const line of lines) { |
| 130 | const parseResult = parseMarkdownLine(line, isCurrentlyInList); |
| 131 | htmlResult += parseResult.html; |
| 132 | isCurrentlyInList = parseResult.isInList; |
| 99 | 133 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 134 | |
| 135 | // Close any remaining open list |
| 136 | if (isCurrentlyInList) { |
| 137 | htmlResult += '</ul>'; |
| 104 | 138 | } |
| 139 | |
| 140 | return htmlResult; |
| 105 | 141 | } |