Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
fix(editor): resolve TOC headings with folded whitespace, images and doc links (#34972)
OrcaWinfdfe1e5a09ac
diff --git a/src/renderer/src/components/editor/markdown-table-of-contents.ts b/src/renderer/src/components/editor/markdown-table-of-contents.tsindex a5bbaf75de8..100802706fe 100644--- a/src/renderer/src/components/editor/markdown-table-of-contents.ts+++ b/src/renderer/src/components/editor/markdown-table-of-contents.ts@@ -55,11 +55,11 @@ export function stripInlineMarkdownForToc(text: string): string {.replace(/\[\[([^\]]+)\]\]/g, '$1').replace(/<[^>]+>/g, '').replace(/[*_`~]/g, '')- return foldMarkdownTocWhitespace(stripped)+ return normalizeMarkdownTocTitle(stripped)}// Collapse display whitespace without another full-string regex pass over large pasted headings.-function foldMarkdownTocWhitespace(value: string): string {+export function normalizeMarkdownTocTitle(value: string): string {let normalized = ''let pendingWhitespace = falsefor (let index = 0; index < value.length; index += 1) {@@ -147,7 +147,7 @@ export function buildMarkdownTableOfContents(markdown: string): MarkdownTocItem[typeof node.depth === 'number' &&isMarkdownTocLevel(node.depth)) {- const title = foldMarkdownTocWhitespace(markdownAstNodeToText(node))+ const title = normalizeMarkdownTocTitle(markdownAstNodeToText(node))if (title) {appendTocItem(stack, {children: [],diff --git a/src/renderer/src/components/editor/rich-markdown-toc-heading-target.test.ts b/src/renderer/src/components/editor/rich-markdown-toc-heading-target.test.tsindex 3683dd528fc..c30132c5513 100644--- a/src/renderer/src/components/editor/rich-markdown-toc-heading-target.test.ts+++ b/src/renderer/src/components/editor/rich-markdown-toc-heading-target.test.ts@@ -1,7 +1,7 @@// @vitest-environment happy-domimport { describe, expect, it } from 'vitest'-import type { MarkdownTocItem } from './markdown-table-of-contents'+import { buildMarkdownTableOfContents, type MarkdownTocItem } from './markdown-table-of-contents'import { findRichMarkdownTocHeadingTarget } from './rich-markdown-toc-heading-target'function makeTocItem(@@ -14,6 +14,56 @@ function makeTocItem(}describe('findRichMarkdownTocHeadingTarget', () => {+ it('resolves normalized TOC titles to headings with repeated display whitespace', () => {+ const container = document.createElement('div')+ container.innerHTML = '<h1>Setup guide</h1><h1>Setup guide</h1>'+ const items = buildMarkdownTableOfContents('# Setup guide\n\n# Setup guide')+ const headings = container.querySelectorAll<HTMLElement>('h1')++ expect(items.map((item) => ({ id: item.id, title: item.title }))).toEqual([+ { id: 'setup-guide', title: 'Setup guide' },+ { id: 'setup-guide-1', title: 'Setup guide' }+ ])+ expect(findRichMarkdownTocHeadingTarget(container, items, items[0].id)).toBe(headings[0])+ expect(findRichMarkdownTocHeadingTarget(container, items, items[1].id)).toBe(headings[1])+ })++ it('uses image alt text inside node views and preserves duplicate occurrences', () => {+ const container = document.createElement('div')+ container.innerHTML = `+ <h1>Before <span contenteditable="false"><img alt="Badge"></span> after</h1>+ <h1>Before <span contenteditable="false"><img alt="Other"></span> after</h1>+ <h1>Before <span contenteditable="false"><img alt="Badge"></span> after</h1>+ `+ const items = buildMarkdownTableOfContents(+ '# Before  after\n\n# Before  after\n\n# Before  after'+ )+ const headings = container.querySelectorAll<HTMLElement>('h1')++ for (const [index, item] of items.entries()) {+ expect(findRichMarkdownTocHeadingTarget(container, items, item.id)).toBe(headings[index])+ }+ })++ it('matches document-link aliases alongside plain and literal headings with the same label', () => {+ const container = document.createElement('div')+ container.innerHTML = `+ <h1>Alias</h1>+ <h1><span data-doc-link-target="Target" data-doc-link-label="Alias">Alias</span></h1>+ <h1><span data-doc-link-target="Other" data-doc-link-label="Alias">Alias</span></h1>+ <h1><code>[[Target|Alias]]</code></h1>+ <h1><span data-doc-link-target="Target">Target</span></h1>+ `+ const items = buildMarkdownTableOfContents(+ '# Alias\n\n# [[ Target | Alias ]]\n\n# [[Other|Alias]]\n\n# `[[Target|Alias]]`\n\n# [[Target]]'+ )+ const headings = container.querySelectorAll<HTMLElement>('h1')++ for (const [index, item] of items.entries()) {+ expect(findRichMarkdownTocHeadingTarget(container, items, item.id)).toBe(headings[index])+ }+ })+it('returns undefined when the requested duplicate occurrence is missing', () => {const container = document.createElement('div')container.innerHTML = '<h2>Repeat</h2><h2>Other</h2>'diff --git a/src/renderer/src/components/editor/rich-markdown-toc-heading-target.ts b/src/renderer/src/components/editor/rich-markdown-toc-heading-target.tsindex 9578bf8d0b6..ff18509755b 100644--- a/src/renderer/src/components/editor/rich-markdown-toc-heading-target.ts+++ b/src/renderer/src/components/editor/rich-markdown-toc-heading-target.ts@@ -1,8 +1,31 @@-import type { MarkdownTocItem } from './markdown-table-of-contents'+import { splitMarkdownDocLinkText } from './markdown-doc-links'+import { normalizeMarkdownTocTitle, type MarkdownTocItem } from './markdown-table-of-contents'// Match all TOC levels, including h4/h5, so every listed heading can resolve.const RICH_MARKDOWN_TOC_HEADING_SELECTOR = 'h1, h2, h3, h4, h5'+// Image node views render alt text as an attribute, but the TOC title includes it.+function headingText(node: Node): string {+ if (node.nodeType === Node.TEXT_NODE) {+ return node.textContent ?? ''+ }+ if (!(node instanceof Element)) {+ return ''+ }+ if (node.tagName === 'IMG') {+ return node.getAttribute('alt') ?? ''+ }+ return Array.from(node.childNodes, headingText).join('')+}++function matchingTitle(value: string): string {+ return normalizeMarkdownTocTitle(+ splitMarkdownDocLinkText(value)+ .map((part) => (part.type === 'text' ? part.value : part.label))+ .join('')+ )+}+export function findRichMarkdownTocHeadingTarget(container: ParentNode,items: readonly MarkdownTocItem[],@@ -13,14 +36,15 @@ export function findRichMarkdownTocHeadingTarget(return undefined}+ const title = matchingTitle(target.title)const sameTitleIndex = items- .filter((item) => item.title === target.title)+ .filter((item) => matchingTitle(item.title) === title).findIndex((item) => item.id === target.id)let remaining = Math.max(0, sameTitleIndex)for (const candidate of container.querySelectorAll<HTMLElement>(RICH_MARKDOWN_TOC_HEADING_SELECTOR)) {- if (candidate.textContent?.trim() === target.title) {+ if (matchingTitle(headingText(candidate)) === title) {if (remaining === 0) {return candidate}