* Parse full markdown text * @param {string} text - Full markdown text * @param {number} activeLine - Currently active line index (optional) * @param {boolean} showActiveLineRaw - Show raw markdown on active line * @param {Function} instanceHighlighter - Instance-specific code highlighte
(text, activeLine = -1, showActiveLineRaw = false, instanceHighlighter, isPreviewMode = false)
| 492 | * @returns {string} Parsed HTML |
| 493 | */ |
| 494 | static parse(text, activeLine = -1, showActiveLineRaw = false, instanceHighlighter, isPreviewMode = false) { |
| 495 | // Reset link counter for each parse |
| 496 | this.resetLinkIndex(); |
| 497 | |
| 498 | const lines = text.split('\n'); |
| 499 | let inCodeBlock = false; |
| 500 | |
| 501 | const parsedLines = lines.map((line, index) => { |
| 502 | // Show raw markdown on active line if requested |
| 503 | if (showActiveLineRaw && index === activeLine) { |
| 504 | const content = this.escapeHtml(line) || ' '; |
| 505 | return `<div class="raw-line">${content}</div>`; |
| 506 | } |
| 507 | |
| 508 | // Check if this line is a code fence |
| 509 | const codeFenceRegex = /^```[^`]*$/; |
| 510 | if (codeFenceRegex.test(line)) { |
| 511 | inCodeBlock = !inCodeBlock; |
| 512 | // Parse fence markers normally to get styled output |
| 513 | return this.applyCustomSyntax(this.parseLine(line, isPreviewMode)); |
| 514 | } |
| 515 | |
| 516 | // If we're inside a code block, don't parse as markdown |
| 517 | if (inCodeBlock) { |
| 518 | const escaped = this.escapeHtml(line); |
| 519 | const indented = this.preserveIndentation(escaped, line); |
| 520 | return `<div>${indented || ' '}</div>`; |
| 521 | } |
| 522 | |
| 523 | // Otherwise, parse the markdown normally |
| 524 | return this.applyCustomSyntax(this.parseLine(line, isPreviewMode)); |
| 525 | }); |
| 526 | |
| 527 | // Join without newlines to prevent extra spacing |
| 528 | const html = parsedLines.join(''); |
| 529 | |
| 530 | // Apply post-processing for list consolidation |
| 531 | return this.postProcessHTML(html, instanceHighlighter); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Post-process HTML to consolidate lists and code blocks |