| 1 | // Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 6 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 7 | // Uses greedy matching to preserve original behavior exactly |
| 8 | function parseBold(markdown) { |
| 9 | return markdown.replace(/__(.+)__/g, '<strong>$1</strong>'); |
| 7 | 10 | } |
| 8 | 11 | |
| 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); |
| 12 | // Parse italic text (_text_ -> <em>text</em>) |
| 13 | // Uses greedy matching to preserve original behavior exactly |
| 14 | function parseItalic(markdown) { |
| 15 | return markdown.replace(/_(.+)_/g, '<em>$1</em>'); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // Parse inline formatting (bold and italic) |
| 19 | function parseInlineFormatting(markdown) { |
| 20 | // Apply bold formatting first, then italic |
| 21 | return parseItalic(parseBold(markdown)); |
| 17 | 22 | } |
| 18 | 23 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 24 | // Parse text with optional paragraph wrapping |
| 25 | function parseText(markdown, isListItem = false) { |
| 26 | const parsedText = parseInlineFormatting(markdown); |
| 27 | // Only wrap in <p> tags if not part of a list item |
| 28 | return isListItem ? parsedText : wrap(parsedText, 'p'); |
| 21 | 29 | } |
| 22 | 30 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 30 | | } |
| 31 | | |
| 32 | | function parseHeader(markdown, list) { |
| 31 | // Parse header elements (# Header -> <h1>Header</h1>) |
| 32 | function parseHeader(markdown, currentlyInList) { |
| 33 | // Count consecutive # characters at the start of the line |
| 33 | 34 | let count = 0; |
| 34 | 35 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 36 | if (markdown[i] === '#') { |
| 38 | 39 | break; |
| 39 | 40 | } |
| 40 | 41 | } |
| 42 | |
| 43 | // Valid headers have 1-6 # characters |
| 41 | 44 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 45 | return [null, currentlyInList]; |
| 43 | 46 | } |
| 47 | |
| 48 | // Extract header content (text after # characters and space) |
| 49 | const headerContent = markdown.substring(count + 1); |
| 44 | 50 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 51 | const headerHtml = wrap(headerContent, headerTag); |
| 52 | |
| 53 | // If we were in a list, close it before the header |
| 54 | if (currentlyInList) { |
| 47 | 55 | return [`</ul>${headerHtml}`, false]; |
| 48 | 56 | } else { |
| 49 | 57 | return [headerHtml, false]; |
| 50 | 58 | } |
| 51 | 59 | } |
| 52 | 60 | |
| 53 | | function parseLineItem(markdown, list) { |
| 61 | // Parse list items (* item -> <li>item</li>) |
| 62 | function parseLineItem(markdown, currentlyInList) { |
| 54 | 63 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 64 | // Extract list item content (text after '* ') |
| 65 | const itemContent = markdown.substring(2); |
| 66 | const innerHtml = wrap(parseText(itemContent, true), 'li'); |
| 67 | |
| 68 | if (currentlyInList) { |
| 69 | // Continue existing list |
| 57 | 70 | return [innerHtml, true]; |
| 58 | 71 | } else { |
| 72 | // Start new list |
| 59 | 73 | return [`<ul>${innerHtml}`, true]; |
| 60 | 74 | } |
| 61 | 75 | } |
| 62 | | return [null, list]; |
| 76 | return [null, currentlyInList]; |
| 63 | 77 | } |
| 64 | 78 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | | } else { |
| 79 | // Parse paragraph elements |
| 80 | function parseParagraph(markdown, currentlyInList) { |
| 81 | if (currentlyInList) { |
| 82 | // If we were in a list, close it before starting a paragraph |
| 69 | 83 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 84 | } else { |
| 85 | // Regular paragraph |
| 86 | return [parseText(markdown, false), false]; |
| 70 | 87 | } |
| 71 | 88 | } |
| 72 | 89 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 90 | // Parse a single line of markdown |
| 91 | function parseLine(markdown, currentlyInList) { |
| 92 | // Try parsing as header first |
| 93 | let [result, newListState] = parseHeader(markdown, currentlyInList); |
| 94 | |
| 95 | // If not a header, try parsing as list item |
| 75 | 96 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 97 | [result, newListState] = parseLineItem(markdown, currentlyInList); |
| 77 | 98 | } |
| 99 | |
| 100 | // If not a list item, parse as regular paragraph |
| 78 | 101 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 102 | [result, newListState] = parseParagraph(markdown, currentlyInList); |
| 80 | 103 | } |
| 104 | |
| 105 | // This should never happen with valid input |
| 81 | 106 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 107 | throw new Error('Invalid markdown line'); |
| 83 | 108 | } |
| 84 | | return [result, inListAfter]; |
| 109 | |
| 110 | return [result, newListState]; |
| 85 | 111 | } |
| 86 | 112 | |
| 87 | 113 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 114 | * Parse markdown text and convert it to HTML |
| 115 | * @param {string} markdown - The markdown text to parse |
| 116 | * @returns {string} - The resulting HTML |
| 90 | 117 | */ |
| 91 | 118 | export function parse(markdown) { |
| 92 | 119 | const lines = markdown.split('\n'); |
| 93 | 120 | let result = ''; |
| 94 | | let list = false; |
| 121 | let currentlyInList = false; |
| 122 | |
| 123 | // Process each line of markdown |
| 95 | 124 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 125 | let [lineResult, newListState] = parseLine(lines[i], currentlyInList); |
| 97 | 126 | result += lineResult; |
| 98 | | list = newList; |
| 127 | currentlyInList = newListState; |
| 99 | 128 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 129 | |
| 130 | // Close any open list at the end of the document |
| 131 | if (currentlyInList) { |
| 132 | result += '</ul>'; |
| 104 | 133 | } |
| 134 | |
| 135 | return result; |
| 105 | 136 | } |