| 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 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 11 | // Refactored: Unified text formatting parser that handles multiple delimiters |
| 12 | function parseTextFormatting(markdown) { |
| 13 | // Parse bold text first (double underscores) |
| 14 | const boldPattern = new RegExp(`__(.+)__`); |
| 15 | markdown = markdown.replace(boldPattern, '<strong>$1</strong>'); |
| 16 | |
| 17 | // Then parse italic text (single underscores) |
| 18 | const italicPattern = new RegExp(`_(.+)_`); |
| 19 | markdown = markdown.replace(italicPattern, '<em>$1</em>'); |
| 20 | |
| 21 | return markdown; |
| 13 | 22 | } |
| 14 | 23 | |
| 24 | // Legacy functions for backward compatibility (if needed) |
| 15 | 25 | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 26 | return parseTextFormatting(markdown); |
| 17 | 27 | } |
| 18 | 28 | |
| 19 | 29 | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 30 | return parseTextFormatting(markdown); |
| 21 | 31 | } |
| 22 | 32 | |
| 33 | // Parse text content with formatting, optionally wrapping in paragraph tags |
| 23 | 34 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 35 | const parsedText = parseTextFormatting(markdown); |
| 36 | return list ? parsedText : wrap(parsedText, 'p'); |
| 30 | 37 | } |
| 31 | 38 | |
| 39 | // Parse header markdown (#, ##, ###, etc.) and convert to HTML headers (h1, h2, h3, etc.) |
| 32 | 40 | 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) { |
| 41 | // Match headers with 1-6 # characters followed by a space and content |
| 42 | const headerPattern = /^(#{1,6})\s(.*)$/; |
| 43 | const match = markdown.match(headerPattern); |
| 44 | |
| 45 | // If no match or invalid header level, return null |
| 46 | if (!match) { |
| 42 | 47 | return [null, list]; |
| 43 | 48 | } |
| 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 | | } |
| 49 | |
| 50 | const headerLevel = match[1].length; // Number of # characters |
| 51 | const headerContent = match[2]; // Content after # characters |
| 52 | |
| 53 | // Create appropriate header tag (h1-h6) |
| 54 | const headerTag = `h${headerLevel}`; |
| 55 | const headerHtml = wrap(headerContent, headerTag); |
| 56 | |
| 57 | // If we're in a list, close it before the header |
| 58 | return list ? [`</ul>${headerHtml}`, false] : [headerHtml, false]; |
| 51 | 59 | } |
| 52 | 60 | |
| 61 | // Parse list items marked with '*' |
| 53 | 62 | 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 | | } |
| 63 | // Check if line starts with '* ' (asterisk followed by space) |
| 64 | if (!markdown.startsWith('* ')) { |
| 65 | return [null, list]; |
| 61 | 66 | } |
| 62 | | return [null, list]; |
| 67 | |
| 68 | // Extract content after '* ' and parse formatting |
| 69 | const content = markdown.substring(2); |
| 70 | const listItemContent = parseText(content, true); // true = don't wrap in <p> tags |
| 71 | const listItemHtml = wrap(listItemContent, 'li'); |
| 72 | |
| 73 | // If we're already in a list, just return the list item |
| 74 | // Otherwise, start a new unordered list |
| 75 | return list ? [listItemHtml, true] : [`<ul>${listItemHtml}`, true]; |
| 63 | 76 | } |
| 64 | 77 | |
| 78 | // Parse regular paragraph text |
| 65 | 79 | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 80 | const paragraphHtml = parseText(markdown, false); // false = wrap in <p> tags |
| 81 | |
| 82 | // If we're in a list, close it before the paragraph |
| 83 | return list ? [`</ul>${paragraphHtml}`, false] : [paragraphHtml, false]; |
| 71 | 84 | } |
| 72 | 85 | |
| 86 | // Parse a single line of markdown and return HTML with updated list state |
| 73 | 87 | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 88 | // Try to parse as header |
| 89 | let [result, newList] = parseHeader(markdown, list); |
| 90 | if (result !== null) { |
| 91 | return [result, newList]; |
| 77 | 92 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 93 | |
| 94 | // Try to parse as list item |
| 95 | [result, newList] = parseLineItem(markdown, list); |
| 96 | if (result !== null) { |
| 97 | return [result, newList]; |
| 80 | 98 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 99 | |
| 100 | // Default to paragraph parsing |
| 101 | [result, newList] = parseParagraph(markdown, list); |
| 102 | if (result !== null) { |
| 103 | return [result, newList]; |
| 83 | 104 | } |
| 84 | | return [result, inListAfter]; |
| 105 | |
| 106 | // This should never happen if all cases are covered |
| 107 | throw new Error('Unable to parse line: ' + markdown); |
| 85 | 108 | } |
| 86 | 109 | |
| 87 | 110 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 111 | * Parse markdown text and convert it to HTML |
| 112 | * @param {string} markdown - The markdown text to parse |
| 113 | * @returns {string} - The resulting HTML |
| 90 | 114 | */ |
| 91 | 115 | export function parse(markdown) { |
| 116 | // Split the markdown into individual lines |
| 92 | 117 | 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; |
| 99 | | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 118 | let htmlResult = ''; |
| 119 | let isInList = false; |
| 120 | |
| 121 | // Process each line of markdown |
| 122 | for (const line of lines) { |
| 123 | // Parse the current line and get the updated list state |
| 124 | const [lineHtml, updatedIsInList] = parseLine(line, isInList); |
| 125 | htmlResult += lineHtml; |
| 126 | isInList = updatedIsInList; |
| 104 | 127 | } |
| 128 | |
| 129 | // If we're still in a list at the end, close it |
| 130 | return isInList ? htmlResult + '</ul>' : htmlResult; |
| 105 | 131 | } |