| 1 | // REFACTORING SUMMARY: |
| 2 | // This markdown parser has been refactored to improve readability and maintainability: |
| 3 | // 1. Renamed functions with descriptive names: |
| 4 | // - parse__ -> parseStrongText (bold text with __) |
| 5 | // - parse_ -> parseEmphasizedText (italic text with _) |
| 6 | // - parseText -> parseInlineText (handles inline formatting) |
| 7 | // - parseHeader -> parseHeaderLine (parses # headers) |
| 8 | // - parseLineItem -> parseListItem (parses * list items) |
| 9 | // - parseParagraph -> parseParagraphLine (parses regular paragraphs) |
| 10 | // - parseLine -> parseMarkdownLine (main line parser) |
| 11 | // 2. Added comprehensive JSDoc documentation for all functions |
| 12 | // 3. Improved code organization with logical grouping |
| 13 | // 4. Added explanatory comments for complex logic |
| 14 | // 5. Maintained exact functionality while improving readability |
| 15 | |
| 16 | // REFACTORING: Utility functions for HTML tag manipulation |
| 17 | |
| 18 | /** |
| 19 | * Wraps text in an HTML tag |
| 20 | * @param {string} text - The text to wrap |
| 21 | * @param {string} tag - The HTML tag name |
| 22 | * @returns {string} The wrapped text |
| 23 | */ |
| 1 | 24 | function wrap(text, tag) { |
| 2 | 25 | return `<${tag}>${text}</${tag}>`; |
| 3 | 26 | } |
| 4 | 27 | |
| 28 | /** |
| 29 | * Checks if text starts with a specific HTML tag |
| 30 | * @param {string} text - The text to check |
| 31 | * @param {string} tag - The HTML tag name |
| 32 | * @returns {boolean} True if text starts with the tag |
| 33 | */ |
| 5 | 34 | function isTag(text, tag) { |
| 6 | 35 | return text.startsWith(`<${tag}>`); |
| 7 | 36 | } |
| 8 | 37 | |
| 38 | /** |
| 39 | * Generic parser for markdown delimiters (emphasis, strong, etc.) |
| 40 | * @param {string} markdown - The markdown text to parse |
| 41 | * @param {string} delimiter - The delimiter pattern (e.g., '_' or '__') |
| 42 | * @param {string} tag - The HTML tag to replace with (e.g., 'em' or 'strong') |
| 43 | * @returns {string} The parsed HTML |
| 44 | */ |
| 9 | 45 | function parser(markdown, delimiter, tag) { |
| 10 | 46 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 47 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 48 | return markdown.replace(pattern, replacement); |
| 13 | 49 | } |
| 14 | 50 | |
| 15 | | function parse__(markdown) { |
| 51 | /** |
| 52 | * Parses strong (bold) text marked with double underscores |
| 53 | * @param {string} markdown - The markdown text to parse |
| 54 | * @returns {string} The parsed HTML with strong tags |
| 55 | */ |
| 56 | function parseStrongText(markdown) { |
| 16 | 57 | return parser(markdown, '__', 'strong'); |
| 17 | 58 | } |
| 18 | 59 | |
| 19 | | function parse_(markdown) { |
| 60 | /** |
| 61 | * Parses emphasized (italic) text marked with single underscores |
| 62 | * @param {string} markdown - The markdown text to parse |
| 63 | * @returns {string} The parsed HTML with em tags |
| 64 | */ |
| 65 | function parseEmphasizedText(markdown) { |
| 20 | 66 | return parser(markdown, '_', 'em'); |
| 21 | 67 | } |
| 22 | 68 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 69 | /** |
| 70 | * Parses inline markdown elements (emphasis, strong) and wraps in paragraph tags if not in a list |
| 71 | * @param {string} markdown - The markdown text to parse |
| 72 | * @param {boolean} list - Whether we're currently in a list context |
| 73 | * @returns {string} The parsed HTML |
| 74 | */ |
| 75 | function parseInlineText(markdown, list) { |
| 76 | const parsedText = parseEmphasizedText(parseStrongText(markdown)); |
| 25 | 77 | if (list) { |
| 26 | 78 | return parsedText; |
| 27 | 79 | } else { |
| 50 | 108 | } |
| 51 | 109 | } |
| 52 | 110 | |
| 53 | | function parseLineItem(markdown, list) { |
| 111 | /** |
| 112 | * Parses list item markdown (lines starting with *) |
| 113 | * @param {string} markdown - The markdown line to parse |
| 114 | * @param {boolean} list - Whether we're currently in a list context |
| 115 | * @returns {[string|null, boolean]} Tuple of [parsed HTML or null, new list state] |
| 116 | */ |
| 117 | function parseListItem(markdown, list) { |
| 54 | 118 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 119 | const innerHtml = wrap(parseInlineText(markdown.substring(2), true), 'li'); |
| 56 | 120 | if (list) { |
| 57 | 121 | return [innerHtml, true]; |
| 58 | 122 | } else { |
| 62 | 126 | return [null, list]; |
| 63 | 127 | } |
| 64 | 128 | |
| 65 | | function parseParagraph(markdown, list) { |
| 129 | /** |
| 130 | * Parses paragraph markdown (regular text lines) |
| 131 | * @param {string} markdown - The markdown line to parse |
| 132 | * @param {boolean} list - Whether we're currently in a list context |
| 133 | * @returns {[string, boolean]} Tuple of [parsed HTML, new list state] |
| 134 | */ |
| 135 | function parseParagraphLine(markdown, list) { |
| 66 | 136 | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 137 | return [parseInlineText(markdown, false), false]; |
| 68 | 138 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 139 | return [`</ul>${parseInlineText(markdown, false)}`, false]; |
| 70 | 140 | } |
| 71 | 141 | } |
| 72 | 142 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 143 | /** |
| 144 | * Parses a single line of markdown by trying different parsers in order |
| 145 | * @param {string} markdown - The markdown line to parse |
| 146 | * @param {boolean} list - Whether we're currently in a list context |
| 147 | * @returns {[string, boolean]} Tuple of [parsed HTML, new list state] |
| 148 | */ |
| 149 | function parseMarkdownLine(markdown, list) { |
| 150 | let [result, inListAfter] = parseHeaderLine(markdown, list); |
| 75 | 151 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 152 | [result, inListAfter] = parseListItem(markdown, list); |
| 77 | 153 | } |
| 78 | 154 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 155 | [result, inListAfter] = parseParagraphLine(markdown, list); |
| 80 | 156 | } |
| 81 | 157 | if (result === null) { |
| 82 | 158 | throw new Error('Remove this line and implement the function'); |
| 85 | 161 | } |
| 86 | 162 | |
| 87 | 163 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 164 | * Main markdown parser function - converts markdown string to HTML |
| 165 | * REFACTORING: This is the main entry point that orchestrates the parsing process |
| 166 | * @param {string} markdown - The markdown string to parse |
| 167 | * @returns {string} The resulting HTML |
| 90 | 168 | */ |
| 91 | 169 | export function parse(markdown) { |
| 92 | 170 | const lines = markdown.split('\n'); |
| 93 | 171 | let result = ''; |
| 94 | 172 | let list = false; |
| 173 | |
| 174 | // Process each line, maintaining list state across lines |
| 95 | 175 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 176 | let [lineResult, newList] = parseMarkdownLine(lines[i], list); |
| 97 | 177 | result += lineResult; |
| 98 | 178 | list = newList; |
| 99 | 179 | } |
| 180 | |
| 181 | // Close any unclosed list |
| 100 | 182 | if (list) { |
| 101 | 183 | return result + '</ul>'; |
| 102 | 184 | } else { |