跳到正文
DEV Community· vision zhiyuan·· 45 分鐘前AI 評分52

為何 AST stringify 會破壞 Git diff:Inkmap 如何建立保留原始位元組的雙向 Markdown 心智圖同步

Why AST Stringify Ruins Git Diffs: Building Byte-Preserving Two-Way Markdown Mind Map Sync

AI 導讀

Inkmap 採用來源區間位元組補丁取代整棵 AST 序列化,提供可雙向同步 Markdown 的心智圖工具。它只修改選定文字或移動區段所需的範圍,保留其他 Markdown 格式,並可在心智圖節點內編輯 Mermaid 圖表。工具提供瀏覽器版、macOS、Windows、Linux 桌面版及 IDE/MCP 整合;所有客戶端可免費試用 7 日,其後為 $15 一次性終身授權。

正文

Every developer who maintains architecture docs, RFCs, or study notes in plain .md eventually wishes they could step back, drag two sections around on a spatial mind map, or tweak a Mermaid state transition without manually counting # hashes and list indents.

Yet almost every "visual Markdown editor" gets uninstalled by engineers within 48 hours. The culprit is the classic round-trip pipeline:

raw .md  ──parse()──▶  AST  ──mutate node──▶  stringify(AST)  ──overwrite disk

The moment remark-stringify (or any full-tree serializer) rewrites your file, a two-word typo fix on one node reformats your hand-aligned GFM tables, normalizes * bullets into -, strips intentional blank lines, and produces a 200-line git diff.

While building Inkmap—a local-first two-way Markdown mind map & inline Mermaid workbench for macOS, Windows, Linux Desktop and 10 IDEs / AI Agents (VS Code, Cursor, Claude Code, Codex)—we threw out full-tree serialization entirely and built a Source-Span Byte-Range Patcher instead.

Try it live in the browser first (zero login/install): https://www.zhiyuanv.com/inkmap.html?lang=en

Two-Way Byte-Preserving Sync


1. Indexing Two Spans per Node: labelSpan vs. subtreeSpan

Instead of treating the AST as a mutable document generator, we treat mdast strictly as a read-only coordinate index. Every heading and list item records two byte/code-unit intervals:

export interface OutlineNode {
  id: string;
  depth: number; // 1..6 for headings, 7+ for nested lists
  labelSpan: { start: number; end: number };   // Double-click rename patches ONLY this slice
  subtreeSpan: { start: number; end: number }; // Drag-and-drop moves this verbatim slice
}

For a ## 2. Auth Pipeline heading, subtreeSpan.end scans forward to the start offset of the next heading with depth <= 2 (or EOF). Everything nested inside—fenced code blocks, GFM tables, HTML comments, callouts, and LaTeX math—rides along verbatim inside the raw string slice without ever being re-formatted.


2. Subtree Drag-and-Drop Without Touching Prose Formatting

When you drag a ### subsection (depth = 3) directly under a # root heading (new depth = 2, so delta = -1), we slice the original substring doc.slice(sub.start, sub.end), adjust # prefixes on heading lines outside fenced code blocks by delta, and apply non-overlapping patches in descending offset order so earlier offsets never shift:

export function applyDescendingPatches(source: string, patches: SpanPatch[]): string {
  const ordered = [...patches].sort((a, b) => b.start - a.start);
  let next = source;
  for (let i = 0; i < ordered.length; i++) {
    const p = ordered[i];
    if (i > 0 && p.end > ordered[i - 1].start) {
      throw new Error("Overlapping source spans");
    }
    next = next.slice(0, p.start) + p.replacement + next.slice(p.end);
  }
  return next;
}

3. Inline Editable Mermaid Diagrams Inside Mind Map Nodes

Keeping prose specs in .md while drawing flowcharts in an external whiteboard guarantees stale diagrams within two sprints. So we render fenced mermaid blocks (flowchart, sequenceDiagram, stateDiagram-v2, erDiagram) and KaTeX math right inside the mind-map node—and tokenize node/edge labels so clicking a label on the diagram patches just those characters in the fenced block:

Inline Editable Mermaid & Math


4. Native Desktop App (macOS / Windows / Linux) + 1-Line IDE/MCP Setup

Not everyone wants to open an IDE just to brainstorm a spec or thesis outline, so the exact same core now runs across:

  • Standalone Desktop App (macOS Apple Silicon/Intel .dmg, Linux .deb/.AppImage, and Windows): https://www.zhiyuanv.com/software.html#desktop
  • VS Code, Cursor, Windsurf, Trae, Claude Desktop/Code & Codex:
  npx -y inkmap-mcp@latest install

All clients include a 7-day full-featured free trial (no login required), followed by a $15 one-time lifetime license across Desktop + all IDEs (no subscriptions). Would love feedback from fellow Markdown & AST tooling nerds!

來源:DEV Community · dev.to