Files
apes-sidebar/src/client/use-markdown-surface.ts
T
ymac 6f2054a56c fix(sidebar): 会话地址的基准统一到会话 header cwd(#947 / #950)
会话地址 `dsh-resource://file/session/<sid>/<path>` 的相对路径,宿主构造端
(ui-chat 的 openFile)与解析端(api-workspace-files)都取会话存储的 header cwd;
插件自 #802 起让 fs 路由与原生 tab 体跟随活动 worktree 根,于是地址里的相对拼写
被活动根再拼一次:worktree 嵌在 header cwd 之下时路径翻倍(ENOENT,聊天链接与
改动页都中招),两个检出都有同名文件时静默读到另一份(可保存写回)。

规则:构造端与消费端共用同一个基准,并在地址被读到的边界把它解析成绝对路径——
绝对路径在任何基准下都一样,下游再无可重定基的余地。

- 新增 src/client/address-base.ts 的 addressBaseOf(ctx, sessionId):会话列表里的
  header cwd(宿主地址契约的基准),取不到返回 undefined 让调用方回退。
- 消费边界:fileParamsOf(info, base?) 在切 `path:line` 规格之前把地址路径解析成
  绝对路径;native/index.ts 的 editor 类型注入
  paramsOf: (info) => fileParamsOf(info, addressBaseOf(ctx, fileSessionIdOf(info)))。
- 构造边界:native/surface.ts 的 fileAddress 与 sidebar-file.ts 的
  openClaimedNativeFile 都以 addressBaseOf 优先,调用方 cwd 只作回退——同一文件
  只有一个地址(与宿主聊天路径的地址一致,页签照旧合并)。
- 意图侧:openSidebarFile(ctx, sessionId, path, { line?, cwd? }) 的 cwd 是调用方
  手上的活根(EditorHost / 改动页 / markdown 面板传 useSessionRoot 的结果),
  相对路径按它解析,缺省才回退 header cwd——同一相对路径在各入口不再落到不同检出。
- 测试:新增 tests/worktree-address.spec.ts(基准三态 / 绝对路径不可重定基 /
  构造端基准 / 相对路径按活根解析),真 git worktree 夹具;file-open-claim 的
  `line` 位置参数随之改为 `{ line }`(唯一签名变更点)。
2026-10-14 23:18:08 +08:00

180 lines
8.0 KiB
TypeScript

/**
* React glue for ONE plugin-drawn markdown surface (the container that holds
* the rendered markdown). It returns three things the caller wires up:
*
* - `surfaceRef`: a CALLBACK REF for that container. The installer behind it
* runs the DOM pass from `markdown-navigation.ts` (heading slugs + collapsed
* explicit anchors) on mount and after every content mutation — the pattern
* the raw-HTML preview pass already uses, which keeps late content (the lazy
* mermaid chunk, shiki highlighting) covered and makes the pass self-healing
* when React re-renders a subtree it owns — and then applies the cross-file
* fragment parked for this document. A callback ref (not a `RefObject`)
* because the container mounts and unmounts with its host — the preview
* container disappears on every preview→edit flip — and only a ref callback
* fires on both edges (React 18 hands it `null` on unmount, which is where
* the observer is torn down). Its identity is keyed on (session, path) alone,
* so an ordinary re-render never detaches anything.
* - `openFile`: the host delegate's file handler. The host renderer only makes
* a local markdown link clickable when a `MarkdownDelegateProvider` supplies
* this, which is why every caller wraps its markdown in that provider with
* `openFile={surface.openFile}`: the provider's scope IS the claim boundary,
* and a link rendered outside it (the host's chat prose, any other panel) is
* never seen by this plugin.
* - `rewrite`: the source-side pass (`rewriteLocalMarkdownLinks`) that runs
* BEFORE rendering, so a heading fragment survives the host's parser and a
* link the plugin does not claim stays inert prose.
*/
import { useCallback, useRef } from 'react'
import type { Context } from '../context-types.ts'
import {
jumpToFragment,
lineOfHostFragment,
markdownAnchorTarget,
prepareMarkdownAnchors,
rememberMarkdownAnchor,
rewriteLocalMarkdownLinks,
takeMarkdownAnchor,
} from './markdown-navigation.ts'
import { openSidebarFile } from './sidebar-file.ts'
/** The DOM marker the installer puts on the container it owns (the claim
* boundary is the provider scope; this makes the surface inspectable in a
* browser and pointable from a spec). */
export const MARKDOWN_SURFACE_ATTR = 'data-dsh-md-surface'
export interface MarkdownSurfaceOptions {
/** Client context: the open goes through `openSidebarFile`, the plugin's own
* claim-aware editor path (same entry point the file tree and the chat
* mentions use). */
ctx: Context
/** The session the markdown renders in — the open scope and the key of the
* pending-fragment table. */
sessionId: string
/** The session workspace root, when the caller has a scope. Read live at
* click time (a session's cwd may land after the tab does). */
cwd?: string
/** The rendered file's absolute path: relative targets resolve against its
* directory. Prose surfaces (transcript, task note) omit it — their
* relative targets resolve against the session cwd instead. */
path?: string
}
/** What a surface wires into its markup. */
export interface MarkdownSurfaceBinding {
/** Callback ref for the container that holds the rendered markdown. */
surfaceRef: (element: HTMLElement | null) => void
/** `MarkdownDelegateProvider.openFile` for this surface. */
openFile: (destination: string, options?: { line?: number }) => void
/** Rewrite one markdown text before rendering it in this surface. */
rewrite: (text: string) => string
}
/**
* Wire one plugin markdown surface.
* @param opts - the surface's context, session and rendered document path.
* @returns the container ref, the delegate handler and the source rewrite.
*/
export function useMarkdownSurface(opts: MarkdownSurfaceOptions): MarkdownSurfaceBinding {
const { ctx, sessionId, cwd, path } = opts
const containerRef = useRef<HTMLElement | null>(null)
const detachRef = useRef<(() => void) | null>(null)
/** The session workspace root: the caller's scope when it has one, else the
* session summary (a stripped-down host or a bare spec context may have no
* session list at all — the opener then resolves against its own cwd). */
const resolveCwd = useCallback(
(): string | undefined => cwd ?? ctx.sessions?.list?.getSnapshot?.()?.byId?.[sessionId]?.cwd,
[ctx, sessionId, cwd],
)
const surfaceRef = useCallback((element: HTMLElement | null): void => {
detachRef.current?.()
detachRef.current = null
containerRef.current = element
if (element === null) return
detachRef.current = attachMarkdownSurface(element, sessionId, path, resolveCwd)
}, [sessionId, path, resolveCwd])
const openFile = useCallback((destination: string, options?: { line?: number }): void => {
const target = markdownAnchorTarget(destination, path, resolveCwd())
if (target === null) return
if (target.kind === 'same-page') {
const container = containerRef.current
if (container !== null) jumpToFragment(container, target.fragment)
return
}
// `#L24` is a LINE, not an anchor: the host hands the number over as
// `options.line` (and leaves the destination untouched), the fragment is
// the same grammar for a caller that only has the destination. Either way
// the line has to travel WITH the open — a line reference asks for source,
// so the editor lands on it (and leaves preview, which renders no lines).
// Only a heading fragment parks a jump for the document about to open.
const line = options?.line ?? lineOfHostFragment(target.fragment)
if (line === undefined && target.fragment !== '') {
rememberMarkdownAnchor(sessionId, resolveCwd(), target.path, target.fragment)
}
try {
// The live root rides along: a destination that resolved to a RELATIVE
// path (neither the document's directory nor a cwd was known) must land
// on the same workspace the surrounding tree and routes use, not on the
// session's frozen header cwd (#950).
openSidebarFile(ctx, sessionId, target.path, { line, cwd: resolveCwd() })
} catch (error) {
// A failed open leaves the reader where they are (the link was claimed);
// the parked fragment expires on its own.
console.error('[dsh-better-sidebar] markdown file link open failed', error)
}
}, [ctx, sessionId, path, resolveCwd])
const rewrite = useCallback(
(text: string): string => rewriteLocalMarkdownLinks(text, path, resolveCwd()),
[path, resolveCwd],
)
return { surfaceRef, openFile, rewrite }
}
/**
* Install the post-render half on one container: the DOM pass, its mutation
* re-runs, and the pending cross-file fragment.
* @returns the disposer (observer + marker).
*/
function attachMarkdownSurface(
container: HTMLElement,
sessionId: string,
path: string | undefined,
resolveCwd: () => string | undefined,
): () => void {
container.setAttribute(MARKDOWN_SURFACE_ATTR, '')
prepareMarkdownAnchors(container)
/** The fragment this surface still owes the reader (taken once, then carried
* here until the heading actually renders — the content may arrive a beat
* after the container does). */
let pending: string | null = path === undefined || path === '' ? null : takeMarkdownAnchor(sessionId, resolveCwd(), path)
const applyPending = (): void => {
if (pending !== null && jumpToFragment(container, pending)) pending = null
}
// After the commit that mounted this container: the host's layout effects
// (the preview's remembered-scroll restore) run first, so this is the last
// write to the scroller.
if (pending !== null) queueMicrotask(applyPending)
let scheduled = false
const observer = new MutationObserver(() => {
if (scheduled) return
scheduled = true
queueMicrotask(() => {
scheduled = false
prepareMarkdownAnchors(container)
applyPending()
})
})
observer.observe(container, { childList: true, subtree: true })
return () => {
observer.disconnect()
container.removeAttribute(MARKDOWN_SURFACE_ATTR)
}
}