| 2 | 2 | return `<${tag}>${text}</${tag}>`; |
| 3 | 3 | } |
| 4 | 4 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 5 | // Refactored: Removed unused isTag function |
| 6 | |
| 7 | // Refactored: Added MarkdownLineType for better code readability |
| 8 | const MarkdownLineType = { |
| 9 | HEADER: 'header', |
| 10 | LIST_ITEM: 'list_item', |
| 11 | PARAGRAPH: 'paragraph' |
| 12 | }; |
| 8 | 13 | |
| 9 | 14 | function parser(markdown, delimiter, tag) { |
| 10 | 15 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 12 | 17 | return markdown.replace(pattern, replacement); |
| 13 | 18 | } |
| 14 | 19 | |
| 15 | | function parse__(markdown) { |
| 20 | // Refactored: Renamed parse__ to parseBoldText for clarity |
| 21 | function parseBoldText(markdown) { |
| 16 | 22 | return parser(markdown, '__', 'strong'); |
| 17 | 23 | } |
| 18 | 24 | |
| 19 | | function parse_(markdown) { |
| 25 | // Refactored: Renamed parse_ to parseItalicText for clarity |
| 26 | function parseItalicText(markdown) { |
| 20 | 27 | return parser(markdown, '_', 'em'); |
| 21 | 28 | } |
| 22 | 29 | |
| 30 | // Refactored: Updated to use renamed functions |
| 23 | 31 | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 32 | const parsedText = parseItalicText(parseBoldText(markdown)); |
| 25 | 33 | if (list) { |
| 26 | 34 | return parsedText; |
| 27 | 35 | } else { |
| 70 | 78 | } |
| 71 | 79 | } |
| 72 | 80 | |
| 81 | // Refactored: Simplified control flow with early returns and improved error message |
| 73 | 82 | function parseLine(markdown, list) { |
| 83 | // Try to parse as header |
| 74 | 84 | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 85 | if (result !== null) { |
| 86 | return [result, inListAfter]; |
| 77 | 87 | } |
| 78 | | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 88 | |
| 89 | // Try to parse as list item |
| 90 | [result, inListAfter] = parseLineItem(markdown, list); |
| 91 | if (result !== null) { |
| 92 | return [result, inListAfter]; |
| 80 | 93 | } |
| 81 | | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 94 | |
| 95 | // Parse as paragraph (fallback) |
| 96 | [result, inListAfter] = parseParagraph(markdown, list); |
| 97 | if (result !== null) { |
| 98 | return [result, inListAfter]; |
| 83 | 99 | } |
| 84 | | return [result, inListAfter]; |
| 100 | |
| 101 | // This should never happen as parseParagraph always returns a result |
| 102 | throw new Error('Failed to parse markdown line: ' + markdown); |
| 85 | 103 | } |
| 86 | 104 | |
| 105 | // Refactored: Extracted list state management to helper function |
| 87 | 106 | /** |
| 88 | 107 | * @param {string} markdown |
| 89 | 108 | * @returns {string} |