| 1 | /** |
| 2 | * Markdown parser that converts Markdown syntax to HTML |
| 3 | * |
| 4 | * Supports: |
| 5 | * - Headers (# Header 1, ## Header 2, etc.) |
| 6 | * - Paragraphs (regular text) |
| 7 | * - Emphasis (_italic_, __bold__) |
| 8 | * - Unordered lists (* item) |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Wraps text with HTML tags |
| 13 | * @param {string} text - Text to wrap |
| 14 | * @param {string} tag - HTML tag to use |
| 15 | * @returns {string} Text wrapped in HTML tags |
| 16 | */ |
| 1 | 17 | function wrap(text, tag) { |
| 2 | 18 | return `<${tag}>${text}</${tag}>`; |
| 3 | 19 | } |
| 4 | 20 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 21 | /** |
| 22 | * Parses bold text (__text__ -> <strong>text</strong>) |
| 23 | * @param {string} markdown - Markdown text |
| 24 | * @returns {string} HTML with bold formatting |
| 25 | */ |
| 26 | function parseBold(markdown) { |
| 27 | const pattern = new RegExp('__(.+)__'); |
| 28 | const replacement = '<strong>$1</strong>'; |
| 12 | 29 | return markdown.replace(pattern, replacement); |
| 13 | 30 | } |
| 14 | 31 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 32 | /** |
| 33 | * Parses italic text (_text_ -> <em>text</em>) |
| 34 | * @param {string} markdown - Markdown text |
| 35 | * @returns {string} HTML with italic formatting |
| 36 | */ |
| 37 | function parseItalic(markdown) { |
| 38 | const pattern = new RegExp('_(.+)_'); |
| 39 | const replacement = '<em>$1</em>'; |
| 40 | return markdown.replace(pattern, replacement); |
| 21 | 41 | } |
| 22 | 42 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 43 | /** |
| 44 | * Parses inline formatting (bold and italic) |
| 45 | * @param {string} markdown - Markdown text |
| 46 | * @returns {string} HTML with inline formatting |
| 47 | */ |
| 48 | function parseInlineFormatting(markdown) { |
| 49 | // Parse bold first, then italic to handle nested formatting correctly |
| 50 | return parseItalic(parseBold(markdown)); |
| 30 | 51 | } |
| 31 | 52 | |
| 32 | | function parseHeader(markdown, list) { |
| 53 | /** |
| 54 | * Parses header markdown (# Header -> <h1>Header</h1>) |
| 55 | * @param {string} markdown - Markdown line |
| 56 | * @returns {string|null} HTML header or null if not a header |
| 57 | */ |
| 58 | function parseHeader(markdown) { |
| 59 | // Count consecutive # characters at the beginning |
| 33 | 60 | let count = 0; |
| 34 | 61 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 62 | if (markdown[i] === '#') { |
| 38 | 65 | break; |
| 39 | 66 | } |
| 40 | 67 | } |
| 68 | |
| 69 | // Valid headers have 1-6 # characters |
| 41 | 70 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 71 | return null; |
| 43 | 72 | } |
| 73 | |
| 74 | // Create appropriate header tag (h1-h6) |
| 44 | 75 | 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 | | } |
| 76 | const headerText = markdown.substring(count + 1); |
| 77 | return wrap(headerText, headerTag); |
| 51 | 78 | } |
| 52 | 79 | |
| 53 | | function parseLineItem(markdown, list) { |
| 80 | /** |
| 81 | * Parses a list item (* item -> <li>item</li>) |
| 82 | * @param {string} markdown - Markdown line |
| 83 | * @returns {string|null} HTML list item or null if not a list item |
| 84 | */ |
| 85 | function parseListItem(markdown) { |
| 54 | 86 | 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 | | } |
| 87 | const itemText = markdown.substring(2); |
| 88 | const formattedText = parseInlineFormatting(itemText); |
| 89 | return wrap(formattedText, 'li'); |
| 61 | 90 | } |
| 62 | | return [null, list]; |
| 91 | return null; |
| 63 | 92 | } |
| 64 | 93 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | | } |
| 94 | /** |
| 95 | * Parses a paragraph (regular text with inline formatting) |
| 96 | * @param {string} markdown - Markdown line |
| 97 | * @returns {string} HTML paragraph |
| 98 | */ |
| 99 | function parseParagraph(markdown) { |
| 100 | const formattedText = parseInlineFormatting(markdown); |
| 101 | return wrap(formattedText, 'p'); |
| 71 | 102 | } |
| 72 | 103 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 104 | /** |
| 105 | * Processes a single line of markdown based on its type |
| 106 | * @param {string} line - A single line of markdown |
| 107 | * @param {boolean} inList - Whether we're currently in a list |
| 108 | * @returns {[string, boolean]} Tuple of [HTML output, new inList state] |
| 109 | */ |
| 110 | function processLine(line, inList) { |
| 111 | // Try to parse as header |
| 112 | const header = parseHeader(line); |
| 113 | if (header !== null) { |
| 114 | // If we were in a list, close it first |
| 115 | const output = inList ? `</ul>${header}` : header; |
| 116 | return [output, false]; // Headers end lists |
| 80 | 117 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 118 | |
| 119 | // Try to parse as list item |
| 120 | const listItem = parseListItem(line); |
| 121 | if (listItem !== null) { |
| 122 | // If we weren't in a list, start one |
| 123 | const output = inList ? listItem : `<ul>${listItem}`; |
| 124 | return [output, true]; // We're now in a list |
| 83 | 125 | } |
| 84 | | return [result, inListAfter]; |
| 126 | |
| 127 | // Parse as paragraph |
| 128 | const paragraph = parseParagraph(line); |
| 129 | // If we were in a list, close it first |
| 130 | const output = inList ? `</ul>${paragraph}` : paragraph; |
| 131 | return [output, false]; // Paragraphs end lists |
| 85 | 132 | } |
| 86 | 133 | |
| 87 | 134 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 135 | * Parses markdown text and converts it to HTML |
| 136 | * @param {string} markdown - Markdown text to parse |
| 137 | * @returns {string} HTML representation |
| 90 | 138 | */ |
| 91 | 139 | export function parse(markdown) { |
| 92 | 140 | 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; |
| 141 | let html = ''; |
| 142 | let inList = false; |
| 143 | |
| 144 | // Process each line |
| 145 | for (const line of lines) { |
| 146 | const [lineHtml, newInList] = processLine(line, inList); |
| 147 | html += lineHtml; |
| 148 | inList = newInList; |
| 99 | 149 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 150 | |
| 151 | // Close any open list at the end |
| 152 | if (inList) { |
| 153 | html += '</ul>'; |
| 104 | 154 | } |
| 155 | |
| 156 | return html; |
| 105 | 157 | } |