* Post-process HTML to consolidate lists and code blocks * @param {string} html - HTML to post-process * @param {Function} instanceHighlighter - Instance-specific code highlighter (optional, overrides global if provided) * @returns {string} Post-processed HTML with consolidated lists and co
(html, instanceHighlighter)
| 538 | * @returns {string} Post-processed HTML with consolidated lists and code blocks |
| 539 | */ |
| 540 | static postProcessHTML(html, instanceHighlighter) { |
| 541 | // Check if we're in a browser environment |
| 542 | if (typeof document === 'undefined' || !document) { |
| 543 | // In Node.js environment - do manual post-processing |
| 544 | return this.postProcessHTMLManual(html, instanceHighlighter); |
| 545 | } |
| 546 | |
| 547 | // Parse HTML string into DOM |
| 548 | const container = document.createElement('div'); |
| 549 | container.innerHTML = html; |
| 550 | |
| 551 | let currentList = null; |
| 552 | let listType = null; |
| 553 | let currentCodeBlock = null; |
| 554 | let inCodeBlock = false; |
| 555 | |
| 556 | // Process all direct children - need to be careful with live NodeList |
| 557 | const children = Array.from(container.children); |
| 558 | |
| 559 | for (let i = 0; i < children.length; i++) { |
| 560 | const child = children[i]; |
| 561 | |
| 562 | // Skip if child was already processed/removed |
| 563 | if (!child.parentNode) continue; |
| 564 | |
| 565 | // Check for code fence start/end |
| 566 | const codeFence = child.querySelector('.code-fence'); |
| 567 | if (codeFence) { |
| 568 | const fenceText = codeFence.textContent; |
| 569 | if (fenceText.startsWith('```')) { |
| 570 | if (!inCodeBlock) { |
| 571 | // Start of code block - keep fence visible, then add pre/code |
| 572 | inCodeBlock = true; |
| 573 | |
| 574 | // Create the code block that will follow the fence |
| 575 | currentCodeBlock = document.createElement('pre'); |
| 576 | const codeElement = document.createElement('code'); |
| 577 | currentCodeBlock.appendChild(codeElement); |
| 578 | currentCodeBlock.className = 'code-block'; |
| 579 | |
| 580 | // Extract language if present |
| 581 | const lang = fenceText.slice(3).trim(); |
| 582 | if (lang) { |
| 583 | codeElement.className = `language-${lang}`; |
| 584 | } |
| 585 | |
| 586 | // Insert code block after the fence div (don't remove the fence) |
| 587 | container.insertBefore(currentCodeBlock, child.nextSibling); |
| 588 | |
| 589 | // Store reference to the code element for adding content |
| 590 | currentCodeBlock._codeElement = codeElement; |
| 591 | currentCodeBlock._language = lang; |
| 592 | currentCodeBlock._codeContent = ''; |
| 593 | continue; |
| 594 | } else { |
| 595 | // End of code block - apply highlighting if needed |
| 596 | // Use instance highlighter if provided, otherwise fall back to global highlighter |
| 597 | const highlighter = instanceHighlighter || this.codeHighlighter; |
no test coverage detected