| 1 | // Helper function to wrap text with 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 | |
| 11 | // Generic parser function to convert Markdown delimiters to HTML tags |
| 9 | 12 | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 13 | // Escape special regex characters in delimiter |
| 14 | const escapedDelimiter = delimiter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 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) { |
| 20 | // Parse double underscore emphasis (__text__ -> <strong>text</strong>) |
| 21 | function parseUnderscoreStrong(markdown) { |
| 16 | 22 | return parser(markdown, '__', 'strong'); |
| 17 | 23 | } |
| 18 | 24 | |
| 19 | | function parse_(markdown) { |
| 25 | // Parse single underscore emphasis (_text_ -> <em>text</em>) |
| 26 | function parseUnderscoreEmphasis(markdown) { |
| 20 | 27 | return parser(markdown, '_', 'em'); |
| 21 | 28 | } |
| 22 | 29 | |
| 30 | // Parse double asterisk emphasis (**text** -> <strong>text</strong>) |
| 31 | function parseAsteriskStrong(markdown) { |
| 32 | return parser(markdown, '**', 'strong'); |
| 33 | } |
| 34 | |
| 35 | // Parse single asterisk emphasis (*text* -> <em>text</em>) |
| 36 | function parseAsteriskEmphasis(markdown) { |
| 37 | return parser(markdown, '*', 'em'); |
| 38 | } |
| 39 | |
| 40 | // Parse text with emphasis formatting |
| 23 | 41 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 42 | // Parse all types of emphasis (both underscore and asterisk based) |
| 43 | let parsedText = markdown; |
| 44 | parsedText = parseAsteriskStrong(parsedText); // **text** -> <strong>text</strong> |
| 45 | parsedText = parseAsteriskEmphasis(parsedText); // *text* -> <em>text</em> |
| 46 | parsedText = parseUnderscoreStrong(parsedText); // __text__ -> <strong>text</strong> |
| 47 | parsedText = parseUnderscoreEmphasis(parsedText); // _text_ -> <em>text</em> |
| 48 | |
| 25 | 49 | if (list) { |
| 26 | 50 | return parsedText; |
| 27 | 51 | } else { |
| 29 | 53 | } |
| 30 | 54 | } |
| 31 | 55 | |
| 56 | // Parse Markdown headers (# ## ### etc.) |
| 32 | 57 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 58 | // Count consecutive # characters at the start |
| 59 | let headerLevel = 0; |
| 34 | 60 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 61 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 62 | headerLevel += 1; |
| 37 | 63 | } else { |
| 38 | 64 | break; |
| 39 | 65 | } |
| 40 | 66 | } |
| 41 | | if (count === 0 || count > 6) { |
| 67 | |
| 68 | // Valid headers have 1-6 # characters |
| 69 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 70 | return [null, list]; |
| 43 | 71 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 72 | |
| 73 | const headerTag = `h${headerLevel}`; |
| 74 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 75 | const headerHtml = wrap(headerContent, headerTag); |
| 76 | |
| 77 | // If we're in a list, close it before the header |
| 46 | 78 | if (list) { |
| 47 | 79 | return [`</ul>${headerHtml}`, false]; |
| 48 | 80 | } else { |
| 50 | 82 | } |
| 51 | 83 | } |
| 52 | 84 | |
| 85 | // Parse list items (* item) |
| 53 | 86 | function parseLineItem(markdown, list) { |
| 54 | 87 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 88 | // Remove the '* ' prefix and parse the remaining text |
| 89 | const itemContent = markdown.substring(2).trim(); |
| 90 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 91 | |
| 56 | 92 | if (list) { |
| 93 | // Already in a list, just add the item |
| 57 | 94 | return [innerHtml, true]; |
| 58 | 95 | } else { |
| 96 | // Start a new list |
| 59 | 97 | return [`<ul>${innerHtml}`, true]; |
| 60 | 98 | } |
| 61 | 99 | } |
| 62 | 100 | return [null, list]; |
| 63 | 101 | } |
| 64 | 102 | |
| 103 | // Parse regular paragraphs |
| 65 | 104 | function parseParagraph(markdown, list) { |
| 66 | 105 | if (!list) { |
| 106 | // Not in a list, just wrap as paragraph |
| 67 | 107 | return [parseText(markdown, false), false]; |
| 68 | 108 | } else { |
| 109 | // In a list, close it first then create paragraph |
| 69 | 110 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 111 | } |
| 71 | 112 | } |
| 72 | 113 | |
| 114 | // Parse a single line of Markdown, determining its type |
| 73 | 115 | function parseLine(markdown, list) { |
| 116 | // Try to parse as header first |
| 74 | 117 | let [result, inListAfter] = parseHeader(markdown, list); |
| 118 | |
| 119 | // If not a header, try to parse as list item |
| 75 | 120 | if (result === null) { |
| 76 | 121 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 122 | } |
| 123 | |
| 124 | // If not a list item, parse as paragraph |
| 78 | 125 | if (result === null) { |
| 79 | 126 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 127 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 83 | | } |
| 128 | |
| 129 | // At this point, result should never be null as parseParagraph always returns a value |
| 84 | 130 | return [result, inListAfter]; |
| 85 | 131 | } |
| 86 | 132 | |
| 87 | 133 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 134 | * Parse Markdown text and convert it to HTML |
| 135 | * @param {string} markdown - The Markdown text to parse |
| 136 | * @returns {string} The corresponding HTML |
| 90 | 137 | */ |
| 91 | 138 | export function parse(markdown) { |
| 92 | 139 | const lines = markdown.split('\n'); |
| 93 | 140 | let result = ''; |
| 94 | | let list = false; |
| 141 | let inList = false; // Track whether we're currently inside a list |
| 142 | |
| 143 | // Process each line |
| 95 | 144 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 145 | const [lineResult, newInList] = parseLine(lines[i], inList); |
| 97 | 146 | result += lineResult; |
| 98 | | list = newList; |
| 147 | inList = newInList; |
| 99 | 148 | } |
| 100 | | if (list) { |
| 149 | |
| 150 | // Close any open list at the end |
| 151 | if (inList) { |
| 101 | 152 | return result + '</ul>'; |
| 102 | 153 | } else { |
| 103 | 154 | return result; |