先做备份
在进行任何批量修改之前,一定要先复制一份你的 Obsidian 库。即使一切运行正常,也难免出错。另外,为了把风险降到最低,我建议使用手动方式。
🎯 我们要替换什么?
以前笔记是这样开头的:
我目前使用的链接模板:
This format:
- isn’t read by the Dataview plugin
- interferes with the note’s structure
- requires manual editing
We want to move to YAML:
What the YAML template looks like:
And here’s what the YAML looks like as source code:
---
input links:
- '[[PSYCHOLOGY]]'
tags:
- psychology
- blog
date: 24-05-2025
---
And the display of backlinks can be moved to the start of the note. How? Let’s look at it below.
🔗 Backlinks — a built-in feature from Obsidian
Instead of Output links with [[Dataview]] code, we now use:
- built-in backlinks (enabled in the settings)
What "Backlinks" look like:
![]()
🎨 How to move the backlinks to the top, next to the YAML, using CSS
- Open the CSS snippets folder:
The CSS snippets folder:
-
Create a CSS document (first create an ordinary notepad file → “Save as” → “All formats” → just add .css to the file name → e.g. backlinks.css)
-
Add this code to
publish.cssorobsidian.css:
.cm-sizer {
display: flex;
flex-direction: column;
}
.inline-title {
order: 1;
}
.metadata-container {
order: 2;
}
.embedded-backlinks {
order: 3;
position: static;
overflow-y: auto;
max-height: 300px;
}
.cm-contentContainer {
order: 4;
}
What the code looks like in a notepad (css):
👉 Now the backlinks will be right under the YAML block. This is convenient for quickly viewing your connections.
🔁 3 ways to move to YAML
1. 🐍 A Python script (for the experienced)
# 见下文——务必把路径改成你自己的库路径(Vault_Path),并检查搜索模板
Warning! This method mass-replaces text. Don't run it if you're not sure. Better consult ChatGPT before running.
Here’s the code itself:
import os
import re
VAULT_PATH = r"C:\delete after\Tutorial 01"
# 灵活的正则:允许可选的空格、换行,以及缺少某些字段
pattern = re.compile(
r"(?:)?",
re.DOTALL
)
def convert_file(filepath):
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
if content.strip().startswith("---"):
return False
match = pattern.search(content)
if not match:
return False
input_raw = match.group(1).strip()
tags_raw = match.group(2).strip()
date = match.group(3).strip() if match.group(3) else None
# 格式化链接:'[[...]]'
input_links = re.findall(r"\[\[(.*?)\]\]", input_raw)
input_yaml = "\n".join([f" - '[[{link.strip()}]]'" for link in input_links])
# 格式化标签
tags = [tag.strip("# ").strip() for tag in tags_raw.split()]
tags_yaml = "\n".join([f" - {tag}" for tag in tags]) if tags else " - "
# 构建 YAML
yaml = [
"---",
"input links:",
input_yaml if input_yaml else " - ",
"tags:",
tags_yaml,
]
if date:
yaml.append(f"date: {date}")
yaml.append("---")
# 删除整个旧区块,并添加 YAML
new_content = "\n".join(yaml) + "\n\n" + content[match.end():].lstrip()
with open(filepath, "w", encoding="utf-8") as f:
f.write(new_content)
print(f"✅ Updated: {filepath}")
return True
def process_vault():
print("🚀 Starting processing...")
count = 0
for root, _, files in os.walk(VAULT_PATH):
for file in files:
if file.endswith(".md"):
full_path = os.path.join(root, file)
if convert_file(full_path):
count += 1
print(f"🎉 Done! Files updated: {count}")
if __name__ == "__main__":
process_vault()
如何根据自己的情况调整?
把
VAULT_PATH改成你自己库的路径把这段代码发给 ChatGPT,并写上:“
请帮我把它改成适配我的模板。下面是一篇示例笔记……”
2. 🔍 Global Search and Replace 插件
适合处理模板字符串。例如:
- 查找:
...%%区块
- 启用反向链接,并用 CSS 把它们放到顶部
📌 总结
迁移到 YAML 的关键在于条理:告别手写代码,使用 Obsidian 的内置功能,让你的系统更整洁。
想要一个适配你自己库的示例吗?去问问 ChatGPT:
“这是我以前的模板。请帮我改成 YAML 格式,并说明哪些行我可以手动插入。”
继续阅读?





