| 1 | // REFACTORED: Added documentation comment |
| 2 | // Utility function to wrap text in HTML tags |
| 1 | 3 | function wrap(text, tag) { |
| 2 | 4 | return `<${tag}>${text}</${tag}>`; |
| 3 | 5 | } |
| 4 | 6 | |
| 7 | // REFACTORED: Added documentation comment |
| 8 | // Utility function to check if text starts with a specific HTML tag |
| 5 | 9 | function isTag(text, tag) { |
| 6 | 10 | return text.startsWith(`<${tag}>`); |
| 7 | 11 | } |
| 8 | 12 | |
| 13 | // REFACTORED: Added documentation comment |
| 14 | // Generic parser function to convert markdown delimiters to HTML tags |
| 9 | 15 | function parser(markdown, delimiter, tag) { |
| 10 | 16 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 17 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 18 | return markdown.replace(pattern, replacement); |
| 13 | 19 | } |
| 14 | 20 | |
| 15 | | function parse__(markdown) { |
| 21 | // REFACTORED: Renamed from parse__ to parseBold for clarity |
| 22 | function parseBold(markdown) { |
| 16 | 23 | return parser(markdown, '__', 'strong'); |
| 17 | 24 | } |
| 18 | 25 | |
| 19 | | function parse_(markdown) { |
| 26 | // REFACTORED: Renamed from parse_ to parseItalic for clarity |
| 27 | function parseItalic(markdown) { |
| 20 | 28 | return parser(markdown, '_', 'em'); |
| 21 | 29 | } |
| 22 | 30 | |
| 31 | // REFACTORED: Updated to use clearer function names and improved structure |
| 23 | 32 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 33 | // Parse bold and italic markdown |
| 34 | const parsedText = parseItalic(parseBold(markdown)); |
| 25 | 35 | if (list) { |
| 26 | 36 | return parsedText; |
| 27 | 37 | } else { |
| 38 | 50 | break; |
| 39 | 51 | } |
| 40 | 52 | } |
| 53 | |
| 54 | // Headers must have 1-6 # characters |
| 41 | 55 | if (count === 0 || count > 6) { |
| 42 | 56 | return [null, list]; |
| 43 | 57 | } |
| 58 | |
| 59 | // Create the appropriate header tag (h1-h6) |
| 44 | 60 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 61 | const headerContent = markdown.substring(count + 1); |
| 62 | const headerHtml = wrap(headerContent, headerTag); |
| 63 | |
| 64 | // If we're currently in a list, close it before the header |
| 46 | 65 | if (list) { |
| 47 | 66 | return [`</ul>${headerHtml}`, false]; |
| 48 | 67 | } else { |
| 50 | 69 | } |
| 51 | 70 | } |
| 52 | 71 | |
| 72 | // REFACTORED: Improved readability and added comments |
| 53 | 73 | function parseLineItem(markdown, list) { |
| 74 | // Check if this line is a list item (starts with *) |
| 54 | 75 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 76 | // Parse the content of the list item (skip '* ' prefix) |
| 77 | const itemContent = parseText(markdown.substring(2), true); |
| 78 | const innerHtml = wrap(itemContent, 'li'); |
| 79 | |
| 80 | // If we're already in a list, just add the item |
| 81 | // Otherwise, start a new list |
| 56 | 82 | if (list) { |
| 57 | 83 | return [innerHtml, true]; |
| 58 | 84 | } else { |
| 62 | 88 | return [null, list]; |
| 63 | 89 | } |
| 64 | 90 | |
| 91 | // REFACTORED: Improved readability and added comments |
| 65 | 92 | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 93 | // Parse as regular paragraph text |
| 94 | const paragraphText = parseText(markdown, false); |
| 95 | |
| 96 | // If we're currently in a list, close it before the paragraph |
| 97 | if (list) { |
| 98 | return [`</ul>${paragraphText}`, false]; |
| 68 | 99 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 100 | return [paragraphText, false]; |
| 70 | 101 | } |
| 71 | 102 | } |
| 72 | 103 | |
| 104 | // REFACTORED: Replaced chain of if statements with more elegant pattern matching |
| 73 | 105 | function parseLine(markdown, list) { |
| 106 | // Try to parse as header first |
| 74 | 107 | let [result, inListAfter] = parseHeader(markdown, list); |
| 108 | |
| 109 | // If not a header, try to parse as list item |
| 75 | 110 | if (result === null) { |
| 76 | 111 | [result, inListAfter] = parseLineItem(markdown, list); |
| 77 | 112 | } |
| 113 | |
| 114 | // If not a list item, parse as paragraph (default case) |
| 78 | 115 | if (result === null) { |
| 79 | 116 | [result, inListAfter] = parseParagraph(markdown, list); |
| 80 | 117 | } |
| 118 | |
| 119 | // This should never happen since parseParagraph always returns a result |
| 81 | 120 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 121 | throw new Error('Unable to parse markdown line'); |
| 83 | 122 | } |
| 123 | |
| 84 | 124 | return [result, inListAfter]; |
| 85 | 125 | } |
| 86 | 126 | |
| 87 | 127 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 128 | * Parse markdown text and convert it to HTML |
| 129 | * @param {string} markdown - The markdown text to parse |
| 130 | * @returns {string} - The resulting HTML |
| 90 | 131 | */ |
| 91 | 132 | export function parse(markdown) { |
| 133 | // Split the markdown into individual lines |
| 92 | 134 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 135 | let htmlResult = ''; |
| 136 | let inList = false; // Track if we're currently inside a list |
| 137 | |
| 138 | // Process each line of the markdown |
| 95 | 139 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 140 | // Parse the current line and get the result and updated list state |
| 141 | let [lineHtml, newListState] = parseLine(lines[i], inList); |
| 142 | htmlResult += lineHtml; |
| 143 | inList = newListState; |
| 99 | 144 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 145 | |
| 146 | // If we're still in a list at the end, close it |
| 147 | if (inList) { |
| 148 | return htmlResult + '</ul>'; |
| 102 | 149 | } else { |
| 103 | | return result; |
| 150 | return htmlResult; |
| 104 | 151 | } |
| 105 | 152 | } |