| 1 | // Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 6 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 7 | function parseBold(markdown) { |
| 8 | // Match double underscores with meaningful content |
| 9 | // Use non-greedy matching but ensure there's actual content |
| 10 | return markdown.replace(/__(.+?)__/g, '<strong>$1</strong>'); |
| 7 | 11 | } |
| 8 | 12 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 13 | // Parse italic text (_text_ -> <em>text</em>) |
| 14 | function parseItalic(markdown) { |
| 15 | // Match single underscores with meaningful content |
| 16 | // But avoid matching underscores that are part of other formatting |
| 17 | // Use word boundaries to be more precise about valid formatting |
| 18 | return markdown.replace(/\B_(\S[^_]*?\S)_\B/g, '<em>$1</em>'); |
| 13 | 19 | } |
| 14 | 20 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 21 | // Parse inline formatting (bold and italic) |
| 22 | function parseInlineFormatting(markdown) { |
| 23 | // Parse bold first, then italic to handle nested formatting correctly |
| 24 | return parseItalic(parseBold(markdown)); |
| 17 | 25 | } |
| 18 | 26 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 21 | | } |
| 22 | | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 27 | // Parse text with appropriate wrapping |
| 28 | function parseText(markdown, inList) { |
| 29 | const parsedText = parseInlineFormatting(markdown); |
| 30 | if (inList) { |
| 26 | 31 | return parsedText; |
| 27 | 32 | } else { |
| 28 | 33 | return wrap(parsedText, 'p'); |
| 29 | 34 | } |
| 30 | 35 | } |
| 31 | 36 | |
| 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 | | } |
| 37 | // Parse headers (# text -> <h1>text</h1>, ## text -> <h2>text</h2>, etc.) |
| 38 | function parseHeader(markdown, inList) { |
| 39 | // Count leading hash characters |
| 40 | let hashCount = 0; |
| 41 | for (let i = 0; i < markdown.length && markdown[i] === '#'; i++) { |
| 42 | hashCount++; |
| 40 | 43 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 44 | |
| 45 | // Valid headers have 1-6 hashes followed by content |
| 46 | if (hashCount === 0 || hashCount > 6) { |
| 47 | return [null, inList]; |
| 43 | 48 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 49 | |
| 50 | // Extract content after hashes and space |
| 51 | const content = markdown.substring(hashCount + 1); |
| 52 | const headerTag = `h${hashCount}`; |
| 53 | const headerHtml = wrap(content, headerTag); |
| 54 | |
| 55 | // Close any open list before adding header |
| 56 | if (inList) { |
| 47 | 57 | return [`</ul>${headerHtml}`, false]; |
| 48 | 58 | } else { |
| 49 | 59 | return [headerHtml, false]; |
| 50 | 60 | } |
| 51 | 61 | } |
| 52 | 62 | |
| 53 | | function parseLineItem(markdown, list) { |
| 54 | | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 63 | // Parse list items (* item -> <li>item</li>) |
| 64 | function parseLineItem(markdown, inList) { |
| 65 | // Only treat as list item if asterisk is followed by a space |
| 66 | if (markdown.startsWith('* ') || markdown.startsWith('*\t')) { |
| 67 | const content = markdown.substring(2); // Remove '* ' prefix |
| 68 | const innerHtml = wrap(parseText(content, true), 'li'); |
| 69 | |
| 70 | if (inList) { |
| 57 | 71 | return [innerHtml, true]; |
| 58 | 72 | } else { |
| 59 | 73 | return [`<ul>${innerHtml}`, true]; |
| 60 | 74 | } |
| 61 | 75 | } |
| 62 | | return [null, list]; |
| 76 | return [null, inList]; |
| 63 | 77 | } |
| 64 | 78 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 79 | // Parse regular paragraphs |
| 80 | function parseParagraph(markdown, inList) { |
| 81 | if (inList) { |
| 82 | // Close the list before starting a new paragraph |
| 69 | 83 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 84 | } else { |
| 85 | return [parseText(markdown, false), false]; |
| 70 | 86 | } |
| 71 | 87 | } |
| 72 | 88 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | | } |
| 89 | // Parse a single line of markdown, trying different parsers in order |
| 90 | function parseLine(markdown, inList) { |
| 91 | // Try to parse as header first |
| 92 | let [result, newInList] = parseHeader(markdown, inList); |
| 93 | |
| 94 | // If not a header, try to parse as list item |
| 78 | 95 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 96 | [result, newInList] = parseLineItem(markdown, inList); |
| 80 | 97 | } |
| 98 | |
| 99 | // If not a list item, parse as paragraph |
| 81 | 100 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 101 | [result, newInList] = parseParagraph(markdown, inList); |
| 83 | 102 | } |
| 84 | | return [result, inListAfter]; |
| 103 | |
| 104 | // At this point, result should never be null since parseParagraph always returns a result |
| 105 | return [result, newInList]; |
| 85 | 106 | } |
| 86 | 107 | |
| 87 | 108 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 109 | * Parse markdown string to HTML |
| 110 | * @param {string} markdown - The markdown string to parse |
| 111 | * @returns {string} - The resulting HTML |
| 90 | 112 | */ |
| 91 | 113 | export function parse(markdown) { |
| 114 | // Handle empty input |
| 115 | if (!markdown || markdown.trim() === '') { |
| 116 | return ''; |
| 117 | } |
| 118 | |
| 92 | 119 | const lines = markdown.split('\n'); |
| 93 | 120 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 121 | let inList = false; |
| 122 | |
| 123 | // Process each line |
| 124 | for (const line of lines) { |
| 125 | const [lineResult, newInList] = parseLine(line, inList); |
| 97 | 126 | result += lineResult; |
| 98 | | list = newList; |
| 127 | inList = newInList; |
| 99 | 128 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 129 | |
| 130 | // Close any open list at the end |
| 131 | if (inList) { |
| 132 | result += '</ul>'; |
| 104 | 133 | } |
| 134 | |
| 135 | return result; |
| 105 | 136 | } |