graph-colorize
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGraph Colorize — Color-code the Obsidian Graph View
Graph Colorize — 为Obsidian图视图添加颜色编码
You are rewriting so Obsidian's graph view tints nodes by tag, folder, or visibility.
$OBSIDIAN_VAULT_PATH/.obsidian/graph.jsonObsidian stores graph settings in . The array is a list of pairs; the first matching query wins per node. Queries use Obsidian's search syntax: , , , etc. Color is where the int is .
<vault>/.obsidian/graph.jsoncolorGroups{query, color}tag:#foopath:"concepts"file:foo{"a": 1, "rgb": <packed-int>}(R << 16) | (G << 8) | B你将重写,使Obsidian的图视图按标签、文件夹或可见性为节点着色。
$OBSIDIAN_VAULT_PATH/.obsidian/graph.jsonObsidian将图设置存储在中。数组是一组键值对;每个节点会匹配第一个符合条件的查询。查询使用Obsidian的搜索语法:、、等。颜色格式为,其中整数由计算得出。
<vault>/.obsidian/graph.jsoncolorGroups{query, color}tag:#foopath:"concepts"file:foo{"a": 1, "rgb": <packed-int>}(R << 16) | (G << 8) | BBefore You Start
开始之前
- Read , or fall back to
~/.obsidian-wiki/configin this repo, to get.env.OBSIDIAN_VAULT_PATH - Confirm exists. If it doesn't, the vault has never been opened in Obsidian — tell the user to open the vault once in Obsidian, then re-run.
$OBSIDIAN_VAULT_PATH/.obsidian/ - Warn the user if Obsidian is likely open: Obsidian overwrites on close. Tell them to close the vault first, or be ready to reload (Cmd/Ctrl+R) and not touch the graph settings until they reload.
graph.json
- 读取,若不存在则读取本仓库中的
~/.obsidian-wiki/config文件,获取.env。OBSIDIAN_VAULT_PATH - 确认目录存在。若不存在,说明该库从未在Obsidian中打开过,请告知用户先在Obsidian中打开一次库,然后重新运行此技能。
$OBSIDIAN_VAULT_PATH/.obsidian/ - 若Obsidian可能处于打开状态,需向用户发出警告:Obsidian会在关闭时覆盖。请用户先关闭库,或准备好重新加载(Cmd/Ctrl+R),且在重新加载前不要修改图设置。
graph.json
Step 1: Pick a Mode
步骤1:选择模式
Infer the mode from the user's phrasing. If ambiguous, default to by-tag.
| User intent | Mode |
|---|---|
| "color by tag", "color my graph", "make it colorful" (default) | |
| "color by folder", "color by category", "color by directory" | |
| "highlight visibility", "show internal/pii in graph", "visibility colors" | |
User provides explicit mapping ( | |
| "combine tag and visibility" / "both" | |
根据用户的表述推断模式。若表述模糊,默认使用by-tag模式。
| 用户意图 | 模式 |
|---|---|
| "color by tag"、"color my graph"、"make it colorful"(默认) | |
| "color by folder"、"color by category"、"color by directory" | |
| "highlight visibility"、"show internal/pii in graph"、"visibility colors" | |
用户提供明确映射(如 | |
| "combine tag and visibility" / "both" | |
Step 2: Build the colorGroups
Array
colorGroups步骤2:构建colorGroups
数组
colorGroupsPalette (10 distinct, colorblind-friendly colors)
调色板(10种区分度高、色弱友好的颜色)
Use in order. If more groups than colors, cycle and add a lightness shift by dividing brightness ~20% via a second pass — or just cap at 10 and tell the user the remaining tags share the "other" color.
| # | Hex | rgb (packed int) | Role |
|---|---|---|---|
| 0 | | | blue |
| 1 | | | orange |
| 2 | | | red |
| 3 | | | teal |
| 4 | | | green |
| 5 | | | yellow |
| 6 | | | purple |
| 7 | | | pink |
| 8 | | | brown |
| 9 | | | gray |
Every color is wrapped as .
{"a": 1, "rgb": <int>}按顺序使用。若分组数量超过颜色数量,循环使用并通过第二次调整将亮度降低约20%来实现明度偏移;或最多保留10组,并告知用户剩余标签将使用“其他”颜色。
| # | 十六进制 | rgb(压缩整数) | 用途 |
|---|---|---|---|
| 0 | | | 蓝色 |
| 1 | | | 橙色 |
| 2 | | | 红色 |
| 3 | | | 蓝绿色 |
| 4 | | | 绿色 |
| 5 | | | 黄色 |
| 6 | | | 紫色 |
| 7 | | | 粉色 |
| 8 | | | 棕色 |
| 9 | | | 灰色 |
每种颜色需包装为格式。
{"a": 1, "rgb": <int>}Mode: by-tag
by-tag模式:by-tag
by-tag- Glob excluding
$VAULT_PATH/**/*.md,_archives/,_raw/,.obsidian/,node_modules/,index.md,log.md._insights.md - Parse frontmatter from each page. Count usage per tag.
tags - Drop tags from the frequency list — they are reserved system tags, handled only in
visibility/*orby-visibilitymode.combined - Take the top 10 tags by usage. If there are fewer than 10 unique tags, use all of them.
- For each tag at index
T: emiti.{"query": "tag:#T", "color": palette[i]} - Optionally, append a final catch-all entry for untagged pages at the end: — skip if color slot 9 is already taken by a real tag.
{"query": "-[\"tag\":]", "color": palette[9]}
- 遍历,排除
$VAULT_PATH/**/*.md、_archives/、_raw/、.obsidian/、node_modules/、index.md、log.md。_insights.md - 解析每个页面的前置元数据,统计每个标签的使用次数。
tags - 从频率列表中移除标签——这些是保留的系统标签,仅在
visibility/*或by-visibility模式下处理。combined - 选取使用次数最多的前10个标签。若唯一标签数量少于10,则全部使用。
- 对于索引为的标签
i,生成T。{"query": "tag:#T", "color": palette[i]} - 可选:在末尾添加一个针对未标记页面的兜底条目:——若第9个颜色槽已被真实标签占用,则跳过此步骤。
{"query": "-[\"tag\":]", "color": palette[9]}
Mode: by-category
by-category模式:by-category
by-categoryUse the seven vault top-level folders in this fixed order so colors are stable across runs:
| Folder | Color index |
|---|---|
| 0 (blue) |
| 1 (orange) |
| 2 (red) |
| 3 (teal) |
| 4 (green) |
| 5 (yellow) |
| 6 (purple) |
Emit one entry per folder that exists AND contains at least one file. Each entry is:
.mdjson
{"query": "path:\"<folder>\"", "color": {"a": 1, "rgb": <int>}}按以下固定顺序使用库的7个顶级文件夹,确保每次运行时颜色保持稳定:
| 文件夹 | 颜色索引 |
|---|---|
| 0(蓝色) |
| 1(橙色) |
| 2(红色) |
| 3(蓝绿色) |
| 4(绿色) |
| 5(黄色) |
| 6(紫色) |
为每个存在且包含至少一个文件的文件夹生成一个条目。每个条目格式如下:
.mdjson
{"query": "path:\"<folder>\"", "color": {"a": 1, "rgb": <int>}}Mode: by-visibility
by-visibility模式:by-visibility
by-visibilityEmit exactly three entries, in this order (first-match wins, so most restrictive comes first):
- →
visibility/pii(red, rgb 14767961)#E15759 - →
visibility/internal(orange, rgb 15896107)#F28E2B - →
visibility/public(green, rgb 5873999)#59A14F
json
{"query": "tag:#visibility/pii", "color": {"a": 1, "rgb": 14767961}}Pages with no tag remain Obsidian's default color — do not add a catch-all.
visibility/按以下顺序生成恰好3个条目(优先匹配第一个,因此限制最严格的条目排在最前):
- →
visibility/pii(红色,rgb 14767961)#E15759 - →
visibility/internal(橙色,rgb 15896107)#F28E2B - →
visibility/public(绿色,rgb 5873999)#59A14F
json
{"query": "tag:#visibility/pii", "color": {"a": 1, "rgb": 14767961}}没有标签的页面将保留Obsidian的默认颜色——不要添加兜底条目。
visibility/Mode: combined
combined模式:combined
combinedEmit entries first, then entries. Visibility wins on conflict because it appears first in the list.
by-visibilityby-tag先生成模式的条目,再生成模式的条目。由于可见性条目排在前面,冲突时将优先匹配可见性。
by-visibilityby-tagMode: custom
custom模式:custom
customIf the user gave explicit mappings, honor them literally. Convert any hex they give (e.g. ) to packed int using . Wrap each as .
#FF00FFint(hex_without_hash, 16){"a": 1, "rgb": <int>}若用户提供了明确的映射,严格按照其要求执行。将用户提供的任何十六进制颜色(如)转换为压缩整数,方法是使用。将每个映射包装为格式。
#FF00FFint(hex_without_hash, 16){"a": 1, "rgb": <int>}Step 3: Merge into graph.json (Do Not Clobber)
步骤3:合并到graph.json(不覆盖原有设置)
-
Read the existing. If it doesn't exist, start from this minimal default:
$VAULT_PATH/.obsidian/graph.jsonjson{ "collapse-filter": true, "search": "", "showTags": false, "showAttachments": false, "hideUnresolved": false, "showOrphans": true, "collapse-color-groups": false, "colorGroups": [], "collapse-display": true, "showArrow": false, "textFadeMultiplier": 0, "nodeSizeMultiplier": 1, "lineSizeMultiplier": 1, "collapse-forces": true, "centerStrength": 0.518713248970312, "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, "scale": 1, "close": true } -
Back up first: copy the existing file tobefore writing. If a backup from the same minute exists, reuse it — don't pile up duplicates.
.obsidian/graph.json.backup-<YYYYMMDD-HHMM> -
Replace only thefield with your new array. Leave every other field untouched. This preserves the user's zoom, physics, filter, search, and display preferences.
colorGroups -
Write the file back with the same JSON style as the original (usually compact single-line or 2-space indent — preserve what's there).
-
读取现有的。若文件不存在,从以下最小默认配置开始:
$VAULT_PATH/.obsidian/graph.jsonjson{ "collapse-filter": true, "search": "", "showTags": false, "showAttachments": false, "hideUnresolved": false, "showOrphans": true, "collapse-color-groups": false, "colorGroups": [], "collapse-display": true, "showArrow": false, "textFadeMultiplier": 0, "nodeSizeMultiplier": 1, "lineSizeMultiplier": 1, "collapse-forces": true, "centerStrength": 0.518713248970312, "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, "scale": 1, "close": true } -
先备份:在写入前将现有文件复制到。若同一分钟内的备份已存在,复用该备份——避免重复创建。
.obsidian/graph.json.backup-<YYYYMMDD-HHMM> -
仅将字段替换为新数组。保留所有其他字段不变。这样可以保留用户的缩放、物理效果、过滤器、搜索和显示偏好。
colorGroups -
按照原始文件的JSON格式将文件写回(通常为紧凑单行或2空格缩进——保留原有格式)。
Step 4: Report and Log
步骤4:报告与日志
Print a summary like:
Graph colorized → .obsidian/graph.json
Mode: by-tag
Groups: 7 color assignments
Palette: blue, orange, red, teal, green, yellow, purple
Backup: .obsidian/graph.json.backup-20260424-1432
Reload Obsidian (Cmd/Ctrl+R) to see the new colors.
If Obsidian is currently open, close it first OR reload immediately — Obsidian
overwrites graph.json on close and can erase these changes.Append to :
$VAULT_PATH/log.md- [TIMESTAMP] GRAPH_COLORIZE mode=<mode> groups=<N> backup=graph.json.backup-<stamp>打印如下摘要:
图已完成颜色编码 → .obsidian/graph.json
模式: by-tag
分组: 7个颜色分配
调色板:蓝色、橙色、红色、蓝绿色、绿色、黄色、紫色
备份: .obsidian/graph.json.backup-20260424-1432
重新加载Obsidian(Cmd/Ctrl+R)即可查看新颜色。
若Obsidian当前处于打开状态,请先关闭它,或立即重新加载——Obsidian会在关闭时覆盖graph.json,可能会清除这些更改。在中追加内容:
$VAULT_PATH/log.md- [TIMESTAMP] GRAPH_COLORIZE mode=<mode> groups=<N> backup=graph.json.backup-<stamp>Edge Cases
边缘情况
- No tags in vault in mode → fall back to
by-tagand tell the user.by-category - User wants to undo → restore from the latest and note that in
graph.json.backup-*.log.md - User wants to clear all color groups → set , back up, log as
colorGroups: [].GRAPH_COLORIZE mode=clear - missing → the vault hasn't been opened in Obsidian yet. Tell the user to open it once, then re-run. Don't create
.obsidian/yourself — Obsidian populates many files there on first open..obsidian/ - Query syntax gotchas: folder paths with spaces need quoting (); tags with nested slashes work literally (
path:"my folder"); don't URL-encode.tag:#visibility/internal - Obsidian open during edit: surface the risk — Obsidian reads graph.json at startup and rewrites it on close. If the user is editing live, tell them to close Obsidian first or run the reload (Cmd/Ctrl+R) immediately and avoid opening graph settings before they do.
- 模式下库中无标签 → 切换为
by-tag模式并告知用户。by-category - 用户想要撤销操作 → 从最新的恢复,并在
graph.json.backup-*中记录此事。log.md - 用户想要清除所有颜色分组 → 将设置为
colorGroups,执行备份,并记录为[]。GRAPH_COLORIZE mode=clear - 目录缺失 → 该库尚未在Obsidian中打开过。告知用户先打开一次库,然后重新运行此技能。不要自行创建
.obsidian/目录——Obsidian会在首次打开时自动生成其中的多个文件。.obsidian/ - 查询语法注意事项:包含空格的文件夹路径需要加引号();包含嵌套斜杠的标签可直接使用(
path:"my folder");无需进行URL编码。tag:#visibility/internal - 编辑时Obsidian处于打开状态:提示风险——Obsidian在启动时读取graph.json,并在关闭时重写它。若用户正在实时编辑,请告知用户先关闭Obsidian,或立即重新加载(Cmd/Ctrl+R),且在重新加载前不要打开图设置。
Notes
注意事项
- This is a pure config edit — no page content changes, no frontmatter writes.
- Re-running is safe: each run creates a new backup, only is rewritten.
colorGroups - If the user has manually curated color groups they want to keep, offer mode or ask before overwriting.
combined - The palette here matches 's
wiki-exportcommunity colors, so the Obsidian graph and the exported visualization look consistent.graph.html
- 这是纯配置编辑——不会修改页面内容或前置元数据。
- 重复运行是安全的:每次运行都会创建新备份,仅重写字段。
colorGroups - 若用户有手动整理的颜色分组想要保留,可提供模式,或在覆盖前询问用户。
combined - 此处使用的调色板与的
wiki-export社区颜色匹配,因此Obsidian图和导出的可视化效果保持一致。graph.html