An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:memo: Add Workspace File-System Layout document https://github.com/siyuan-note/siyuan/issues/18007
Danield24ee98b50be
diff --git a/SY-FORMAT.md b/SY-FORMAT.mdindex 859666631b..40e1e273ed 100644--- a/SY-FORMAT.md+++ b/SY-FORMAT.md@@ -3,6 +3,7 @@> Spec baseline: `2` (current across all files).> Verified against samples: `20200825162036-4dx365o.sy` (formatting elements), `20200905090211-2vixtlf.sy` (block types).> All conclusions are based on real samples and the Lute / SiYuan kernel source. Fields marked `【inferred】` were not directly observed in the samples — re-verify against a real sample before generating them.+> Companion document: [`WORKSPACE.md`](./WORKSPACE.md) covers the overall on-disk layout of the workspace (how notebooks, parent/child documents, and assets are organized); this document focuses on the **internal** JSON structure of a `.sy` file.## 0. In one sentence@@ -15,7 +16,7 @@ A `.sy` file is a Lute AST tree serialized to JSON. The root node is always `NodSiYuan offers three official paths to mutate data: **HTTP API, MCP, and CLI**. **Prefer them by default.** The kernel handles AST serialization, block-ID allocation, and synchronization of two indexes: the block-tree index (`blocktree.db`, the block-ID → file-path map that block refs and breadcrumbs depend on) and the full-text search index (`siyuan.db` + FTS5). Writing the files directly bypasses all of this and easily leaves the indexes out of sync.**Only read/write `.sy` as JSON when the official paths are inconvenient.** Applicable scenarios:-- Bulk offline migration (cold-init a workspace, import external data)+- Bulk offline migration (cold-init a workspace, import external data; for the workspace's on-disk layout see [`WORKSPACE.md`](./WORKSPACE.md))- Read-only statistics, analysis, custom export / format conversion- Repairing low-level structural issues (legacy files, illegal nodes)- Programmatic scaffolding / template generation@@ -59,7 +60,7 @@ Division of labor among the four paths:| `Properties` | ✅ | Document-level IAL — see §8 || `Children` | ✅ | Array of body child blocks; must contain at least one block |-> ⚠️ The file path strictly corresponds to the root ID: `data/<box>/<...>/<rootID>.sy`. Changing the root ID means renaming the file — don't change it casually.+> ⚠️ The file path strictly corresponds to the root ID: `data/<box>/<...>/<rootID>.sy`. Changing the root ID means renaming the file — don't change it casually. For the full file-system layout see [`WORKSPACE.md`](./WORKSPACE.md).---diff --git a/SY-FORMAT.zh-CN.md b/SY-FORMAT.zh-CN.mdindex ab6f07a9b2..c796eccc3b 100644--- a/SY-FORMAT.zh-CN.md+++ b/SY-FORMAT.zh-CN.md@@ -3,6 +3,7 @@> 版本基准:Spec `2`(当前所有文件)。> 核验样本:`20200825162036-4dx365o.sy`(排版元素)、`20200905090211-2vixtlf.sy`(内容块类型)。> 本文档所有结论均基于真实样本与 Lute / 思源内核代码核验;标注 `【推断】` 的字段表示未在样本中直接出现,建议生成前再用真实样本核验。+> 配套文档:[`WORKSPACE.zh-CN.md`](./WORKSPACE.zh-CN.md) 讲工作区在磁盘上的整体布局(笔记本、父子文档、附件的组织方式);本文档专注 `.sy` 文件**内部**的 JSON 结构。## 0. 一句话本质@@ -15,7 +16,7 @@思源提供了 **HTTP API、MCP、CLI** 三条官方路径来修改数据。**默认应优先使用它们**,因为内核会负责 AST 序列化、块 ID 分配,以及两套索引的同步——块树索引(`blocktree.db`,块 ID → 文件路径映射,块引用/面包屑依赖它)和全文搜索索引(`siyuan.db` + FTS5)。直接改盘绕过了这些逻辑,容易导致索引不一致。**仅当官方路径不便时,才直接以 JSON 读写 `.sy`**。适用场景:-- 批量离线迁移(冷初始化工作区、外部数据导入)+- 批量离线迁移(冷初始化工作区、外部数据导入;工作区的磁盘布局见 [`WORKSPACE.zh-CN.md`](./WORKSPACE.zh-CN.md))- 只读的内容统计、分析、自定义导出/格式转换- 修复底层结构问题(遗留文件、非法节点)- 程序化生成模板/脚手架@@ -59,7 +60,7 @@| `Properties` | ✅ | 文档级 IAL,见 §8 || `Children` | ✅ | 正文子块数组,至少含一个块 |-> ⚠️ 文件路径与根 ID 严格对应:`data/<box>/<...>/<根ID>.sy`。改根 ID 等于改文件名,AI 不要随意改根 ID。+> ⚠️ 文件路径与根 ID 严格对应:`data/<box>/<...>/<根ID>.sy`。改根 ID 等于改文件名,AI 不要随意改根 ID。文件系统的完整布局见 [`WORKSPACE.zh-CN.md`](./WORKSPACE.zh-CN.md)。---diff --git a/WORKSPACE.md b/WORKSPACE.mdnew file mode 100644index 0000000000..03b088c49b--- /dev/null+++ b/WORKSPACE.md@@ -0,0 +1,250 @@+# SiYuan Workspace File-System Layout — Reference++> This document describes how a SiYuan workspace is organized on disk.+> It complements [`SY-FORMAT.md`](./SY-FORMAT.md): the latter covers the **internal** JSON structure of a `.sy` file, while this one covers the **overall** file-system layout of the workspace.+> All conclusions are verified against a real workspace and the kernel source.++## 0. In one sentence++A SiYuan workspace is a **self-describing** directory tree: notebooks, documents, and assets all live on disk as readable files and directories. **The filename is the ID; the directory structure is the document hierarchy.** There is no binary database recording "where documents are" — the file system itself is the single source of truth. The kernel reconstructs all notebooks and documents simply by walking the directories.++---++## 1. Cheat-sheet tree (overview)++```+<workspace root>/ e.g. F:\SiYuan\+├── conf/+│ ├── conf.json # ★Workspace-level config (appearance/system/sync/editor...)+│ ├── ca.crt / cert.pem / key.pem # TLS certificates+│ └── windowState.json+├── data/ # DataDir — root of all notebook data+│ ├── .siyuan/ # Workspace-level hidden config (*ignore rules, etc.)+│ ├── assets/ # ★Global assets (images/audio/video/files)+│ ├── templates/ # Global templates (.md)+│ ├── widgets/ # Widgets+│ ├── plugins/ # Plugins+│ ├── emojis/ # Custom emoji+│ ├── snippets/ # Code snippets (CSS/JS)+│ ├── public/ # Static resources+│ ├── storage/ # Runtime indexes / cache / databases+│ └── <boxID>/ # ★Notebook (dir name = notebook ID)+│ ├── .siyuan/+│ │ ├── conf.json # ★Notebook config (BoxConf: name/icon/closed...)+│ │ └── sort.json # Custom sort mapping {docID: order}+│ ├── <docID>.sy # Document (JSON tree; filename = doc rootID)+│ └── <docID>/ # ★Children of that document (same-named pair)+│ ├── <childID>.sy+│ └── <childID>/ ... # Nesting of arbitrary depth+├── repo/ # Data repo (snapshots/sync)+├── history/ # Edit-history archive+└── temp/ # Temp/export files+```++---++## 2. Workspace root++The workspace root is chosen by the user (e.g. `F:\SiYuan\`). Whether a directory is a valid workspace is decided by: `conf/conf.json` exists and contains a `kernelVersion` field.++Fixed sub-directories under the root:++| Sub-dir | Purpose |+|---|---|+| `conf/` | Workspace-level config + TLS certificates |+| `data/` | **All notebook data** (the core of this document) |+| `repo/` | Data-snapshot repo (used for sync/history) |+| `history/` | Edit-history archive |+| `temp/` | Temp/export files |+| `corrupted/` | Quarantine for corrupted data (auto-moved by the kernel) |++---++## 3. Top-level layout of `data/`++The first level of `data/` mixes two kinds of entries:++1. **Reserved resource directories:** `.siyuan/`, `assets/`, `templates/`, `widgets/`, `plugins/`, `emojis/`, `snippets/`, `public/`, `storage/`.+2. **Notebook directories:** each is a folder named with the notebook ID.++### Reserved-filename list (★ avoid when writing files)++`IsReservedFilename`:++```go+func IsReservedFilename(baseName string) bool {+ return "assets" == baseName || "templates" == baseName || "widgets" == baseName ||+ "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".")+}+```++That is, `assets` / `templates` / `widgets` / `emojis` / `.siyuan`, as well as anything starting with `.`, **cannot** be used as a notebook or document name.++### How a notebook is recognized++`ListNotebooks` enumerates `data/` and, for each directory:++1. Skip reserved names;+2. Must be a directory (not a file);+3. The directory name must match the NodeID format (`ast.IsNodeIDPattern`);+4. If all the above hold → that directory name is the `box.ID` (notebook ID).++---++## 4. Inside a notebook (★ core: parent-child document pairing)++This is the single most important convention in the whole layout:++> **Document `A`'s on-disk representation = the `A.sy` file + the `A/` directory (the latter exists only if A has children). All of A's child documents live inside `A/`; if a child has its own children, nest an `A/B/` directory — to arbitrary depth.**++Real example (from `find` output):++```+20221126104620-m06prws/ # parent document A's directory+20221126104620-m06prws.sy # parent document A itself+20221126104620-m06prws/20230928134805-z11t56h.sy # A's child B+20221126104620-m06prws/20230928134805-z11t56h/ # B's child directory (grandchild layer)+20221126104620-m06prws/20230928134805-z11t56h/20240304105333-v7g5j1s.sy+```++Code evidence:++- Expanding children (`Ls`): given a `.sy` path, strip the `.sy` suffix, check whether the same-named directory exists, and list its entries if so.+- Child depth (`GetChildDocDepth`): walks the same-named directory accordingly.++> ⚠️ This means: **moving or renaming a document that has children must also move its same-named directory**, otherwise the children get orphaned.++---++## 5. Notebook metadata: `<boxID>/.siyuan/conf.json`++Each notebook directory contains a `.siyuan/` hidden folder with:++| File | Purpose |+|---|---|+| `conf.json` | Notebook config (BoxConf) |+| `sort.json` | Custom sort mapping `{docID: order}` |++The `BoxConf` struct:++| Field | Type | Meaning |+|---|---|---|+| `name` | string | Notebook display name |+| `sort` | int | Sort weight |+| `icon` | string | Icon (emoji hex code, e.g. `"1f3af"`; or a custom-icon filename) |+| `closed` | bool | Whether the notebook is closed |+| `refCreateSaveBox` | string | Target notebook for docs created via block-ref |+| `refCreateSavePath` | string | Target path for docs created via block-ref |+| `docCreateSaveBox` | string | Target notebook for new docs |+| `docCreateSavePath` | string | Target path for new docs |+| `dailyNoteSavePath` | string | Storage path for new daily notes (supports templates) |+| `dailyNoteTemplatePath` | string | Template path for new daily notes |+| `sortMode` | int | Sort mode |++> ⚠️ **The notebook ID is NOT in `conf.json`** — the ID is the notebook directory name itself (see §3).++Read/write sites: `GetConf` / `SaveConf`. The path is hard-coded as `<DataDir>/<boxID>/.siyuan/conf.json`.++---++## 6. Workspace-wide config: `<workspace>/conf/conf.json`++Be careful to distinguish the two `conf.json` files:++| File | Location | Scope |+|---|---|---|+| `conf/conf.json` | **Workspace root** | Workspace-level global config (appearance / langs / system / editor / sync / repo, etc.) |+| `<boxID>/.siyuan/conf.json` | **Inside a notebook** | That notebook only (BoxConf) |++In practice `conf/conf.json` is ~42 KB and holds UI appearance, account, sync, AI, flashcard, and all other workspace-level settings.++---++## 7. Workspace-level hidden config: `data/.siyuan/`++`data/.siyuan/` is different from the per-notebook `.siyuan/` — the former holds **workspace-level** rules and state:++| File | Purpose |+|---|---|+| `syncignore` | Sync-ignore rules (gitignore style) |+| `searchignore` | Search-ignore rules |+| `embeddingignore` | AI-embedding ignore rules |+| `indexignore` | Indexing-ignore rules |+| `refsearchignore` | Ref-search ignore rules |+| `publishAccess.json` | Publishing access control |+| `filesys_status_check/` | File-system consistency-check state |++> There is **no** `conf.json` here — the workspace config lives at the workspace root's `conf/conf.json` (see §6).++---++## 8. Assets++Two coexisting locations:++1. **Global `data/assets/`** — the main one. Naming convention looks like `<orig-name>-<docID-suffix>.<ext>` (e.g. `640-20240927104411-0jh7x96.webp`). Referenced inside documents as the relative path `assets/xxx`.+2. **Per-notebook `<notebook>/assets/`** — mostly used by the built-in guide notebook; ordinary user notebooks generally don't have one.++Asset-link prefix recognition: only `assets/`, `emojis/`, `plugins/`, `public/`, and `widgets/` are accepted as legal asset-link prefixes.++---++## 9. ID and path relationships++### ID format++- **Notebook ID:** `YYYYMMDDHHMMSS-xxxxxx` (14-digit timestamp + `-` + 6 random chars).+- **Document ID:** `YYYYMMDDHHMMSS-xxxxxxx` (14-digit timestamp + `-` + 7 random chars).+- The first 14 digits encode the creation time (extracted by `TimeFromID`).+- Random alphabet is `[a-z0-9]`.++### Filename = ID (hard constraint)++- A document's `.sy` filename (without `.sy`) = the document rootID.+- A notebook's directory name = the notebook ID.+- The kernel enforces `filename ID == root.ID` and auto-corrects mismatches.++### Document absolute path++A document's absolute path on disk = `data/<boxID>/<relative-path>`, where `<relative-path>` looks like `/20221126104620-m06prws/20230928134805-z11t56h.sy` (POSIX style, leading `/`).++Extracting the doc ID from a path (`GetTreeID`): simply `filepath.Base(path)` with the `.sy` suffix removed.++---++## 10. How HPath (human-readable path) is derived++HPath is **not stored** — it is computed on the fly when loading a document (`LoadTreeByData`):++1. Split the document's own relative path by `/`, drop the leading empty segment and the trailing self segment, yielding the **ID segments of each ancestor document**.+2. For each ancestor ID, build `<ancestorID>.sy` and read its `title` via `DocIAL()` (a **streaming read of `Properties` only**, without parsing the whole tree).+3. Join the titles with `/` and append the current document's title → an HPath like `/Parent/Child/Current`.+4. If some ancestor `.sy` is missing, the kernel **auto-creates** an `Untitled` parent (issue #7376).++> This is the fundamental reason "directory structure is data": **moving or renaming a file/directory directly changes the document's HPath and breadcrumbs** — because HPath is entirely derived from the directory chain.++---++## 11. Notes on operating the file system directly++> Consistent with [SY-FORMAT.md §0.5](./SY-FORMAT.md): **prefer HTTP API / MCP / CLI** for mutating data, so the kernel handles index synchronization. Direct file-system operations are only for bulk offline migration, cold init, and similar scenarios.++When writing files directly:++1. ☐ Notebook/document names must match the NodeID format and **avoid reserved names** (§3).+2. ☐ A document's `.sy` filename equals its rootID (§9).+3. ☐ A document with children must have the **paired same-named directory**; moving the parent means moving that directory too (§4).+4. ☐ A notebook's name/icon/closed state lives in `<boxID>/.siyuan/conf.json`, not in `.sy` (§5).+5. ☐ After changes you usually need to **rebuild the index**, or search/block-refs/breadcrumbs will be stale.++---++## 12. Relationship with SY-FORMAT++| Document | Layer | Question answered |+|---|---|---|+| **This one (WORKSPACE)** | File-system layer | "What does a workspace look like on disk? How are notebooks/documents/assets organized?" |+| **SY-FORMAT** | AST layer | "What does the JSON tree inside a `.sy` file look like? What node types/fields exist?" |++The two are complementary: this document tells you where a `.sy` file **lives, what it's named, and who shares its directory**; SY-FORMAT tells you **what's inside** it.+diff --git a/WORKSPACE.zh-CN.md b/WORKSPACE.zh-CN.mdnew file mode 100644index 0000000000..a8a57eedba--- /dev/null+++ b/WORKSPACE.zh-CN.md@@ -0,0 +1,250 @@+# SiYuan 工作区文件系统布局 —— 参考文档++> 本文档描述 SiYuan 工作区在磁盘上的目录与文件组织。+> 它与 [`SY-FORMAT.zh-CN.md`](./SY-FORMAT.zh-CN.md) 互补:后者讲 `.sy` 文件**内部**的 JSON 结构,本文档讲工作区**整体**在文件系统中的布局。+> 所有结论基于真实工作区与内核代码核验。++## 0. 一句话本质++SiYuan 工作区是一棵**自描述**的目录树:笔记本、文档、附件都以可读的文件/目录形式存在磁盘上。**文件名即 ID,目录结构即文档层级**。没有用二进制数据库来记录"文档在哪"——文件系统本身就是真相来源(single source of truth),内核通过遍历目录即可还原全部笔记本与文档结构。++---++## 1. 速查树(总览)++```+<工作区根>/ 例 F:\SiYuan\+├── conf/+│ ├── conf.json # ★工作区总配置(appearance/system/sync/editor...)+│ ├── ca.crt / cert.pem / key.pem # TLS 证书+│ └── windowState.json+├── data/ # DataDir — 所有笔记本数据的根+│ ├── .siyuan/ # 工作区级隐藏配置(*ignore 规则等)+│ ├── assets/ # ★全局附件(图片/音视频/文件)+│ ├── templates/ # 全局模板(.md)+│ ├── widgets/ # 挂件+│ ├── plugins/ # 插件+│ ├── emojis/ # 自定义 emoji+│ ├── snippets/ # 代码片段(CSS/JS)+│ ├── public/ # 静态资源+│ ├── storage/ # 运行时索引/缓存/数据库+│ └── <boxID>/ # ★笔记本(目录名 = 笔记本 ID)+│ ├── .siyuan/+│ │ ├── conf.json # ★笔记本配置(BoxConf:name/icon/closed...)+│ │ └── sort.json # 自定义排序映射 {docID: order}+│ ├── <docID>.sy # 文档(JSON 树;文件名 = 文档 rootID)+│ └── <docID>/ # ★该文档的子文档目录(同名配对)+│ ├── <childID>.sy+│ └── <childID>/ ... # 任意深度嵌套+├── repo/ # 数据仓库(快照/同步)+├── history/ # 编辑历史归档+└── temp/ # 临时/导出文件+```++---++## 2. 工作区根++工作区根由用户选定(如 `F:\SiYuan\`)。判定一个目录是否为合法工作区:`conf/conf.json` 存在且含 `kernelVersion` 字段。++根目录下固定子目录:++| 子目录 | 用途 |+|---|---|+| `conf/` | 工作区级配置 + TLS 证书 |+| `data/` | **所有笔记本数据**(本文档核心) |+| `repo/` | 数据快照仓库(用于同步/历史) |+| `history/` | 编辑历史归档 |+| `temp/` | 临时/导出文件 |+| `corrupted/` | 损坏数据隔离区(内核自动迁入) |++---++## 3. `data/` 顶层布局++`data/` 第一层是两类东西混排:++1. **保留资源目录**:`.siyuan/`、`assets/`、`templates/`、`widgets/`、`plugins/`、`emojis/`、`snippets/`、`public/`、`storage/`。+2. **笔记本目录**:每个是一个以笔记本 ID 命名的文件夹。++### 保留文件名清单(★ AI 落盘时务必避开)++`IsReservedFilename`:++```go+func IsReservedFilename(baseName string) bool {+ return "assets" == baseName || "templates" == baseName || "widgets" == baseName ||+ "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".")+}+```++即 `assets` / `templates` / `widgets` / `emojis` / `.siyuan` 以及任意 `.` 开头的名字**都不能**用作笔记本名或文档名。++### 笔记本的判定规则++`ListNotebooks` 枚举 `data/` 时,逐个目录判定:++1. 跳过保留名;+2. 必须是目录(非文件);+3. 目录名必须符合 NodeID 格式(`ast.IsNodeIDPattern`);+4. 满足以上三条 → 该目录名就是 `box.ID`(笔记本 ID)。++---++## 4. 笔记本内部布局(★ 核心:父子文档配对)++这是整个布局最关键的约定:++> **文档 `A` 的磁盘表示 = `A.sy` 文件 + `A/` 目录(后者仅在 A 有子文档时存在)。A 的子文档全部放在 `A/` 目录里;子文档若有自己的子文档,则再嵌套 `A/B/` 目录,任意深度。**++真实示例(来自 `find` 输出):++```+20221126104620-m06prws/ # 父文档 A 的目录+20221126104620-m06prws.sy # 父文档 A 本体+20221126104620-m06prws/20230928134805-z11t56h.sy # A 的子文档 B+20221126104620-m06prws/20230928134805-z11t56h/ # B 的子目录(孙子层)+20221126104620-m06prws/20230928134805-z11t56h/20240304105333-v7g5j1s.sy+```++代码佐证:++- 展开子文档(`Ls`):传入 `.sy` 路径,去掉 `.sy` 后缀,检查同名目录是否存在,存在则列出其子项。+- 子文档深度(`GetChildDocDepth`):同理 walk 同名目录。++> ⚠️ 这意味着:**移动 / 改名一个有子文档的文档,必须同步移动它的同名目录**,否则子文档会失联。++---++## 5. 笔记本元数据:`<boxID>/.siyuan/conf.json`++每个笔记本目录下有一个 `.siyuan/` 隐藏目录,内含:++| 文件 | 用途 |+|---|---|+| `conf.json` | 笔记本配置(BoxConf) |+| `sort.json` | 文档自定义排序映射 `{docID: order}` |++`BoxConf` 结构:++| 字段 | 类型 | 含义 |+|---|---|---|+| `name` | string | 笔记本显示名称 |+| `sort` | int | 排序权重 |+| `icon` | string | 图标(emoji hex 码,如 `"1f3af"`;或自定义图标文件名) |+| `closed` | bool | 是否处于关闭状态 |+| `refCreateSaveBox` | string | 块引用时新建文档的目标笔记本 |+| `refCreateSavePath` | string | 块引用时新建文档的目标路径 |+| `docCreateSaveBox` | string | 新建文档的目标笔记本 |+| `docCreateSavePath` | string | 新建文档的目标路径 |+| `dailyNoteSavePath` | string | 新建日记的存储路径(支持模板) |+| `dailyNoteTemplatePath` | string | 新建日记的模板路径 |+| `sortMode` | int | 排序方式 |++> ⚠️ **笔记本 ID 不在 `conf.json` 里**——ID 就是笔记本目录名本身(见 §3 的判定规则)。++读写位置:`GetConf` / `SaveConf`。路径硬编码为 `<DataDir>/<boxID>/.siyuan/conf.json`。++---++## 6. 工作区总配置:`<workspace>/conf/conf.json`++注意区分两个 `conf.json`:++| 文件 | 位置 | 作用域 |+|---|---|---|+| `conf/conf.json` | **工作区根** | 工作区级总配置(appearance / langs / system / editor / sync / repo 等全局段) |+| `<boxID>/.siyuan/conf.json` | **笔记本内** | 仅该笔记本(BoxConf) |++实测 `conf/conf.json` 约 42KB,包含 UI 外观、账号、同步、AI、闪卡等全部工作区级设置。++---++## 7. 工作区级隐藏配置:`data/.siyuan/`++`data/.siyuan/` 与笔记本内的 `.siyuan/` 不同,前者是**工作区级**的规则与状态:++| 文件 | 用途 |+|---|---|+| `syncignore` | 同步忽略规则(gitignore 风格) |+| `searchignore` | 搜索忽略 |+| `embeddingignore` | AI embedding 忽略 |+| `indexignore` | 建立索引忽略 |+| `refsearchignore` | 反链搜索忽略 |+| `publishAccess.json` | 发布访问控制 |+| `filesys_status_check/` | 文件系统一致性检查状态 |++> 这里**没有** `conf.json`——工作区总配置在工作区根的 `conf/conf.json`(见 §6)。++---++## 8. 附件(assets)++两套并存:++1. **工作区级全局 `data/assets/`**——主用。命名规则形如 `<原名>-<docID后缀>.<ext>`(如 `640-20240927104411-0jh7x96.webp`)。文档内以 `assets/xxx` 相对路径引用。+2. **笔记本级 `<notebook>/assets/`**——主要是内置 guide 笔记本用,普通用户笔记本一般没有。++附件链接前缀识别:仅认 `assets/`、`emojis/`、`plugins/`、`public/`、`widgets/` 这几个前缀为合法附件链接。++---++## 9. ID 与路径的关系++### ID 格式++- **笔记本 ID**:`YYYYMMDDHHMMSS-xxxxxx`(14 位时间戳 + `-` + 6 位随机)。+- **文档 ID**:`YYYYMMDDHHMMSS-xxxxxxx`(14 位时间戳 + `-` + 7 位随机)。+- 前 14 位即创建时间(`TimeFromID` 提取)。+- 随机字符集 `[a-z0-9]`。++### 文件名 = ID(硬约束)++- 文档的 `.sy` 文件名(去 `.sy` 后缀)= 文档 rootID。+- 笔记本的目录名 = 笔记本 ID。+- 内核强制 `文件名 ID == root.ID`,不一致会自动订正。++### 文档绝对路径++文档在磁盘上的绝对路径 = `data/<boxID>/<相对路径>`,其中 `<相对路径>` 形如 `/20221126104620-m06prws/20230928134805-z11t56h.sy`(以 `/` 开头,POSIX 风格)。++从路径取文档 ID(`GetTreeID`):即 `filepath.Base(路径)` 去掉 `.sy` 后缀。++---++## 10. HPath(人类可读路径)如何派生++HPath **不存储**,加载文档时现场计算(`LoadTreeByData`):++1. 文档自身的相对路径按 `/` 拆分,去掉开头空段和结尾自身段,得到**各级父文档的 ID 段**。+2. 对每个父 ID,拼出 `<父ID>.sy`,用 `DocIAL()`(**流式只读 `Properties`**,不解析整棵树)读出该父文档的 `title`。+3. 把各级 title 用 `/` 串起来,末尾接当前文档 title → 得到形如 `/父标题/子标题/当前标题` 的 HPath。+4. 若某父 `.sy` 缺失,内核会**自动补建**一个 `Untitled` 父文档(对应 issue #7376)。++> 这是"目录结构即数据"的根本原因:**移动或重命名文件/目录,会直接改变文档的 HPath 与面包屑**——因为 HPath 完全派生自目录链。++---++## 11. 直接操作文件系统的注意事项++> 与 [SY-FORMAT.zh-CN.md §0.5](./SY-FORMAT.zh-CN.md) 一致:**修改数据优先走 HTTP API / MCP / CLI**,由内核负责索引同步。直接操作文件系统仅用于批量离线迁移、冷初始化等场景。++直接落盘时务必:++1. ☐ 笔记本/文档命名符合 NodeID 格式,且**避开保留名**(§3)。+2. ☐ 文档 `.sy` 文件名 == 文档 rootID(§9)。+3. ☐ 有子文档的文档,必须**配套同名目录**;移动父文档时连带移动同名目录(§4)。+4. ☐ 笔记本的名称/图标/关闭状态写在 `<boxID>/.siyuan/conf.json`,不在 `.sy` 里(§5)。+5. ☐ 改动后通常需要**重建索引**,否则搜索/块引用/面包屑会失效。++---++## 12. 与 SY-FORMAT 的关系++| 文档 | 范畴 | 回答的问题 |+|---|---|---|+| **本文档(WORKSPACE)** | 文件系统层 | "一个工作区在磁盘上长什么样?笔记本/文档/附件怎么组织?" |+| **SY-FORMAT** | AST 层 | "一个 `.sy` 文件内部的 JSON 树长什么样?有哪些节点类型/字段?" |++两者互补:本文档告诉你 `.sy` 文件**放在哪、叫什么名、与谁同目录**;SY-FORMAT 告诉你 `.sy` 文件**里面写什么**。+