| 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 with double underscores (__) |
| 7 | function parseBoldUnderscore(markdown) { |
| 8 | // Use greedy matching like the original implementation |
| 9 | return markdown.replace(/__(.+)__/g, '<strong>$1</strong>'); |
| 7 | 10 | } |
| 8 | 11 | |
| 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); |
| 12 | // Parse italic text with single underscores (_) |
| 13 | function parseItalicUnderscore(markdown) { |
| 14 | // Use greedy matching like the original implementation |
| 15 | return markdown.replace(/_(.+)_/g, '<em>$1</em>'); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // Parse bold text with double asterisks (**) |
| 19 | function parseBoldAsterisk(markdown) { |
| 20 | // Use greedy matching to match the original pattern |
| 21 | return markdown.replace(/\*\*(.+)\*\*/g, '<strong>$1</strong>'); |
| 17 | 22 | } |
| 18 | 23 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 24 | // Parse italic text with single asterisks (*) |
| 25 | function parseItalicAsterisk(markdown) { |
| 26 | // Use greedy matching to match the original pattern |
| 27 | return markdown.replace(/\*(.+)\*/g, '<em>$1</em>'); |
| 21 | 28 | } |
| 22 | 29 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 30 | // Parse all emphasis markers in text |
| 31 | function parseEmphasis(markdown) { |
| 32 | // Parse bold first (both variants), then italic (both variants) |
| 33 | // This order handles nested emphasis correctly |
| 34 | let result = parseBoldUnderscore(markdown); |
| 35 | result = parseBoldAsterisk(result); |
| 36 | result = parseItalicUnderscore(result); |
| 37 | result = parseItalicAsterisk(result); |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | // Parse text content, adding paragraph tags if not in a list |
| 42 | function parseText(markdown, inList) { |
| 43 | const parsedText = parseEmphasis(markdown); |
| 44 | if (inList) { |
| 26 | 45 | return parsedText; |
| 27 | 46 | } else { |
| 28 | 47 | return wrap(parsedText, 'p'); |
| 29 | 48 | } |
| 30 | 49 | } |
| 31 | 50 | |
| 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]; |
| 51 | // Parse header markdown (# ## ### etc.) |
| 52 | function parseHeader(markdown, inList) { |
| 53 | // Match 1-6 hash characters at start of line followed by a space |
| 54 | const headerMatch = markdown.match(/^(#{1,6})\s(.*)/); |
| 55 | |
| 56 | if (!headerMatch) { |
| 57 | return [null, inList]; |
| 43 | 58 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 59 | |
| 60 | const level = headerMatch[1].length; // Number of # characters |
| 61 | const content = headerMatch[2]; |
| 62 | const headerTag = `h${level}`; |
| 63 | const headerHtml = wrap(content, headerTag); |
| 64 | |
| 65 | // Close any open list before starting a header |
| 66 | if (inList) { |
| 47 | 67 | return [`</ul>${headerHtml}`, false]; |
| 48 | 68 | } else { |
| 49 | 69 | return [headerHtml, false]; |
| 50 | 70 | } |
| 51 | 71 | } |
| 52 | 72 | |
| 53 | | function parseLineItem(markdown, list) { |
| 54 | | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 73 | // Parse list items (*) |
| 74 | function parseLineItem(markdown, inList) { |
| 75 | if (markdown.startsWith('* ')) { // Require space after asterisk |
| 76 | const content = markdown.substring(2); // Remove '* ' prefix |
| 77 | const innerHtml = wrap(parseText(content, true), 'li'); |
| 78 | |
| 79 | if (inList) { |
| 80 | // Continue existing list |
| 57 | 81 | return [innerHtml, true]; |
| 58 | 82 | } else { |
| 83 | // Start new list |
| 59 | 84 | return [`<ul>${innerHtml}`, true]; |
| 60 | 85 | } |
| 61 | 86 | } |
| 62 | | return [null, list]; |
| 87 | return [null, inList]; |
| 63 | 88 | } |
| 64 | 89 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 90 | // Parse regular paragraph text |
| 91 | function parseParagraph(markdown, inList) { |
| 92 | if (inList) { |
| 93 | // Close list and start paragraph |
| 69 | 94 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 95 | } else { |
| 96 | // Regular paragraph |
| 97 | return [parseText(markdown, false), false]; |
| 70 | 98 | } |
| 71 | 99 | } |
| 72 | 100 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 101 | // Parse a single line of markdown |
| 102 | function parseLine(markdown, inList) { |
| 103 | // Try parsing as header first |
| 104 | let [result, newList] = parseHeader(markdown, inList); |
| 105 | if (result !== null) { |
| 106 | return [result, newList]; |
| 77 | 107 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 108 | |
| 109 | // Try parsing as list item |
| 110 | [result, newList] = parseLineItem(markdown, inList); |
| 111 | if (result !== null) { |
| 112 | return [result, newList]; |
| 80 | 113 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 83 | | } |
| 84 | | return [result, inListAfter]; |
| 114 | |
| 115 | // Default to paragraph |
| 116 | [result, newList] = parseParagraph(markdown, inList); |
| 117 | return [result, newList]; |
| 85 | 118 | } |
| 86 | 119 | |
| 87 | 120 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 121 | * Parse markdown text and convert to HTML |
| 122 | * @param {string} markdown - The markdown text to parse |
| 123 | * @returns {string} - The resulting HTML |
| 90 | 124 | */ |
| 91 | 125 | export function parse(markdown) { |
| 92 | 126 | const lines = markdown.split('\n'); |
| 93 | 127 | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 128 | let inList = false; |
| 129 | |
| 130 | for (const line of lines) { |
| 131 | let [lineResult, newList] = parseLine(line, inList); |
| 97 | 132 | result += lineResult; |
| 98 | | list = newList; |
| 133 | inList = newList; |
| 99 | 134 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 135 | |
| 136 | // Close any open list at the end |
| 137 | if (inList) { |
| 138 | result += '</ul>'; |
| 104 | 139 | } |
| 140 | |
| 141 | return result; |
| 105 | 142 | } |