| 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 | |
| 6 | // Helper function to check if text starts with a specific HTML tag |
| 5 | 7 | function isTag(text, tag) { |
| 6 | 8 | return text.startsWith(`<${tag}>`); |
| 7 | 9 | } |
| 8 | 10 | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 11 | // Generic parser for delimiter-based markdown (bold, italic, etc.) |
| 12 | function parseDelimitedText(markdown, delimiter, tag) { |
| 10 | 13 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 14 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 15 | return markdown.replace(pattern, replacement); |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 18 | // Parse bold text using __ delimiter |
| 19 | function parseBoldText(markdown) { |
| 20 | return parseDelimitedText(markdown, '__', 'strong'); |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 23 | // Parse italic text using _ delimiter |
| 24 | function parseItalicText(markdown) { |
| 25 | return parseDelimitedText(markdown, '_', 'em'); |
| 21 | 26 | } |
| 22 | 27 | |
| 28 | // Parse text with inline formatting (bold, italic) |
| 23 | 29 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 30 | const parsedText = parseItalicText(parseBoldText(markdown)); |
| 25 | 31 | if (list) { |
| 26 | 32 | return parsedText; |
| 27 | 33 | } else { |
| 38 | 46 | break; |
| 39 | 47 | } |
| 40 | 48 | } |
| 49 | |
| 50 | // Valid headers have 1-6 # characters |
| 41 | 51 | if (count === 0 || count > 6) { |
| 42 | 52 | return [null, list]; |
| 43 | 53 | } |
| 54 | |
| 55 | // Create appropriate header tag (h1, h2, h3, etc.) |
| 44 | 56 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 57 | const headerContent = markdown.substring(count + 1); // Skip # and space |
| 58 | const headerHtml = wrap(headerContent, headerTag); |
| 59 | |
| 60 | // Close any open list before starting header |
| 46 | 61 | if (list) { |
| 47 | 62 | return [`</ul>${headerHtml}`, false]; |
| 48 | 63 | } else { |
| 50 | 65 | } |
| 51 | 66 | } |
| 52 | 67 | |
| 68 | // Parse markdown list item (* item) |
| 53 | 69 | function parseLineItem(markdown, list) { |
| 54 | 70 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 71 | // Parse the content of the list item (skip '* ' prefix) |
| 72 | const listItemContent = markdown.substring(2); |
| 73 | const parsedContent = parseText(listItemContent, true); // true indicates we're in a list |
| 74 | const listItemHtml = wrap(parsedContent, 'li'); |
| 75 | |
| 76 | // Either continue existing list or start a new one |
| 56 | 77 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 78 | return [listItemHtml, true]; |
| 58 | 79 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 80 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 81 | } |
| 61 | 82 | } |
| 62 | 83 | return [null, list]; |
| 63 | 84 | } |
| 64 | 85 | |
| 86 | // Parse markdown paragraph (regular text) |
| 65 | 87 | function parseParagraph(markdown, list) { |
| 66 | 88 | if (!list) { |
| 89 | // Regular paragraph - just wrap in p tags |
| 67 | 90 | return [parseText(markdown, false), false]; |
| 68 | 91 | } else { |
| 92 | // Close any open list before starting paragraph |
| 69 | 93 | return [`</ul>${parseText(markdown, false)}`, false]; |
| 70 | 94 | } |
| 71 | 95 | } |
| 72 | 96 | |
| 97 | // Parse a single line of markdown by trying different parsers in order |
| 73 | 98 | function parseLine(markdown, list) { |
| 99 | // Try parsing as header first |
| 74 | 100 | let [result, inListAfter] = parseHeader(markdown, list); |
| 101 | |
| 102 | // If not a header, try parsing as list item |
| 75 | 103 | if (result === null) { |
| 76 | 104 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 105 | } |
| 106 | |
| 107 | // If not a list item, parse as paragraph |
| 78 | 108 | if (result === null) { |
| 79 | 109 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 110 | } |
| 111 | |
| 112 | // This should never happen with valid input |
| 81 | 113 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 114 | throw new Error('Invalid markdown line: ' + markdown); |
| 83 | 115 | } |
| 116 | |
| 84 | 117 | return [result, inListAfter]; |
| 85 | 118 | } |
| 86 | 119 | |
| 87 | 120 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 121 | * Parse markdown text and convert to HTML |
| 122 | * @param {string} markdown - The markdown text to parse |
| 123 | * @returns {string} The resulting HTML |
| 90 | 124 | */ |
| 91 | 125 | export function parse(markdown) { |
| 92 | 126 | const lines = markdown.split('\n'); |
| 93 | 127 | let result = ''; |
| 94 | | let list = false; |
| 128 | let inList = false; // Track whether we're currently inside a list |
| 129 | |
| 130 | // Process each line of markdown |
| 95 | 131 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 132 | const currentLine = lines[i]; |
| 133 | let [lineResult, newInList] = parseLine(currentLine, inList); |
| 134 | |
| 97 | 135 | result += lineResult; |
| 98 | | list = newList; |
| 136 | inList = newInList; |
| 99 | 137 | } |
| 100 | | if (list) { |
| 138 | |
| 139 | // Close any unclosed list at the end |
| 140 | if (inList) { |
| 101 | 141 | return result + '</ul>'; |
| 102 | 142 | } else { |
| 103 | 143 | return result; |