| 12 | 12 | return markdown.replace(pattern, replacement); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | | function parse__(markdown) { |
| 15 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 16 | function parseBold(markdown) { |
| 16 | 17 | return parser(markdown, '__', 'strong'); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | // Parse italic text (_text_ -> <em>text</em>) |
| 21 | function parseItalic(markdown) { |
| 20 | 22 | return parser(markdown, '_', 'em'); |
| 21 | 23 | } |
| 22 | 24 | |
| 25 | // Parse inline markdown elements (_ for italic, __ for bold) and wrap in paragraph tags if not in a 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 { |
| 29 | 32 | } |
| 30 | 33 | } |
| 31 | 34 | |
| 35 | // Parse markdown headers (# to ######) and handle list state transitions |
| 32 | 36 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 37 | // Count consecutive # characters at the start |
| 38 | let headerLevel = 0; |
| 34 | 39 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 40 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 41 | headerLevel += 1; |
| 37 | 42 | } else { |
| 38 | 43 | break; |
| 39 | 44 | } |
| 40 | 45 | } |
| 41 | | if (count === 0 || count > 6) { |
| 46 | |
| 47 | // Valid headers are # to ###### (1-6) |
| 48 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 49 | return [null, list]; |
| 43 | 50 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 51 | |
| 52 | const headerTag = `h${headerLevel}`; |
| 53 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 54 | const headerHtml = wrap(headerContent, headerTag); |
| 55 | |
| 56 | // If we were in a list, close it before the header |
| 46 | 57 | if (list) { |
| 47 | 58 | return [`</ul>${headerHtml}`, false]; |
| 48 | 59 | } else { |
| 50 | 61 | } |
| 51 | 62 | } |
| 52 | 63 | |
| 64 | // Parse list items (* text) and handle list state transitions |
| 53 | 65 | function parseLineItem(markdown, list) { |
| 54 | 66 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 67 | const itemContent = markdown.substring(2); // Remove '* ' prefix |
| 68 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 69 | |
| 70 | // If we're already in a list, just add the item |
| 71 | // Otherwise, start a new list |
| 56 | 72 | if (list) { |
| 57 | 73 | return [innerHtml, true]; |
| 58 | 74 | } else { |
| 62 | 78 | return [null, list]; |
| 63 | 79 | } |
| 64 | 80 | |
| 81 | // Parse regular paragraphs and handle list state transitions |
| 65 | 82 | function parseParagraph(markdown, list) { |
| 66 | 83 | if (!list) { |
| 84 | // Not in a list, just parse as a paragraph |
| 67 | 85 | return [parseText(markdown, false), false]; |
| 68 | 86 | } else { |
| 87 | // We were in a list, close it and start a paragraph |
| 69 | 88 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 89 | } |
| 71 | 90 | } |
| 72 | 91 | |
| 92 | // Parse a single line of markdown by trying different parsers in order |
| 93 | // Order: headers -> list items -> paragraphs |
| 73 | 94 | function parseLine(markdown, list) { |
| 95 | // Try parsing as header first |
| 74 | 96 | let [result, inListAfter] = parseHeader(markdown, list); |
| 97 | |
| 98 | // If not a header, try parsing as list item |
| 75 | 99 | if (result === null) { |
| 76 | 100 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 101 | } |
| 102 | |
| 103 | // If not a list item, parse as paragraph |
| 78 | 104 | if (result === null) { |
| 79 | 105 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 106 | } |
| 107 | |
| 108 | // This should never happen as one of the parsers should always match |
| 81 | 109 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 110 | throw new Error('Unable to parse markdown line'); |
| 83 | 111 | } |
| 112 | |
| 84 | 113 | return [result, inListAfter]; |
| 85 | 114 | } |
| 86 | 115 | |
| 87 | 116 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 117 | * Parse markdown text and convert it to HTML |
| 118 | * @param {string} markdown - The markdown text to parse |
| 119 | * @returns {string} - The resulting HTML |
| 90 | 120 | */ |
| 91 | 121 | export function parse(markdown) { |
| 92 | 122 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 123 | let htmlResult = ''; |
| 124 | let inList = false; // Track if we're currently inside a list |
| 125 | |
| 126 | // Process each line of markdown |
| 95 | 127 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 128 | let [lineHtml, newListState] = parseLine(lines[i], inList); |
| 129 | htmlResult += lineHtml; |
| 130 | inList = newListState; |
| 99 | 131 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 132 | |
| 133 | // If we're still in a list at the end, close it |
| 134 | if (inList) { |
| 135 | return htmlResult + '</ul>'; |
| 102 | 136 | } else { |
| 103 | | return result; |
| 137 | return htmlResult; |
| 104 | 138 | } |
| 105 | 139 | } |