| 1 | // REFACTORING: Markdown parser |
| 2 | // |
| 3 | // This code has been refactored to improve readability and maintainability |
| 4 | // while preserving all existing functionality. The refactoring focuses on: |
| 5 | // |
| 6 | // - Clear separation of concerns between different markdown elements |
| 7 | // - More functional approach with explicit state management |
| 8 | // - Consistent use of regex patterns for all parsing operations |
| 9 | // - Better function organization and naming conventions |
| 10 | // - Improved code structure for easier understanding and modification |
| 11 | |
| 12 | // ============================================================================= |
| 13 | // CONSTANTS AND CONFIGURATION |
| 14 | // ============================================================================= |
| 15 | |
| 16 | // Markdown syntax constants |
| 17 | const MARKDOWN = { |
| 18 | HEADER_PATTERN: /^(#{1,6})\s+(.+)$/, |
| 19 | LIST_ITEM_PREFIX: '*', |
| 20 | BOLD_DELIMITER: '__', |
| 21 | ITALIC_DELIMITER: '_', |
| 22 | MAX_HEADER_LEVEL: 6 |
| 23 | }; |
| 24 | |
| 25 | // HTML tag constants |
| 26 | const HTML_TAGS = { |
| 27 | BOLD: 'strong', |
| 28 | ITALIC: 'em', |
| 29 | PARAGRAPH: 'p', |
| 30 | LIST: 'ul', |
| 31 | LIST_ITEM: 'li' |
| 32 | }; |
| 33 | |
| 34 | // ============================================================================= |
| 35 | // CORE HELPER FUNCTIONS |
| 36 | // ============================================================================= |
| 37 | |
| 38 | // Helper function for HTML wrapping |
| 1 | 39 | function wrap(text, tag) { |
| 2 | 40 | return `<${tag}>${text}</${tag}>`; |
| 3 | 41 | } |
| 4 | 42 | |
| 43 | // Helper function to check if text starts with a specific HTML tag |
| 5 | 44 | function isTag(text, tag) { |
| 6 | 45 | return text.startsWith(`<${tag}>`); |
| 7 | 46 | } |
| 8 | 47 | |
| 48 | // ============================================================================= |
| 49 | // TEXT FORMATTING FUNCTIONS |
| 50 | // ============================================================================= |
| 51 | |
| 52 | // Generic text formatting parser using regex patterns |
| 53 | // Replaces text between delimiters with HTML tags |
| 9 | 54 | function parser(markdown, delimiter, tag) { |
| 10 | 55 | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | 56 | const replacement = `<${tag}>$1</${tag}>`; |
| 12 | 57 | return markdown.replace(pattern, replacement); |
| 13 | 58 | } |
| 14 | 59 | |
| 60 | // Bold text parser using double underscore delimiter |
| 15 | 61 | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 62 | return parser(markdown, MARKDOWN.BOLD_DELIMITER, HTML_TAGS.BOLD); |
| 17 | 63 | } |
| 18 | 64 | |
| 65 | // Italic text parser using single underscore delimiter |
| 19 | 66 | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 67 | return parser(markdown, MARKDOWN.ITALIC_DELIMITER, HTML_TAGS.ITALIC); |
| 21 | 68 | } |
| 22 | 69 | |
| 70 | // ============================================================================= |
| 71 | // CONTENT PARSING FUNCTIONS |
| 72 | // ============================================================================= |
| 73 | |
| 74 | // Text content parser with proper formatting |
| 75 | // Applies bold and italic formatting, then wraps in paragraph tag if not in a list |
| 23 | 76 | function parseText(markdown, list) { |
| 24 | 77 | const parsedText = parse_(parse__(markdown)); |
| 25 | 78 | if (list) { |
| 26 | 79 | return parsedText; |
| 27 | 80 | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 81 | return wrap(parsedText, HTML_TAGS.PARAGRAPH); |
| 29 | 82 | } |
| 30 | 83 | } |
| 31 | 84 | |
| 85 | // ============================================================================= |
| 86 | // STRUCTURAL ELEMENT PARSERS |
| 87 | // ============================================================================= |
| 88 | |
| 89 | // Header parser with proper level detection and list handling |
| 90 | // Detects headers using # symbols (1-6 levels) and handles list state transitions |
| 32 | 91 | function parseHeader(markdown, list) { |
| 33 | | let count = 0; |
| 34 | | for (let i = 0; i < markdown.length; i++) { |
| 35 | | if (markdown[i] === '#') { |
| 36 | | count += 1; |
| 37 | | } else { |
| 38 | | break; |
| 39 | | } |
| 40 | | } |
| 41 | | if (count === 0 || count > 6) { |
| 92 | const headerMatch = markdown.match(MARKDOWN.HEADER_PATTERN); |
| 93 | if (!headerMatch) { |
| 42 | 94 | return [null, list]; |
| 43 | 95 | } |
| 44 | | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 96 | |
| 97 | const [, hashes, content] = headerMatch; |
| 98 | const level = hashes.length; |
| 99 | const headerTag = `h${level}`; |
| 100 | const headerHtml = wrap(content.trim(), headerTag); |
| 101 | |
| 102 | // Close list if we were in one and now have a header |
| 46 | 103 | if (list) { |
| 47 | | return [`</ul>${headerHtml}`, false]; |
| 104 | return [`</${HTML_TAGS.LIST}>${headerHtml}`, false]; |
| 48 | 105 | } else { |
| 49 | 106 | return [headerHtml, false]; |
| 50 | 107 | } |
| 51 | 108 | } |
| 52 | 109 | |
| 110 | // List item parser with proper formatting and list state management |
| 111 | // Detects list items using * symbol and handles list opening/closing |
| 53 | 112 | function parseLineItem(markdown, list) { |
| 54 | | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 113 | if (markdown.startsWith(MARKDOWN.LIST_ITEM_PREFIX)) { |
| 114 | const innerHtml = wrap(parseText(markdown.substring(2), true), HTML_TAGS.LIST_ITEM); |
| 56 | 115 | if (list) { |
| 116 | // Already in a list, just add the item |
| 57 | 117 | return [innerHtml, true]; |
| 58 | 118 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 119 | // Start a new list |
| 120 | return [`<${HTML_TAGS.LIST}>${innerHtml}`, true]; |
| 60 | 121 | } |
| 61 | 122 | } |
| 62 | 123 | return [null, list]; |
| 63 | 124 | } |
| 64 | 125 | |
| 126 | // Paragraph parser with proper formatting and list state management |
| 127 | // Handles regular text paragraphs and closes lists when transitioning to paragraphs |
| 65 | 128 | function parseParagraph(markdown, list) { |
| 66 | 129 | if (!list) { |
| 130 | // Not in a list, just parse as paragraph |
| 67 | 131 | return [parseText(markdown, false), false]; |
| 68 | 132 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 133 | // Close the list and start a paragraph |
| 134 | return [`</${HTML_TAGS.LIST}>${parseText(markdown, false)}`, false]; |
| 70 | 135 | } |
| 71 | 136 | } |
| 72 | 137 | |
| 138 | // ============================================================================= |
| 139 | // MAIN PARSING PIPELINE |
| 140 | // ============================================================================= |
| 141 | |
| 142 | // Main line parser that dispatches to appropriate specialized parsers |
| 143 | // Tries parsers in order: headers -> list items -> paragraphs |
| 144 | // This provides a clear pipeline for processing different markdown elements |
| 73 | 145 | function parseLine(markdown, list) { |
| 74 | 146 | let [result, inListAfter] = parseHeader(markdown, list); |
| 75 | 147 | if (result === null) { |