| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 1 | /** |
| 2 | * Markdown Parser |
| 3 | * Parses Markdown syntax and converts it to HTML |
| 4 | * |
| 5 | * Refactored to improve readability and maintainability while preserving exact functionality |
| 6 | */ |
| 4 | 7 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | class MarkdownParser { |
| 9 | /** |
| 10 | * Wraps text with HTML tags |
| 11 | * @param {string} text - Text to wrap |
| 12 | * @param {string} tag - HTML tag to use |
| 13 | * @returns {string} Wrapped text |
| 14 | */ |
| 15 | wrap(text, tag) { |
| 16 | return `<${tag}>${text}</${tag}>`; |
| 17 | } |
| 8 | 18 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | | return markdown.replace(pattern, replacement); |
| 13 | | } |
| 19 | /** |
| 20 | * Checks if text starts with a specific HTML tag |
| 21 | * @param {string} text - Text to check |
| 22 | * @param {string} tag - HTML tag to look for |
| 23 | * @returns {boolean} True if text starts with the tag |
| 24 | */ |
| 25 | isTag(text, tag) { |
| 26 | return text.startsWith(`<${tag}>`); |
| 27 | } |
| 14 | 28 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 29 | /** |
| 30 | * Generic parser for text formatting |
| 31 | * @param {string} markdown - Markdown text |
| 32 | * @param {string} delimiter - Delimiter to look for |
| 33 | * @param {string} tag - HTML tag to replace with |
| 34 | * @returns {string} Parsed text |
| 35 | */ |
| 36 | parseWithDelimiter(markdown, delimiter, tag) { |
| 37 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 38 | const replacement = `<${tag}>$1</${tag}>`; |
| 39 | return markdown.replace(pattern, replacement); |
| 40 | } |
| 18 | 41 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 21 | | } |
| 42 | /** |
| 43 | * Parses strong text (__text__) |
| 44 | * @param {string} markdown - Markdown text |
| 45 | * @returns {string} Parsed strong text |
| 46 | */ |
| 47 | parseStrong(markdown) { |
| 48 | return this.parseWithDelimiter(markdown, '__', 'strong'); |
| 49 | } |
| 22 | 50 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 51 | /** |
| 52 | * Parses emphasis text (_text_) |
| 53 | * @param {string} markdown - Markdown text |
| 54 | * @returns {string} Parsed emphasis text |
| 55 | */ |
| 56 | parseEmphasis(markdown) { |
| 57 | return this.parseWithDelimiter(markdown, '_', 'em'); |
| 29 | 58 | } |
| 30 | | } |
| 31 | 59 | |
| 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; |
| 60 | /** |
| 61 | * Parses inline text formatting (strong and emphasis) |
| 62 | * @param {string} markdown - Markdown text |
| 63 | * @param {boolean} inList - Whether we're currently in a list |
| 64 | * @returns {string} Parsed text |
| 65 | */ |
| 66 | parseInlineText(markdown, inList) { |
| 67 | const parsedText = this.parseEmphasis(this.parseStrong(markdown)); |
| 68 | if (inList) { |
| 69 | return parsedText; |
| 37 | 70 | } else { |
| 38 | | break; |
| 71 | return this.wrap(parsedText, 'p'); |
| 39 | 72 | } |
| 40 | 73 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 43 | | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 47 | | return [`</ul>${headerHtml}`, false]; |
| 48 | | } else { |
| 49 | | return [headerHtml, false]; |
| 50 | | } |
| 51 | | } |
| 52 | 74 | |
| 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]; |
| 75 | /** |
| 76 | * Parses header text (# Header) |
| 77 | * @param {string} markdown - Markdown text |
| 78 | * @param {boolean} inList - Whether we're currently in a list |
| 79 | * @returns {[string|null, boolean]} Parsed header HTML and updated list state |
| 80 | */ |
| 81 | parseHeader(markdown, inList) { |
| 82 | let count = 0; |
| 83 | for (let i = 0; i < markdown.length; i++) { |
| 84 | if (markdown[i] === '#') { |
| 85 | count += 1; |
| 86 | } else { |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Headers must be between 1 and 6 # characters |
| 92 | if (count === 0 || count > 6) { |
| 93 | return [null, inList]; |
| 94 | } |
| 95 | |
| 96 | const headerTag = `h${count}`; |
| 97 | const headerContent = markdown.substring(count + 1); |
| 98 | const headerHtml = this.wrap(headerContent, headerTag); |
| 99 | |
| 100 | // If we were in a list, close it before the header |
| 101 | if (inList) { |
| 102 | return [`</ul>${headerHtml}`, false]; |
| 58 | 103 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 104 | return [headerHtml, false]; |
| 60 | 105 | } |
| 61 | 106 | } |
| 62 | | return [null, list]; |
| 63 | | } |
| 64 | 107 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 108 | /** |
| 109 | * Parses list items (* item) |
| 110 | * @param {string} markdown - Markdown text |
| 111 | * @param {boolean} inList - Whether we're currently in a list |
| 112 | * @returns {[string|null, boolean]} Parsed list item HTML and updated list state |
| 113 | */ |
| 114 | parseListItem(markdown, inList) { |
| 115 | if (markdown.startsWith('*')) { |
| 116 | const itemContent = markdown.substring(2); |
| 117 | const innerHtml = this.wrap(this.parseInlineText(itemContent, true), 'li'); |
| 118 | |
| 119 | // If we're already in a list, just add the item |
| 120 | // Otherwise, start a new list |
| 121 | if (inList) { |
| 122 | return [innerHtml, true]; |
| 123 | } else { |
| 124 | return [`<ul>${innerHtml}`, true]; |
| 125 | } |
| 126 | } |
| 127 | return [null, inList]; |
| 70 | 128 | } |
| 71 | | } |
| 72 | 129 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 130 | /** |
| 131 | * Parses paragraph text |
| 132 | * @param {string} markdown - Markdown text |
| 133 | * @param {boolean} inList - Whether we're currently in a list |
| 134 | * @returns {[string, boolean]} Parsed paragraph HTML and updated list state |
| 135 | */ |
| 136 | parseParagraph(markdown, inList) { |
| 137 | // If we were in a list, close it before the paragraph |
| 138 | if (inList) { |
| 139 | return [`</ul>${this.parseInlineText(markdown, false)}`, false]; |
| 140 | } else { |
| 141 | return [this.parseInlineText(markdown, false), false]; |
| 142 | } |
| 77 | 143 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 144 | |
| 145 | /** |
| 146 | * Parses a single line of markdown |
| 147 | * @param {string} markdown - Markdown text |
| 148 | * @param {boolean} inList - Whether we're currently in a list |
| 149 | * @returns {[string, boolean]} Parsed HTML and updated list state |
| 150 | */ |
| 151 | parseLine(markdown, inList) { |
| 152 | // Try to parse as header first |
| 153 | let [result, newListState] = this.parseHeader(markdown, inList); |
| 154 | if (result !== null) { |
| 155 | return [result, newListState]; |
| 156 | } |
| 157 | |
| 158 | // Try to parse as list item |
| 159 | [result, newListState] = this.parseListItem(markdown, inList); |
| 160 | if (result !== null) { |
| 161 | return [result, newListState]; |
| 162 | } |
| 163 | |
| 164 | // Default to paragraph |
| 165 | [result, newListState] = this.parseParagraph(markdown, inList); |
| 166 | if (result !== null) { |
| 167 | return [result, newListState]; |
| 168 | } |
| 169 | |
| 170 | // This should never happen with valid input |
| 171 | throw new Error('Unable to parse line: ' + markdown); |
| 80 | 172 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 173 | |
| 174 | /** |
| 175 | * Parses multi-line markdown text |
| 176 | * @param {string} markdown - Markdown text |
| 177 | * @returns {string} Parsed HTML |
| 178 | */ |
| 179 | parse(markdown) { |
| 180 | const lines = markdown.split('\n'); |
| 181 | let result = ''; |
| 182 | let inList = false; |
| 183 | |
| 184 | for (const line of lines) { |
| 185 | let [lineResult, newListState] = this.parseLine(line, inList); |
| 186 | result += lineResult; |
| 187 | inList = newListState; |
| 188 | } |
| 189 | |
| 190 | // Close any open list at the end |
| 191 | if (inList) { |
| 192 | result += '</ul>'; |
| 193 | } |
| 194 | |
| 195 | return result; |
| 83 | 196 | } |
| 84 | | return [result, inListAfter]; |
| 85 | 197 | } |
| 86 | 198 | |
| 199 | // Create a singleton instance for backward compatibility |
| 200 | const parser = new MarkdownParser(); |
| 201 | |
| 87 | 202 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 203 | * Parses Markdown syntax and converts it to HTML |
| 204 | * @param {string} markdown - Markdown text to parse |
| 205 | * @returns {string} HTML representation of the markdown |
| 90 | 206 | */ |
| 91 | 207 | export function parse(markdown) { |
| 92 | | 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; |
| 99 | | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 104 | | } |
| 208 | return parser.parse(markdown); |
| 105 | 209 | } |