| 1 | | function wrap(text, tag) { |
| 2 | | return `<${tag}>${text}</${tag}>`; |
| 3 | | } |
| 4 | | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | | |
| 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 | | } |
| 14 | | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 21 | | } |
| 22 | | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 1 | /** |
| 2 | * A Markdown parser that converts Markdown syntax to HTML. |
| 3 | * |
| 4 | * Refactored to use a class-based approach for better organization and readability. |
| 5 | * The parser handles headers, lists, paragraphs, and text formatting (emphasis). |
| 6 | */ |
| 7 | class MarkdownParser { |
| 8 | constructor() { |
| 9 | // Constants for header levels |
| 10 | this.MAX_HEADER_LEVEL = 6; |
| 29 | 11 | } |
| 30 | | } |
| 31 | 12 | |
| 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; |
| 37 | | } else { |
| 38 | | break; |
| 13 | /** |
| 14 | * Main parsing method that converts markdown text to HTML |
| 15 | * @param {string} markdown - The markdown text to parse |
| 16 | * @returns {string} - The resulting HTML |
| 17 | */ |
| 18 | parse(markdown) { |
| 19 | const lines = markdown.split('\n'); |
| 20 | let html = ''; |
| 21 | let inList = false; |
| 22 | |
| 23 | for (const line of lines) { |
| 24 | const [lineHtml, newListState] = this.parseLine(line, inList); |
| 25 | html += lineHtml; |
| 26 | inList = newListState; |
| 39 | 27 | } |
| 28 | |
| 29 | // Close any open list at the end |
| 30 | if (inList) { |
| 31 | html += '</ul>'; |
| 32 | } |
| 33 | |
| 34 | return html; |
| 40 | 35 | } |
| 41 | | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 36 | |
| 37 | /** |
| 38 | * Parse a single line of markdown |
| 39 | * @param {string} line - A single line of markdown |
| 40 | * @param {boolean} inList - Whether we're currently in a list |
| 41 | * @returns {[string, boolean]} - [HTML for the line, new list state] |
| 42 | */ |
| 43 | parseLine(line, inList) { |
| 44 | // Try to parse as header first |
| 45 | const headerResult = this.parseHeader(line, inList); |
| 46 | if (headerResult[0] !== null) { |
| 47 | return headerResult; |
| 48 | } |
| 49 | |
| 50 | // Try to parse as list item |
| 51 | const listItemResult = this.parseListItem(line, inList); |
| 52 | if (listItemResult[0] !== null) { |
| 53 | return listItemResult; |
| 54 | } |
| 55 | |
| 56 | // Default to paragraph |
| 57 | return this.parseParagraph(line, inList); |
| 43 | 58 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 47 | | return [`</ul>${headerHtml}`, false]; |
| 48 | | } else { |
| 59 | |
| 60 | /** |
| 61 | * Parse header markdown (# Header) |
| 62 | * @param {string} line - Line to parse |
| 63 | * @param {boolean} inList - Current list state |
| 64 | * @returns {[string|null, boolean]} - [HTML or null if not a header, new list state] |
| 65 | */ |
| 66 | parseHeader(line, inList) { |
| 67 | // Count consecutive # characters at the start |
| 68 | let level = 0; |
| 69 | while (level < line.length && line[level] === '#') { |
| 70 | level++; |
| 71 | } |
| 72 | |
| 73 | // Valid headers have 1-6 # characters followed by a space |
| 74 | if (level === 0 || level > this.MAX_HEADER_LEVEL || line[level] !== ' ') { |
| 75 | return [null, inList]; |
| 76 | } |
| 77 | |
| 78 | const headerTag = `h${level}`; |
| 79 | const content = line.substring(level + 1); |
| 80 | const headerHtml = this.wrap(content, headerTag); |
| 81 | |
| 82 | // If we were in a list, close it before the header |
| 83 | if (inList) { |
| 84 | return [`</ul>${headerHtml}`, false]; |
| 85 | } |
| 86 | |
| 49 | 87 | return [headerHtml, false]; |
| 50 | 88 | } |
| 51 | | } |
| 52 | 89 | |
| 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]; |
| 90 | /** |
| 91 | * Parse list item markdown (* item) |
| 92 | * @param {string} line - Line to parse |
| 93 | * @param {boolean} inList - Current list state |
| 94 | * @returns {[string|null, boolean]} - [HTML or null if not a list item, new list state] |
| 95 | */ |
| 96 | parseListItem(line, inList) { |
| 97 | if (!line.startsWith('* ')) { |
| 98 | return [null, inList]; |
| 99 | } |
| 100 | |
| 101 | const content = line.substring(2); |
| 102 | const textHtml = this.parseText(content, true); |
| 103 | const listItemHtml = this.wrap(textHtml, 'li'); |
| 104 | |
| 105 | if (inList) { |
| 106 | // Continue existing list |
| 107 | return [listItemHtml, true]; |
| 58 | 108 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 109 | // Start new list |
| 110 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 111 | } |
| 61 | 112 | } |
| 62 | | return [null, list]; |
| 63 | | } |
| 64 | 113 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 114 | /** |
| 115 | * Parse paragraph (default case for any line) |
| 116 | * @param {string} line - Line to parse |
| 117 | * @param {boolean} inList - Current list state |
| 118 | * @returns {[string, boolean]} - [HTML for paragraph, new list state] |
| 119 | */ |
| 120 | parseParagraph(line, inList) { |
| 121 | const textHtml = this.parseText(line, false); |
| 122 | const paragraphHtml = this.wrap(textHtml, 'p'); |
| 123 | |
| 124 | // If we were in a list, close it before the paragraph |
| 125 | if (inList) { |
| 126 | return [`</ul>${paragraphHtml}`, false]; |
| 127 | } |
| 128 | |
| 129 | return [paragraphHtml, false]; |
| 70 | 130 | } |
| 71 | | } |
| 72 | 131 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 132 | /** |
| 133 | * Parse text with emphasis formatting (_ and __) |
| 134 | * @param {string} text - Text to parse |
| 135 | * @param {boolean} inList - Whether this text is inside a list item |
| 136 | * @returns {string} - HTML with emphasis tags |
| 137 | */ |
| 138 | parseText(text, inList) { |
| 139 | // Parse strong (__text__) then emphasis (_text_) |
| 140 | let parsedText = this.parseStrong(text); |
| 141 | parsedText = this.parseEmphasis(parsedText); |
| 142 | |
| 143 | // Return parsed text without wrapping - wrapping is handled by callers |
| 144 | return parsedText; |
| 77 | 145 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 146 | |
| 147 | /** |
| 148 | * Parse strong emphasis (__text__ -> <strong>text</strong>) |
| 149 | * @param {string} text - Text to parse |
| 150 | * @returns {string} - Text with strong tags |
| 151 | */ |
| 152 | parseStrong(text) { |
| 153 | return this.parser(text, '__', 'strong'); |
| 80 | 154 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 155 | |
| 156 | /** |
| 157 | * Parse emphasis (_text_ -> <em>text</em>) |
| 158 | * @param {string} text - Text to parse |
| 159 | * @returns {string} - Text with emphasis tags |
| 160 | */ |
| 161 | parseEmphasis(text) { |
| 162 | return this.parser(text, '_', 'em'); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Generic parser for text formatting with delimiters |
| 167 | * @param {string} text - Text to parse |
| 168 | * @param {string} delimiter - Delimiter to look for (e.g., '__', '_') |
| 169 | * @param {string} tag - HTML tag to wrap matches with |
| 170 | * @returns {string} - Text with replacements made |
| 171 | */ |
| 172 | parser(text, delimiter, tag) { |
| 173 | const pattern = new RegExp(`${delimiter}(.+?)${delimiter}`); |
| 174 | const replacement = `<${tag}>$1</${tag}>`; |
| 175 | return text.replace(pattern, replacement); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Wrap text in HTML tags |
| 180 | * @param {string} text - Text to wrap |
| 181 | * @param {string} tag - HTML tag to use |
| 182 | * @returns {string} - Wrapped text |
| 183 | */ |
| 184 | wrap(text, tag) { |
| 185 | return `<${tag}>${text}</${tag}>`; |
| 83 | 186 | } |
| 84 | | return [result, inListAfter]; |
| 85 | 187 | } |
| 86 | 188 | |
| 189 | // Create a single instance and export the parse function for backward compatibility |
| 190 | const markdownParser = new MarkdownParser(); |
| 191 | |
| 87 | 192 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 193 | * Parse markdown text and convert to HTML |
| 194 | * @param {string} markdown - The markdown text to parse |
| 195 | * @returns {string} - The resulting HTML |
| 90 | 196 | */ |
| 91 | 197 | 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 | | } |
| 198 | return markdownParser.parse(markdown); |
| 105 | 199 | } |