| 12 | 12 | return markdown.replace(pattern, replacement); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | | function parse__(markdown) { |
| 15 | // Parse bold text (double underscore syntax) |
| 16 | function parseBold(markdown) { |
| 16 | 17 | return parser(markdown, '__', 'strong'); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | // Parse italic text (single underscore syntax) |
| 21 | function parseItalic(markdown) { |
| 20 | 22 | return parser(markdown, '_', 'em'); |
| 21 | 23 | } |
| 22 | 24 | |
| 25 | // Parse text content with emphasis (bold/italic) and wrap in paragraph if not in list |
| 23 | 26 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 27 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 28 | if (list) { |
| 26 | 29 | return parsedText; |
| 27 | 30 | } else { |
| 28 | 31 | return wrap(parsedText, 'p'); |
| 29 | 32 | } |
| 30 | | } |
| 33 | } |
| 34 | |
| 35 | // Maximum header level supported |
| 36 | const MAX_HEADER_LEVEL = 6; |
| 31 | 37 | |
| 38 | // Parse header markdown (#, ##, ###, etc.) and convert to HTML |
| 32 | 39 | function parseHeader(markdown, list) { |
| 33 | 40 | let count = 0; |
| 34 | 41 | for (let i = 0; i < markdown.length; i++) { |
| 50 | 62 | } |
| 51 | 63 | } |
| 52 | 64 | |
| 65 | // Parse list item markdown (starting with *) and convert to HTML |
| 53 | 66 | function parseLineItem(markdown, list) { |
| 54 | 67 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 68 | const itemContent = markdown.substring(2); // Remove "* " prefix |
| 69 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 70 | |
| 56 | 71 | if (list) { |
| 72 | // Continue existing list |
| 57 | 73 | return [innerHtml, true]; |
| 58 | 74 | } else { |
| 75 | // Start new list |
| 59 | 76 | return [`<ul>${innerHtml}`, true]; |
| 60 | 77 | } |
| 61 | 78 | } |
| 62 | 79 | return [null, list]; |
| 63 | 80 | } |
| 64 | 81 | |
| 82 | // Parse regular paragraph text and convert to HTML |
| 65 | 83 | function parseParagraph(markdown, list) { |
| 66 | 84 | if (!list) { |
| 85 | // Regular paragraph |
| 67 | 86 | return [parseText(markdown, false), false]; |
| 68 | 87 | } else { |
| 88 | // Close list and start paragraph |
| 69 | 89 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 90 | } |
| 71 | 91 | } |
| 72 | 92 | |
| 93 | // Parse a single line of markdown and convert to HTML |
| 73 | 94 | function parseLine(markdown, list) { |
| 95 | // Try to parse as header |
| 74 | 96 | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 97 | if (result !== null) { |
| 98 | return [result, inListAfter]; |
| 77 | 99 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 100 | |
| 101 | // Try to parse as list item |
| 102 | [result, inListAfter] = parseLineItem(markdown, list); |
| 103 | if (result !== null) { |
| 104 | return [result, inListAfter]; |
| 80 | 105 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 106 | |
| 107 | // Parse as regular paragraph (fallback) |
| 108 | [result, inListAfter] = parseParagraph(markdown, list); |
| 109 | if (result !== null) { |
| 110 | return [result, inListAfter]; |
| 83 | 111 | } |
| 84 | | return [result, inListAfter]; |
| 112 | |
| 113 | // This should never happen with valid input |
| 114 | throw new Error('Invalid markdown line'); |
| 85 | 115 | } |
| 86 | 116 | |
| 87 | 117 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 118 | * Parse markdown text and convert to HTML |
| 119 | * Supports headers (#, ##, ###, etc.), bold (__text__), italic (_text_), and lists (* item) |
| 120 | * @param {string} markdown - The markdown text to parse |
| 121 | * @returns {string} - The resulting HTML |
| 90 | 122 | */ |
| 91 | 123 | export function parse(markdown) { |
| 92 | 124 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 125 | let html = ''; |
| 126 | let inList = false; |
| 127 | |
| 128 | // Process each line of markdown |
| 95 | 129 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 130 | let [lineHtml, newListState] = parseLine(lines[i], inList); |
| 131 | html += lineHtml; |
| 132 | inList = newListState; |
| 99 | 133 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 134 | |
| 135 | // Close any open list at the end |
| 136 | if (inList) { |
| 137 | html += '</ul>'; |
| 104 | 138 | } |
| 139 | |
| 140 | return html; |
| 105 | 141 | } |