| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 4 | | |
| 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}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 13 | | } |
| 14 | | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 21 | | } |
| 22 | | |
| 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 | | } |
| 30 | | } |
| 31 | | |
| 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; |
| 1 | /** |
| 2 | * A Markdown parser that converts Markdown syntax to HTML |
| 3 | * Supports headers (#), bold (__text__), italic (_text_), and lists (*) |
| 4 | */ |
| 5 | class MarkdownParser { |
| 6 | /** |
| 7 | * Parse Markdown text and convert it to HTML |
| 8 | * @param {string} markdown - The Markdown text to parse |
| 9 | * @returns {string} - The resulting HTML |
| 10 | */ |
| 11 | parse(markdown) { |
| 12 | const lines = markdown.split('\n'); |
| 13 | let html = ''; |
| 14 | let inList = false; |
| 15 | |
| 16 | for (const line of lines) { |
| 17 | const result = this.parseLine(line, inList); |
| 18 | html += result.html; |
| 19 | inList = result.inList; |
| 39 | 20 | } |
| 21 | |
| 22 | // Close any open list at the end |
| 23 | if (inList) { |
| 24 | html += '</ul>'; |
| 25 | } |
| 26 | |
| 27 | return html; |
| 40 | 28 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 43 | | } |
| 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]; |
| 29 | |
| 30 | /** |
| 31 | * Parse a single line of Markdown |
| 32 | * @param {string} line - The line to parse |
| 33 | * @param {boolean} inList - Whether we're currently in a list |
| 34 | * @returns {object} - Object with html and inList properties |
| 35 | */ |
| 36 | parseLine(line, inList) { |
| 37 | // Try to parse as header |
| 38 | const headerResult = this.parseHeader(line, inList); |
| 39 | if (headerResult.matched) { |
| 40 | return { html: headerResult.html, inList: headerResult.inList }; |
| 41 | } |
| 42 | |
| 43 | // Try to parse as list item |
| 44 | const listItemResult = this.parseListItem(line, inList); |
| 45 | if (listItemResult.matched) { |
| 46 | return { html: listItemResult.html, inList: listItemResult.inList }; |
| 47 | } |
| 48 | |
| 49 | // Parse as paragraph |
| 50 | const paragraphResult = this.parseParagraph(line, inList); |
| 51 | return { html: paragraphResult.html, inList: paragraphResult.inList }; |
| 50 | 52 | } |
| 51 | | } |
| 52 | | |
| 53 | | 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]; |
| 53 | |
| 54 | /** |
| 55 | * Parse a header (# Header) |
| 56 | * @param {string} line - The line to parse |
| 57 | * @param {boolean} inList - Whether we're currently in a list |
| 58 | * @returns {object} - Object with matched, html, and inList properties |
| 59 | */ |
| 60 | parseHeader(line, inList) { |
| 61 | // Count the number of # characters at the start of the line |
| 62 | let count = 0; |
| 63 | for (let i = 0; i < line.length; i++) { |
| 64 | if (line[i] === '#') { |
| 65 | count++; |
| 66 | } else { |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Headers must have 1-6 # characters |
| 72 | if (count === 0 || count > 6) { |
| 73 | return { matched: false }; |
| 60 | 74 | } |
| 75 | |
| 76 | // Extract the header text (skip the # characters and the following space) |
| 77 | const headerText = line.substring(count + 1); |
| 78 | const headerTag = `h${count}`; |
| 79 | const headerHtml = this.wrapHtml(headerText, headerTag); |
| 80 | |
| 81 | // If we were in a list, close it before the header |
| 82 | const html = inList ? `</ul>${headerHtml}` : headerHtml; |
| 83 | |
| 84 | return { matched: true, html, inList: false }; |
| 61 | 85 | } |
| 62 | | return [null, list]; |
| 63 | | } |
| 64 | | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 86 | |
| 87 | /** |
| 88 | * Parse a list item (* item) |
| 89 | * @param {string} line - The line to parse |
| 90 | * @param {boolean} inList - Whether we're currently in a list |
| 91 | * @returns {object} - Object with matched, html, and inList properties |
| 92 | */ |
| 93 | parseListItem(line, inList) { |
| 94 | // Check if the line starts with '* ' |
| 95 | if (!line.startsWith('* ')) { |
| 96 | return { matched: false }; |
| 97 | } |
| 98 | |
| 99 | // Extract the list item text (skip '* ') |
| 100 | const itemText = line.substring(2); |
| 101 | |
| 102 | // Parse any inline formatting in the item text |
| 103 | const parsedText = this.parseInlineFormatting(itemText); |
| 104 | |
| 105 | // Wrap the text in <li> tags |
| 106 | const itemHtml = this.wrapHtml(parsedText, 'li'); |
| 107 | |
| 108 | // If we weren't already in a list, start one |
| 109 | const html = inList ? itemHtml : `<ul>${itemHtml}`; |
| 110 | |
| 111 | return { matched: true, html, inList: true }; |
| 70 | 112 | } |
| 71 | | } |
| 72 | | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 113 | |
| 114 | /** |
| 115 | * Parse a paragraph |
| 116 | * @param {string} line - The line to parse |
| 117 | * @param {boolean} inList - Whether we're currently in a list |
| 118 | * @returns {object} - Object with html and inList properties |
| 119 | */ |
| 120 | parseParagraph(line, inList) { |
| 121 | // Parse any inline formatting in the paragraph text |
| 122 | const parsedText = this.parseInlineFormatting(line); |
| 123 | |
| 124 | // Wrap the text in <p> tags |
| 125 | const paragraphHtml = this.wrapHtml(parsedText, 'p'); |
| 126 | |
| 127 | // If we were in a list, close it before the paragraph |
| 128 | const html = inList ? `</ul>${paragraphHtml}` : paragraphHtml; |
| 129 | |
| 130 | return { html, inList: false }; |
| 77 | 131 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 132 | |
| 133 | /** |
| 134 | * Parse inline formatting (__bold__ and _italic_) |
| 135 | * @param {string} text - The text to parse |
| 136 | * @returns {string} - The text with inline formatting converted to HTML |
| 137 | */ |
| 138 | parseInlineFormatting(text) { |
| 139 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 140 | let result = text.replace(/__(.+)__/g, '<strong>$1</strong>'); |
| 141 | |
| 142 | // Parse italic text (_text_ -> <em>text</em>) |
| 143 | result = result.replace(/_(.+)_/g, '<em>$1</em>'); |
| 144 | |
| 145 | return result; |
| 80 | 146 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 147 | |
| 148 | /** |
| 149 | * Wrap text in HTML tags |
| 150 | * @param {string} text - The text to wrap |
| 151 | * @param {string} tag - The HTML tag to use |
| 152 | * @returns {string} - The wrapped HTML |
| 153 | */ |
| 154 | wrapHtml(text, tag) { |
| 155 | return `<${tag}>${text}</${tag}>`; |
| 83 | 156 | } |
| 84 | | return [result, inListAfter]; |
| 85 | 157 | } |
| 86 | 158 | |
| 159 | // Create a single instance of the parser |
| 160 | const parser = new MarkdownParser(); |
| 161 | |
| 87 | 162 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 163 | * Parse Markdown text and convert it to HTML |
| 164 | * @param {string} markdown - The Markdown text to parse |
| 165 | * @returns {string} - The resulting HTML |
| 90 | 166 | */ |
| 91 | 167 | export function parse(markdown) { |
| 92 | | 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; |
| 104 | | } |
| 168 | return parser.parse(markdown); |
| 105 | 169 | } |