| 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) { |
| 11 | // Generic parser for markdown delimiters (bold, italic, etc.) |
| 12 | function parseWithDelimiter(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) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // Parse bold text using __ delimiter |
| 19 | function parseBold(markdown) { |
| 20 | return parseWithDelimiter(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 23 | // Parse italic text using _ delimiter |
| 24 | function parseItalic(markdown) { |
| 25 | return parseWithDelimiter(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 28 | // Parse text with inline formatting (bold, italic) and wrap in paragraph tags if not in a list |
| 29 | function parseText(markdown, isInList) { |
| 30 | // Apply inline formatting parsers |
| 31 | const formattedText = parseItalic(parseBold(markdown)); |
| 32 | |
| 33 | // If we're in a list, return text without paragraph tags |
| 34 | if (isInList) { |
| 35 | return formattedText; |
| 27 | 36 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 37 | // Otherwise wrap in paragraph tags |
| 38 | return wrap(formattedText, 'p'); |
| 29 | 39 | } |
| 30 | 40 | } |
| 31 | 41 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 42 | // Parse header markdown (# ## ### etc.) and return HTML with updated list state |
| 43 | function parseHeader(markdown, isInList) { |
| 44 | // Count consecutive # characters at the beginning of the line |
| 45 | let hashCount = 0; |
| 34 | 46 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 47 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 48 | hashCount += 1; |
| 37 | 49 | } else { |
| 38 | 50 | break; |
| 39 | 51 | } |
| 40 | 52 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 53 | |
| 54 | // Valid headers have 1-6 # characters |
| 55 | if (hashCount === 0 || hashCount > 6) { |
| 56 | return [null, isInList]; |
| 43 | 57 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 58 | |
| 59 | // Create appropriate header tag (h1, h2, etc.) |
| 60 | const headerTag = `h${hashCount}`; |
| 61 | const headerContent = markdown.substring(hashCount + 1); |
| 62 | const headerHtml = wrap(headerContent, headerTag); |
| 63 | |
| 64 | // If we were in a list, close it before the header |
| 65 | if (isInList) { |
| 47 | 66 | return [`</ul>${headerHtml}`, false]; |
| 48 | 67 | } else { |
| 49 | 68 | return [headerHtml, false]; |
| 50 | 69 | } |
| 51 | 70 | } |
| 52 | 71 | |
| 53 | | function parseLineItem(markdown, list) { |
| 72 | // Parse list item markdown (* item) and return HTML with updated list state |
| 73 | function parseLineItem(markdown, isInList) { |
| 74 | // Check if line starts with a list marker |
| 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); // true = we're in a list |
| 78 | const listItemHtml = wrap(formattedContent, 'li'); |
| 79 | |
| 80 | // If we're already in a list, just return the list item |
| 81 | if (isInList) { |
| 82 | return [listItemHtml, true]; |
| 58 | 83 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 84 | // Otherwise, start a new unordered list |
| 85 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 86 | } |
| 61 | 87 | } |
| 62 | | return [null, list]; |
| 88 | |
| 89 | // Not a list item |
| 90 | return [null, isInList]; |
| 63 | 91 | } |
| 64 | 92 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 93 | // Parse paragraph text and return HTML with updated list state |
| 94 | function parseParagraph(markdown, isInList) { |
| 95 | const paragraphHtml = parseText(markdown, false); // false = not in a list |
| 96 | |
| 97 | // If we were in a list, close it before the paragraph |
| 98 | if (isInList) { |
| 99 | return [`</ul>${paragraphHtml}`, false]; |
| 68 | 100 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 101 | return [paragraphHtml, false]; |
| 70 | 102 | } |
| 71 | 103 | } |
| 72 | 104 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 105 | // Parse a single line of markdown and return HTML with updated list state |
| 106 | function parseLine(markdown, isInList) { |
| 107 | // Try parsing as header first |
| 108 | let [result, newListState] = parseHeader(markdown, isInList); |
| 109 | |
| 110 | // If not a header, try parsing as list item |
| 75 | 111 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 112 | [result, newListState] = parseLineItem(markdown, isInList); |
| 77 | 113 | } |
| 114 | |
| 115 | // If not a list item, parse as paragraph |
| 78 | 116 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 117 | [result, newListState] = parseParagraph(markdown, isInList); |
| 80 | 118 | } |
| 119 | |
| 120 | // This should never happen with valid input |
| 81 | 121 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 122 | throw new Error('Unrecognized markdown syntax'); |
| 83 | 123 | } |
| 84 | | return [result, inListAfter]; |
| 124 | |
| 125 | return [result, newListState]; |
| 85 | 126 | } |
| 86 | 127 | |
| 87 | 128 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 129 | * Parse markdown text and convert to HTML |
| 130 | * @param {string} markdown - The markdown text to parse |
| 131 | * @returns {string} - The resulting HTML |
| 90 | 132 | */ |
| 91 | 133 | export function parse(markdown) { |
| 92 | 134 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 135 | let htmlResult = ''; |
| 136 | let isInList = false; // Track if we're currently inside an unordered list |
| 137 | |
| 138 | // Process each line of markdown |
| 95 | 139 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 140 | const [lineHtml, newListState] = parseLine(lines[i], isInList); |
| 141 | htmlResult += lineHtml; |
| 142 | isInList = newListState; |
| 99 | 143 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 144 | |
| 145 | // If we're still in a list at the end, close it |
| 146 | if (isInList) { |
| 147 | return htmlResult + '</ul>'; |
| 102 | 148 | } else { |
| 103 | | return result; |
| 149 | return htmlResult; |
| 104 | 150 | } |
| 105 | 151 | } |