| 1 | // Helper functions for wrapping 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 tag |
| 5 | 7 | function isTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 11 | // Generic parser for inline elements (bold, italic) |
| 9 | 12 | function parser(markdown, delimiter, tag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 14 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 18 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 19 | function parseBold(markdown) { |
| 16 | 20 | return parser(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 23 | // Parse italic text (_text_ -> <em>text</em>) |
| 24 | function parseItalic(markdown) { |
| 20 | 25 | return parser(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 28 | // Parse text with inline formatting (bold, italic) |
| 29 | function parseInlineFormatting(markdown) { |
| 30 | return parseItalic(parseBold(markdown)); |
| 31 | } |
| 32 | |
| 33 | // Parse regular text, wrapping in paragraph tags if not in a list |
| 34 | function parseText(markdown, inList) { |
| 35 | const parsedText = parseInlineFormatting(markdown); |
| 36 | if (inList) { |
| 26 | 37 | return parsedText; |
| 27 | 38 | } else { |
| 28 | 39 | return wrap(parsedText, 'p'); |
| 29 | 40 | } |
| 30 | 41 | } |
| 31 | 42 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 43 | // Parse header markdown (# Header -> <h1>Header</h1>) |
| 44 | function parseHeader(markdown, inList) { |
| 45 | // Count consecutive # characters at the beginning |
| 46 | let hashCount = 0; |
| 34 | 47 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 48 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 49 | hashCount += 1; |
| 37 | 50 | } else { |
| 38 | 51 | break; |
| 39 | 52 | } |
| 40 | 53 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 54 | |
| 55 | // Headers must have 1-6 # characters |
| 56 | if (hashCount === 0 || hashCount > 6) { |
| 57 | return [null, inList]; |
| 43 | 58 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 59 | |
| 60 | // Create the appropriate header tag (h1-h6) |
| 61 | const headerTag = `h${hashCount}`; |
| 62 | const headerContent = markdown.substring(hashCount + 1); |
| 63 | const headerHtml = wrap(headerContent, headerTag); |
| 64 | |
| 65 | // If we were in a list, close it before the header |
| 66 | if (inList) { |
| 47 | 67 | return [`</ul>${headerHtml}`, false]; |
| 48 | 68 | } else { |
| 49 | 69 | return [headerHtml, false]; |
| 50 | 70 | } |
| 51 | 71 | } |
| 52 | 72 | |
| 53 | | function parseLineItem(markdown, list) { |
| 73 | // Parse list items (* item -> <li>item</li>) |
| 74 | function parseLineItem(markdown, inList) { |
| 54 | 75 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 76 | const itemContent = markdown.substring(2); // Skip '* ' prefix |
| 77 | const formattedContent = parseText(itemContent, true); // Parse inline formatting |
| 78 | const listItemHtml = wrap(formattedContent, 'li'); |
| 79 | |
| 80 | // If we're already in a list, just add the list item |
| 81 | // Otherwise, start a new unordered list |
| 82 | if (inList) { |
| 83 | return [listItemHtml, true]; |
| 58 | 84 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 85 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 86 | } |
| 61 | 87 | } |
| 62 | | return [null, list]; |
| 88 | return [null, inList]; |
| 63 | 89 | } |
| 64 | 90 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 91 | // Parse regular paragraphs (text -> <p>text</p>) |
| 92 | function parseParagraph(markdown, inList) { |
| 93 | const paragraphHtml = parseText(markdown, false); |
| 94 | |
| 95 | // If we were in a list, close it before the paragraph |
| 96 | if (inList) { |
| 97 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 98 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 99 | return [paragraphHtml, false]; |
| 70 | 100 | } |
| 71 | 101 | } |
| 72 | 102 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 103 | // Parse a single line of markdown, determining its type and converting to HTML |
| 104 | function parseLine(markdown, inList) { |
| 105 | // Try to parse as header first |
| 106 | let [result, newInList] = parseHeader(markdown, inList); |
| 107 | |
| 108 | // If not a header, try to parse as list item |
| 75 | 109 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 110 | [result, newInList] = parseLineItem(markdown, inList); |
| 77 | 111 | } |
| 112 | |
| 113 | // If not a list item, parse as paragraph |
| 78 | 114 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 115 | [result, newInList] = parseParagraph(markdown, inList); |
| 80 | 116 | } |
| 117 | |
| 118 | // This should never happen as one of the parsers should always succeed |
| 81 | 119 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 120 | throw new Error('Unable to parse markdown line'); |
| 83 | 121 | } |
| 84 | | return [result, inListAfter]; |
| 122 | |
| 123 | return [result, newInList]; |
| 85 | 124 | } |
| 86 | 125 | |
| 87 | 126 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 127 | * Parse markdown text and convert it to HTML |
| 128 | * @param {string} markdown - The markdown text to parse |
| 129 | * @returns {string} The equivalent HTML |
| 90 | 130 | */ |
| 91 | 131 | export function parse(markdown) { |
| 92 | 132 | 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; |
| 133 | let htmlResult = ''; |
| 134 | let inList = false; |
| 135 | |
| 136 | // Process each line of markdown |
| 137 | for (const line of lines) { |
| 138 | const [lineHtml, newInListState] = parseLine(line, inList); |
| 139 | htmlResult += lineHtml; |
| 140 | inList = newInListState; |
| 99 | 141 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 142 | |
| 143 | // If we're still in a list at the end, close it |
| 144 | if (inList) { |
| 145 | return htmlResult + '</ul>'; |
| 102 | 146 | } else { |
| 103 | | return result; |
| 147 | return htmlResult; |
| 104 | 148 | } |
| 105 | 149 | } |