| 6 | 6 | return text.startsWith(`<${tag}>`); |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 9 | // Parse emphasis formatting (__ for bold, _ for italic) |
| 10 | // Uses non-greedy matching to handle multiple instances correctly |
| 11 | function parseEmphasis(markdown, delimiter, tag) { |
| 12 | // Escape special regex characters in delimiter |
| 13 | const escapedDelimiter = delimiter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 14 | // Use non-greedy matching to avoid overlapping issues |
| 15 | const pattern = new RegExp(`${escapedDelimiter}(.+?)${escapedDelimiter}`, 'g'); |
| 11 | 16 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 17 | return markdown.replace(pattern, replacement); |
| 13 | 18 | } |
| 14 | 19 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 20 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 21 | function parseBold(markdown) { |
| 22 | return parseEmphasis(markdown, '__', 'strong'); |
| 17 | 23 | } |
| 18 | 24 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 25 | // Parse italic text (_text_ -> <em>text</em>) |
| 26 | function parseItalic(markdown) { |
| 27 | return parseEmphasis(markdown, '_', 'em'); |
| 21 | 28 | } |
| 22 | 29 | |
| 30 | // Parse text with emphasis formatting and wrap in paragraph tags if not in a list |
| 23 | 31 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 32 | // Apply emphasis formatting (bold, then italic) |
| 33 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 34 | if (list) { |
| 26 | 35 | return parsedText; |
| 27 | 36 | } else { |
| 29 | 38 | } |
| 30 | 39 | } |
| 31 | 40 | |
| 41 | // Parse markdown headers (# to ######) |
| 42 | // Returns [html, inList] tuple |
| 32 | 43 | 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 | | } |
| 44 | // Count consecutive # characters at the start |
| 45 | let headerLevel = 0; |
| 46 | for (let i = 0; i < markdown.length && markdown[i] === '#'; i++) { |
| 47 | headerLevel++; |
| 40 | 48 | } |
| 41 | | if (count === 0 || count > 6) { |
| 49 | |
| 50 | // Valid headers are 1-6 # characters |
| 51 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 52 | return [null, list]; |
| 43 | 53 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 54 | |
| 55 | const headerTag = `h${headerLevel}`; |
| 56 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 57 | const headerHtml = wrap(headerContent, headerTag); |
| 58 | |
| 59 | // Close any open list before header |
| 46 | 60 | if (list) { |
| 47 | 61 | return [`</ul>${headerHtml}`, false]; |
| 48 | 62 | } else { |
| 50 | 64 | } |
| 51 | 65 | } |
| 52 | 66 | |
| 67 | // Parse list items starting with * |
| 68 | // Returns [html, inList] tuple |
| 53 | 69 | function parseLineItem(markdown, list) { |
| 54 | 70 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 71 | // Extract list item content (after "* ") |
| 72 | const itemContent = markdown.substring(2).trim(); |
| 73 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 74 | |
| 56 | 75 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 76 | // Already in a list, just add the list item |
| 77 | return [listItemHtml, true]; |
| 58 | 78 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 79 | // Start a new list |
| 80 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 81 | } |
| 61 | 82 | } |
| 62 | 83 | return [null, list]; |
| 63 | 84 | } |
| 64 | 85 | |
| 86 | // Parse regular paragraphs |
| 87 | // Returns [html, inList] tuple |
| 65 | 88 | function parseParagraph(markdown, list) { |
| 66 | 89 | if (!list) { |
| 90 | // Not in a list, just parse as paragraph |
| 67 | 91 | return [parseText(markdown, false), false]; |
| 68 | 92 | } else { |
| 93 | // Close the list and start a paragraph |
| 69 | 94 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 95 | } |
| 71 | 96 | } |
| 72 | 97 | |
| 98 | // Main line parser - tries different parsers in order |
| 99 | // Returns [html, inList] tuple |
| 73 | 100 | function parseLine(markdown, list) { |
| 101 | // Try parsing as header first |
| 74 | 102 | let [result, inListAfter] = parseHeader(markdown, list); |
| 103 | |
| 104 | // If not a header, try parsing as list item |
| 75 | 105 | if (result === null) { |
| 76 | 106 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 107 | } |
| 108 | |
| 109 | // If not a list item, parse as paragraph |
| 78 | 110 | if (result === null) { |
| 79 | 111 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 112 | } |
| 113 | |
| 114 | // This should never happen as one of the parsers should always match |
| 81 | 115 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 116 | throw new Error('Unable to parse markdown line'); |
| 83 | 117 | } |
| 118 | |
| 84 | 119 | return [result, inListAfter]; |
| 85 | 120 | } |
| 86 | 121 | |
| 87 | 122 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 123 | * Main markdown parser function |
| 124 | * @param {string} markdown - The markdown string to parse |
| 125 | * @returns {string} - The equivalent HTML |
| 90 | 126 | */ |
| 91 | 127 | export function parse(markdown) { |
| 128 | // Split input into lines for processing |
| 92 | 129 | const lines = markdown.split('\n'); |
| 93 | 130 | let result = ''; |
| 94 | | let list = false; |
| 131 | let inList = false; // Track whether we're currently inside a list |
| 132 | |
| 133 | // Process each line sequentially |
| 95 | 134 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 135 | const [lineResult, newInListState] = parseLine(lines[i], inList); |
| 97 | 136 | result += lineResult; |
| 98 | | list = newList; |
| 137 | inList = newInListState; |
| 99 | 138 | } |
| 100 | | if (list) { |
| 139 | |
| 140 | // Close any unclosed list at the end |
| 141 | if (inList) { |
| 101 | 142 | return result + '</ul>'; |
| 102 | 143 | } else { |
| 103 | 144 | return result; |