| 1 | /** |
| 2 | * Wraps text content in HTML tags |
| 3 | * @param {string} text - The text content to wrap |
| 4 | * @param {string} tag - The HTML tag to use |
| 5 | * @returns {string} - The wrapped HTML content |
| 6 | */ |
| 1 | 7 | function wrap(text, tag) { |
| 2 | 8 | return `<${tag}>${text}</${tag}>`; |
| 3 | 9 | } |
| 4 | 10 | |
| 11 | /** |
| 12 | * Checks if text starts with a specific HTML tag |
| 13 | * @param {string} text - The text to check |
| 14 | * @param {string} tag - The HTML tag to look for |
| 15 | * @returns {boolean} - True if text starts with the tag |
| 16 | */ |
| 5 | 17 | function isTag(text, tag) { |
| 6 | 18 | return text.startsWith(`<${tag}>`); |
| 7 | 19 | } |
| 8 | 20 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 21 | /** |
| 22 | * Generic parser for markdown text formatting (bold, italic) |
| 23 | * @param {string} markdown - The markdown text to parse |
| 24 | * @param {string} delimiter - The markdown delimiter (e.g., '__', '_') |
| 25 | * @param {string} tag - The HTML tag to use (e.g., 'strong', 'em') |
| 26 | * @returns {string} - The HTML with formatting applied |
| 27 | */ |
| 28 | function parseTextFormatting(markdown, delimiter, tag) { |
| 10 | 29 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 30 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 31 | return markdown.replace(pattern, replacement); |
| 13 | 32 | } |
| 14 | 33 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 34 | /** |
| 35 | * Parses bold text marked with __ |
| 36 | * @param {string} markdown - The markdown text to parse |
| 37 | * @returns {string} - HTML with bold formatting |
| 38 | */ |
| 39 | function parseBoldText(markdown) { |
| 40 | return parseTextFormatting(markdown, '__', 'strong'); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Parses italic text marked with _ |
| 45 | * @param {string} markdown - The markdown text to parse |
| 46 | * @returns {string} - HTML with italic formatting |
| 47 | */ |
| 48 | function parseItalicText(markdown) { |
| 49 | return parseTextFormatting(markdown, '_', 'em'); |
| 17 | 50 | } |
| 18 | 51 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 52 | /** |
| 53 | * Parses text formatting (bold and italic) without paragraph wrapping |
| 54 | * @param {string} markdown - The markdown text to parse |
| 55 | * @returns {string} - HTML with formatting applied |
| 56 | */ |
| 57 | function parseTextFormattingOnly(markdown) { |
| 58 | return parseItalicText(parseBoldText(markdown)); |
| 21 | 59 | } |
| 22 | 60 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 61 | /** |
| 62 | * Parses text formatting (bold and italic) and optionally wraps in paragraph tags |
| 63 | * @param {string} markdown - The markdown text to parse |
| 64 | * @param {boolean} isInList - Whether this text is inside a list item |
| 65 | * @returns {string} - HTML with formatting applied, optionally wrapped in <p> tags |
| 66 | */ |
| 67 | function parseTextWithFormatting(markdown, isInList) { |
| 68 | const parsedText = parseTextFormattingOnly(markdown); |
| 69 | if (isInList) { |
| 26 | 70 | return parsedText; |
| 27 | 71 | } else { |
| 28 | 72 | return wrap(parsedText, 'p'); |
| 29 | 73 | } |
| 30 | 74 | } |
| 31 | 75 | |
| 32 | | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 76 | /** |
| 77 | * Parses markdown headers (lines starting with #) |
| 78 | * @param {string} markdown - The markdown line to parse |
| 79 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 80 | * @returns {Array} - [htmlResult, newListState] or [null, currentListState] if not a header |
| 81 | */ |
| 82 | function parseHeader(markdown, currentlyInList) { |
| 83 | // Count consecutive # symbols at the start of the line |
| 84 | let headerLevel = 0; |
| 34 | 85 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 86 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 87 | headerLevel += 1; |
| 37 | 88 | } else { |
| 38 | 89 | break; |
| 39 | 90 | } |
| 40 | 91 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 92 | |
| 93 | // Valid headers are h1-h6 (1-6 # symbols) |
| 94 | if (headerLevel === 0 || headerLevel > 6) { |
| 95 | return [null, currentlyInList]; |
| 43 | 96 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 97 | |
| 98 | const headerTag = `h${headerLevel}`; |
| 99 | const headerText = markdown.substring(headerLevel + 1); // Skip the # and space |
| 100 | const formattedHeaderText = parseTextFormattingOnly(headerText); |
| 101 | const headerHtml = wrap(formattedHeaderText, headerTag); |
| 102 | |
| 103 | // If we were in a list, close it before the header |
| 104 | if (currentlyInList) { |
| 47 | 105 | return [`</ul>${headerHtml}`, false]; |
| 48 | 106 | } else { |
| 49 | 107 | return [headerHtml, false]; |
| 50 | 108 | } |
| 51 | 109 | } |
| 52 | 110 | |
| 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]; |
| 60 | | } |
| 111 | /** |
| 112 | * Parses markdown list items (lines starting with *) |
| 113 | * @param {string} markdown - The markdown line to parse |
| 114 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 115 | * @returns {Array} - [htmlResult, newListState] or [null, currentListState] if not a list item |
| 116 | */ |
| 117 | function parseListItem(markdown, currentlyInList) { |
| 118 | if (!markdown.startsWith('*')) { |
| 119 | return [null, currentlyInList]; |
| 120 | } |
| 121 | |
| 122 | // Extract the list item text (skip the * and space) |
| 123 | const itemText = markdown.substring(2); |
| 124 | const listItemHtml = wrap(parseTextWithFormatting(itemText, true), 'li'); |
| 125 | |
| 126 | if (currentlyInList) { |
| 127 | // Already in a list, just add the item |
| 128 | return [listItemHtml, true]; |
| 129 | } else { |
| 130 | // Start a new list |
| 131 | return [`<ul>${listItemHtml}`, true]; |
| 61 | 132 | } |
| 62 | | return [null, list]; |
| 63 | 133 | } |
| 64 | 134 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 135 | /** |
| 136 | * Parses regular text as paragraphs |
| 137 | * @param {string} markdown - The markdown line to parse |
| 138 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 139 | * @returns {Array} - [htmlResult, newListState] |
| 140 | */ |
| 141 | function parseParagraph(markdown, currentlyInList) { |
| 142 | const parsedText = parseTextWithFormatting(markdown, false); |
| 143 | |
| 144 | if (!currentlyInList) { |
| 145 | // Not in a list, just return the paragraph |
| 146 | return [parsedText, false]; |
| 68 | 147 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 148 | // Close the list before starting the paragraph |
| 149 | return [`</ul>${parsedText}`, false]; |
| 70 | 150 | } |
| 71 | 151 | } |
| 72 | 152 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | | } |
| 153 | /** |
| 154 | * Parses a single line of markdown by trying different parsers in order |
| 155 | * @param {string} markdown - The markdown line to parse |
| 156 | * @param {boolean} currentlyInList - Whether we're currently inside a list |
| 157 | * @returns {Array} - [htmlResult, newListState] |
| 158 | */ |
| 159 | function parseLine(markdown, currentlyInList) { |
| 160 | // Try to parse as header first |
| 161 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 162 | |
| 163 | // If not a header, try to parse as list item |
| 78 | 164 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 165 | [result, newListState] = parseListItem(markdown, currentlyInList); |
| 80 | 166 | } |
| 167 | |
| 168 | // If not a list item, parse as paragraph |
| 81 | 169 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 170 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 83 | 171 | } |
| 84 | | return [result, inListAfter]; |
| 172 | |
| 173 | return [result, newListState]; |
| 85 | 174 | } |
| 86 | 175 | |
| 87 | 176 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 177 | * Main entry point for parsing markdown text to HTML |
| 178 | * @param {string} markdown - The markdown text to parse |
| 179 | * @returns {string} - The resulting HTML |
| 90 | 180 | */ |
| 91 | 181 | export function parse(markdown) { |
| 182 | // Split the markdown into individual lines |
| 92 | 183 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 184 | let htmlResult = ''; |
| 185 | let isInList = false; |
| 186 | |
| 187 | // Process each line |
| 95 | 188 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 189 | let [lineHtml, newListState] = parseLine(lines[i], isInList); |
| 190 | htmlResult += lineHtml; |
| 191 | isInList = newListState; |
| 99 | 192 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 193 | |
| 194 | // Close any open list at the end |
| 195 | if (isInList) { |
| 196 | htmlResult += '</ul>'; |
| 104 | 197 | } |
| 198 | |
| 199 | return htmlResult; |
| 105 | 200 | } |