| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 1 | /** |
| 2 | * REFACTORING NOTES: |
| 3 | * This markdown parser has been refactored for improved readability and maintainability. |
| 4 | * Key improvements made: |
| 5 | * 1. Renamed functions to be more descriptive (parseLineItem -> parseListItem, parse__ -> -> parseBold, parse_ -> -> parseItalic) |
| 6 | * 2. Added comprehensive JSDoc documentation for all functions |
| 7 | * 3. Improved variable names for clarity (list -> currentlyInList, markdown -> markdownLine, result -> htmlResult) |
| 8 | * 4. Added inline comments to explain complex logic |
| 9 | * 5. Removed unused code (isTag function) |
| 10 | * 6. Improved code structure and flow |
| 11 | * 7. Used more descriptive parameter names |
| 12 | * |
| 13 | * All functionality has been preserved - the parser produces identical output to the original. |
| 14 | */ |
| 4 | 15 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 16 | /** |
| 17 | * Wrap text content in HTML tags |
| 18 | * @param {string} content - The content to wrap |
| 19 | * @param {string} tagName - The HTML tag name (e.g., 'p', 'h1', 'li') |
| 20 | * @returns {string} - The content wrapped in HTML tags |
| 21 | */ |
| 22 | function wrap(content, tagName) { |
| 23 | return `<${tagName}>${content}</${tagName}>`; |
| 7 | 24 | } |
| 8 | 25 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 26 | /** |
| 27 | * Generic function to wrap text with HTML tags when surrounded by delimiters |
| 28 | * @param {string} text - The text to parse |
| 29 | * @param {string} delimiter - The delimiter to look for (e.g., '__' for bold) |
| 30 | * @param {string} tagName - The HTML tag to wrap with (e.g., 'strong' for bold) |
| 31 | * @returns {string} - The text with delimiters replaced by HTML tags |
| 32 | */ |
| 33 | function parser(text, delimiter, tagName) { |
| 10 | 34 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 35 | const replacement = `<${tagName}>$1</${tagName}>`; |
| 36 | return text.replace(pattern, replacement); |
| 13 | 37 | } |
| 14 | 38 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 39 | /** |
| 40 | * Parse bold text surrounded by __ delimiters |
| 41 | * @param {string} text - The text to parse |
| 42 | * @returns {string} - The text with __text__ converted to <strong>text</strong> |
| 43 | */ |
| 44 | function parseBold(text) { |
| 45 | return parser(text, '__', 'strong'); |
| 17 | 46 | } |
| 18 | 47 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 48 | /** |
| 49 | * Parse italic text surrounded by _ delimiters |
| 50 | * @param {string} text - The text to parse |
| 51 | * @returns {string} - The text with _text_ converted to <em>text</em> |
| 52 | */ |
| 53 | function parseItalic(text) { |
| 54 | return parser(text, '_', 'em'); |
| 21 | 55 | } |
| 22 | 56 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 57 | /** |
| 58 | * Parse inline markdown formatting (__ for bold, _ for italic) in text |
| 59 | * @param {string} text - The text to parse |
| 60 | * @param {boolean} inList - Whether the text is inside a list item |
| 61 | * @returns {string} - The text with inline formatting converted to HTML |
| 62 | */ |
| 63 | function parseText(text, inList) { |
| 64 | // Parse bold (__text__) and italic (_text_) formatting |
| 65 | const formattedText = parseItalic(parseBold(text)); |
| 66 | |
| 67 | // If we're in a list, don't wrap in paragraph tags |
| 68 | if (inList) { |
| 69 | return formattedText; |
| 27 | 70 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 71 | return wrap(formattedText, 'p'); |
| 29 | 72 | } |
| 30 | 73 | } |
| 31 | 74 | |
| 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; |
| 75 | /** |
| 76 | * Parse a markdown header (starts with #) |
| 77 | * @param {string} markdownLine - The markdown line to parse |
| 78 | * @param {boolean} currentlyInList - Whether we are currently inside a list |
| 79 | * @returns {Array} - [htmlResult, newInListState] or [null, currentlyInList] if not a header |
| 80 | */ |
| 81 | function parseHeader(markdownLine, currentlyInList) { |
| 82 | // Count the number of consecutive # characters at the start |
| 83 | let headerLevel = 0; |
| 84 | for (let i = 0; i < markdownLine.length; i++) { |
| 85 | if (markdownLine[i] === '#') { |
| 86 | headerLevel += 1; |
| 37 | 87 | } else { |
| 38 | 88 | break; |
| 39 | 89 | } |
| 40 | 90 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 91 | |
| 92 | // Headers must be between 1 and 6 # characters |
| 93 | if (headerLevel === 0 || headerLevel > 6) { |
| 94 | return [null, currentlyInList]; |
| 43 | 95 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 96 | |
| 97 | // Create the header tag (h1, h2, etc.) |
| 98 | const headerTag = `h${headerLevel}`; |
| 99 | // Remove the # characters and the space, then wrap in header tag |
| 100 | const headerContent = markdownLine.substring(headerLevel + 1); |
| 101 | const headerHtml = wrap(headerContent, headerTag); |
| 102 | |
| 103 | // If we were in a list, close it before starting 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]; |
| 111 | /** |
| 112 | * Parse a markdown list item (starts with *) |
| 113 | * @param {string} markdownLine - The markdown line to parse |
| 114 | * @param {boolean} currentlyInList - Whether we are currently inside a list |
| 115 | * @returns {Array} - [htmlResult, newInListState] or [null, currentlyInList] if not a list item |
| 116 | */ |
| 117 | function parseListItem(markdownLine, currentlyInList) { |
| 118 | if (markdownLine.startsWith('*')) { |
| 119 | // Remove the '* ' prefix and parse the remaining text |
| 120 | const itemText = markdownLine.substring(2); |
| 121 | const listItemHtml = wrap(parseText(itemText, true), 'li'); |
| 122 | |
| 123 | if (currentlyInList) { |
| 124 | // Already in a list, just add the list item |
| 125 | return [listItemHtml, true]; |
| 58 | 126 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 127 | // Start a new list |
| 128 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 129 | } |
| 61 | 130 | } |
| 62 | | return [null, list]; |
| 131 | |
| 132 | // Not a list item |
| 133 | return [null, currentlyInList]; |
| 63 | 134 | } |
| 64 | 135 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 136 | /** |
| 137 | * Parse a markdown paragraph (default case when not header or list item) |
| 138 | * @param {string} markdownLine - The markdown line to parse |
| 139 | * @param {boolean} currentlyInList - Whether we are currently inside a list |
| 140 | * @returns {Array} - [htmlResult, newInListState] |
| 141 | */ |
| 142 | function parseParagraph(markdownLine, currentlyInList) { |
| 143 | if (!currentlyInList) { |
| 144 | // Not in a list, wrap in paragraph tags |
| 145 | return [parseText(markdownLine, false), false]; |
| 68 | 146 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 147 | // We were in a list, so close it and start a paragraph |
| 148 | return [`</ul>${parseText(markdownLine, false)}`, false]; |
| 70 | 149 | } |
| 71 | 150 | } |
| 72 | 151 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 152 | /** |
| 153 | * Parse a single line of markdown and return HTML with list state |
| 154 | * @param {string} markdownLine - The markdown line to parse |
| 155 | * @param {boolean} currentlyInList - Whether we are currently inside a list |
| 156 | * @returns {Array} - [htmlResult, newInListState] |
| 157 | */ |
| 158 | function parseLine(markdownLine, currentlyInList) { |
| 159 | // Try to parse as header first |
| 160 | let [htmlResult, newInListState] = parseHeader(markdownLine, currentlyInList); |
| 161 | |
| 162 | // If not a header, try to parse as list item |
| 163 | if (htmlResult === null) { |
| 164 | [htmlResult, newInListState] = parseListItem(markdownLine, currentlyInList); |
| 77 | 165 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 166 | |
| 167 | // If not a list item, parse as paragraph |
| 168 | if (htmlResult === null) { |
| 169 | [htmlResult, newInListState] = parseParagraph(markdownLine, currentlyInList); |
| 80 | 170 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 171 | |
| 172 | // This should never happen as one of the parsers should always match |
| 173 | if (htmlResult === null) { |
| 174 | throw new Error('Unable to parse markdown line'); |
| 83 | 175 | } |
| 84 | | return [result, inListAfter]; |
| 176 | |
| 177 | return [htmlResult, newInListState]; |
| 85 | 178 | } |
| 86 | 179 | |
| 87 | 180 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 181 | * Parse markdown text and convert it to HTML |
| 182 | * @param {string} markdown - The markdown text to parse |
| 183 | * @returns {string} - The resulting HTML |
| 90 | 184 | */ |
| 91 | 185 | export function parse(markdown) { |
| 92 | 186 | 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; |
| 187 | let html = ''; |
| 188 | let inList = false; |
| 189 | |
| 190 | for (const line of lines) { |
| 191 | let [lineHtml, newListState] = parseLine(line, inList); |
| 192 | html += lineHtml; |
| 193 | inList = newListState; |
| 99 | 194 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 195 | |
| 196 | // Close any open list tag |
| 197 | if (inList) { |
| 198 | html += '</ul>'; |
| 104 | 199 | } |
| 200 | |
| 201 | return html; |
| 105 | 202 | } |