| 12 | 12 | return markdown.replace(pattern, replacement); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | | function parse__(markdown) { |
| 15 | // REFACTORED: Renamed from parse__ to parseBold for clarity |
| 16 | function parseBold(markdown) { |
| 16 | 17 | return parser(markdown, '__', 'strong'); |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | | function parse_(markdown) { |
| 20 | // REFACTORED: Renamed from parse_ to parseItalic for clarity |
| 21 | function parseItalic(markdown) { |
| 20 | 22 | return parser(markdown, '_', 'em'); |
| 21 | 23 | } |
| 22 | 24 | |
| 25 | // REFACTORED: Improved function to use renamed functions and clearer variable names |
| 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 | // REFACTORED: Improved variable names and added comments for clarity |
| 32 | 36 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 37 | let headerLevel = 0; |
| 38 | |
| 39 | // Count consecutive # characters to determine header level |
| 34 | 40 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 41 | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 42 | headerLevel += 1; |
| 37 | 43 | } else { |
| 38 | 44 | break; |
| 39 | 45 | } |
| 40 | 46 | } |
| 41 | | if (count === 0 || count > 6) { |
| 47 | |
| 48 | // Invalid header if no # or more than 6 # characters |
| 49 | if (headerLevel === 0 || headerLevel > 6) { |
| 42 | 50 | return [null, list]; |
| 43 | 51 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 52 | |
| 53 | const headerTag = `h${headerLevel}`; |
| 54 | const headerContent = markdown.substring(headerLevel + 1).trim(); |
| 55 | const headerHtml = wrap(headerContent, headerTag); |
| 56 | |
| 57 | // Close any open list before adding header |
| 46 | 58 | if (list) { |
| 47 | 59 | return [`</ul>${headerHtml}`, false]; |
| 48 | 60 | } else { |
| 50 | 62 | } |
| 51 | 63 | } |
| 52 | 64 | |
| 65 | // REFACTORED: Improved variable names and added comments for clarity |
| 53 | 66 | function parseLineItem(markdown, list) { |
| 54 | 67 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 68 | const listItemContent = markdown.substring(2).trim(); |
| 69 | const listItemHtml = wrap(parseText(listItemContent, true), 'li'); |
| 70 | |
| 56 | 71 | if (list) { |
| 57 | | return [innerHtml, true]; |
| 72 | return [listItemHtml, true]; |
| 58 | 73 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 74 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 75 | } |
| 61 | 76 | } |
| 62 | 77 | return [null, list]; |
| 63 | 78 | } |
| 64 | 79 | |
| 80 | // REFACTORED: Improved variable names and added comments for clarity |
| 65 | 81 | function parseParagraph(markdown, list) { |
| 82 | const paragraphHtml = parseText(markdown, false); |
| 83 | |
| 66 | 84 | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 85 | return [paragraphHtml, false]; |
| 68 | 86 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 87 | // Close any open list before adding paragraph |
| 88 | return [`</ul>${paragraphHtml}`, false]; |
| 70 | 89 | } |
| 71 | 90 | } |
| 72 | 91 | |
| 92 | // REFACTORED: Improved variable names and added comments for clarity |
| 73 | 93 | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 94 | let [htmlResult, isInListAfter] = parseHeader(markdown, list); |
| 95 | |
| 96 | // Try to parse as list item if not a header |
| 97 | if (htmlResult === null) { |
| 98 | [htmlResult, isInListAfter] = parseLineItem(markdown, list); |
| 77 | 99 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 100 | |
| 101 | // Try to parse as paragraph if not header or list item |
| 102 | if (htmlResult === null) { |
| 103 | [htmlResult, isInListAfter] = parseParagraph(markdown, list); |
| 80 | 104 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 105 | |
| 106 | // This should never happen as one of the parsers should always succeed |
| 107 | if (htmlResult === null) { |
| 108 | throw new Error('Unable to parse markdown line'); |
| 83 | 109 | } |
| 84 | | return [result, inListAfter]; |
| 110 | |
| 111 | return [htmlResult, isInListAfter]; |
| 85 | 112 | } |
| 86 | 113 | |
| 87 | 114 | /** |
| 88 | | * @param {string} markdown |
| 89 | | * @returns {string} |
| 115 | * Parse markdown text and convert it to HTML |
| 116 | * @param {string} markdown - The markdown text to parse |
| 117 | * @returns {string} - The resulting HTML |
| 90 | 118 | */ |
| 91 | 119 | export function parse(markdown) { |
| 92 | 120 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 121 | let htmlResult = ''; |
| 122 | let isInList = false; |
| 123 | |
| 95 | 124 | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 125 | let [lineHtml, newListState] = parseLine(lines[i], isInList); |
| 126 | htmlResult += lineHtml; |
| 127 | isInList = newListState; |
| 99 | 128 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 129 | |
| 130 | // Close any open list at the end of the document |
| 131 | if (isInList) { |
| 132 | return htmlResult + '</ul>'; |
| 102 | 133 | } else { |
| 103 | | return result; |
| 134 | return htmlResult; |
| 104 | 135 | } |
| 105 | 136 | } |