| 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 | |
| 6 | // Helper function to check if text starts with a specific HTML tag |
| 5 | 7 | function isTag(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 | // Generic parser for emphasis markers (bold, italic) |
| 12 | // Fixed: Use non-greedy matching and global flag to handle multiple instances |
| 13 | function parseEmphasis(markdown, delimiter, tag) { |
| 14 | // Escape special regex characters in the delimiter |
| 15 | const escapedDelimiter = delimiter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 16 | // Use non-greedy quantifier (.+?) and global flag to match all instances |
| 17 | const pattern = new RegExp(`${escapedDelimiter}(.+?)${escapedDelimiter}`, 'g'); |
| 11 | 18 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 19 | return markdown.replace(pattern, replacement); |
| 13 | 20 | } |
| 14 | 21 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 22 | // Parse bold text using ** delimiter |
| 23 | function parseBold(markdown) { |
| 24 | return parseEmphasis(markdown, '**', 'strong'); |
| 17 | 25 | } |
| 18 | 26 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 27 | // Parse italic text using * delimiter |
| 28 | function parseItalic(markdown) { |
| 29 | return parseEmphasis(markdown, '*', 'em'); |
| 21 | 30 | } |
| 22 | 31 | |
| 32 | // Parse text with emphasis formatting (bold and italic) |
| 23 | 33 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 34 | // Apply emphasis parsing in correct order (bold first, then italic) |
| 35 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 36 | if (list) { |
| 26 | 37 | return parsedText; |
| 27 | 38 | } else { |
| 50 | 70 | } |
| 51 | 71 | } |
| 52 | 72 | |
| 73 | // Parse list items starting with * (must be followed by space) |
| 53 | 74 | function parseLineItem(markdown, list) { |
| 54 | | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 75 | // Only treat as list item if it starts with "* " (asterisk followed by space) |
| 76 | // This prevents conflict with emphasis like "*italic*" |
| 77 | if (markdown.startsWith('* ')) { |
| 78 | // Extract list item content (skip "* ") |
| 79 | const itemContent = markdown.substring(2); |
| 80 | // Parse the content for emphasis and wrap in li tag |
| 81 | const listItemHtml = wrap(parseText(itemContent, true), 'li'); |
| 82 | |
| 56 | 83 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 84 | // Already in a list, just add the item |
| 85 | return [listItemHtml, true]; |
| 58 | 86 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 87 | // Start a new list |
| 88 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 89 | } |
| 61 | 90 | } |
| 62 | 91 | return [null, list]; |
| 63 | 92 | } |
| 64 | 93 | |
| 94 | // Parse regular paragraphs |
| 65 | 95 | function parseParagraph(markdown, list) { |
| 96 | const paragraphHtml = parseText(markdown, false); |
| 97 | |
| 66 | 98 | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 99 | // Not in a list, just return the paragraph |
| 100 | return [paragraphHtml, false]; |
| 68 | 101 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 102 | // Close the list and start the paragraph |
| 103 | return [`</ul>${paragraphHtml}`, false]; |
| 70 | 104 | } |
| 71 | 105 | } |
| 72 | 106 | |
| 107 | // Parse a single line of markdown |
| 73 | 108 | function parseLine(markdown, list) { |
| 109 | // Try parsing as header first |
| 74 | 110 | let [result, inListAfter] = parseHeader(markdown, list); |
| 111 | |
| 112 | // If not a header, try parsing as list item |
| 75 | 113 | if (result === null) { |
| 76 | 114 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 115 | } |
| 116 | |
| 117 | // If neither header nor list item, treat as paragraph |
| 78 | 118 | if (result === null) { |
| 79 | 119 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 120 | } |
| 121 | |
| 122 | // This should never happen with valid markdown |
| 81 | 123 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 124 | throw new Error('Unable to parse markdown line: ' + markdown); |
| 83 | 125 | } |
| 126 | |
| 84 | 127 | return [result, inListAfter]; |
| 85 | 128 | } |
| 86 | 129 | |
| 87 | 130 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 131 | * Parse markdown text and convert it to HTML |
| 132 | * @param {string} markdown - The markdown text to parse |
| 133 | * @returns {string} - The resulting HTML |
| 90 | 134 | */ |
| 91 | 135 | export function parse(markdown) { |
| 136 | // Split the markdown into individual lines |
| 92 | 137 | const lines = markdown.split('\n'); |
| 93 | 138 | let result = ''; |
| 94 | | let list = false; |
| 139 | let list = false; // Track if we're currently inside a list |
| 140 | |
| 141 | // Process each line of markdown |
| 95 | 142 | for (let i = 0; i < lines.length; i++) { |
| 96 | 143 | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | 144 | result += lineResult; |
| 98 | 145 | list = newList; |
| 99 | 146 | } |
| 147 | |
| 148 | // Close any open list at the end of the document |
| 100 | 149 | if (list) { |
| 101 | 150 | return result + '</ul>'; |
| 102 | 151 | } else { |