协作办公
在 Obsidian 中用 `.base`…
需要为 Obsidian 笔记创建或编辑 `.base` 文件,并用 YAML 定义 filters、formulas、summaries、table/cards/list/map 视图时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 Obsidian 中用 `.base特定
解决思路
- 在 Obsidian vault 中创建扩展名为 `.base` 的文件,内容必须是合法 YAML。
- 用全局 `filters` 选择笔记范围:可按 tag、folder、property 或 date 过滤;`filters` 可以是单个过滤字符串,也可以是递归过滤对象,但对象只能有 `and`、`or`、`not` 中的一个键。
- 在 `formulas` 段定义可跨视图使用的公式属性,格式为 `formula_name: 'expression'`。
- 用 `properties` 为属性、公式属性和文件属性设置显示名,例如 `property_name.displayName`、`formula.formula_name.displayName`、`file.ext.displayName`。
- 在 `summaries` 段定义自定义汇总公式,例如 `custom_summary_name: 'values.mean().round(3)'`。
- 在 `views` 下配置一个或多个视图:`type` 可选 `table | cards | list | map`,并设置 `name`;可选 `limit` 限制结果数量,`groupBy` 按属性分组并设置 `direction: ASC | DESC`,视图级 `filters` 规则同全局 filters,`order` 指定显示属性顺序,`summaries` 把属性映射到汇总公式。
- 校验 `.base` 文件:确认是合法 YAML 且无语法错误;检查所有引用的属性和公式存在;常见问题包括未加引号的特殊 YAML 字符、公式表达式引号不匹配、引用 `formula.X` 但未在 `formulas` 定义 `X`。
- 在 Obsidian 中打开 `.base` 文件确认视图正确渲染;若显示 YAML 错误,检查下方引号规则。
- 过滤语法:单条件写作 `filters: 'status == "done"'`。
- 过滤语法:`and` 列表内所有条件为真才显示。
- 过滤语法:`or` 列表内任一条件为真就显示。
- 过滤语法:`not` 列表排除匹配项。
- 过滤语法:可嵌套 `or`、`and`、`not`,例如 `or` 中包含 `file.hasTag("tag")`、`and` 中包含 `file.hasTag("book")` 与 `file.hasLink("Textbook")`、`not` 中包含 `file.hasTag("book")` 与 `file.inFolder("Required Reading")`。
- 过滤操作符:`==` 等于,`!=` 不等于,`>` 大于,`<` 小于,`>=` 大于等于,`<=` 小于等于,`&&` 逻辑与,`||` 逻辑或,`!` 逻辑非。
- 属性分三类:笔记属性来自 frontmatter,写作 `note.author` 或 `author`;文件属性写作 `file.name`、`file.mtime` 等;公式属性写作 `formula.my_formula`。
- 使用文件属性:`file.name` 文件名,`file.basename` 无扩展名文件名,`file.path` 完整路径,`file.folder` 父文件夹路径,`file.ext` 扩展名,`file.size` 字节大小,`file.ctime` 创建时间,`file.mtime` 修改时间,`file.tags` 全部标签,`file.links` 内部链接,`file.backlinks` 反向链接文件,`file.embeds` 嵌入内容,`file.properties` 全部 frontmatter 属性。
- 理解 `this`:在主内容区指 base 文件本身;被嵌入时指嵌入它的文件;在侧边栏指主内容区当前活动文件。
- 公式语法:在 `formulas` 中编写表达式,支持算术如 `total: "price * quantity"`。
- 公式语法:条件逻辑用 `if()`,如 `status_icon: 'if(done, "✅", "⏳")'`。
- 公式语法:字符串格式化如 `formatted_price: 'if(price, price.toFixed(2) + " dollars")'`。
- 公式语法:日期格式化如 `created: 'file.ctime.format("YYYY-MM-DD")'`。
- 公式语法:计算创建至今天数用 `days_old: '(now() - file.ctime).days'`。
- 公式语法:计算距截止天数用 `days_until_due: 'if(due_date, (date(due_date) - today()).days, "")'`。
- 常用函数:`date(string): date` 解析字符串为日期(`YYYY-MM-DD HH:mm:ss`);`now(): date` 当前日期时间;`today(): date` 当前日期(时间 `00:00:00`);`if(condition, trueResult, falseResult?)` 条件;`duration(string): duration` 解析时长;`file(path): file` 获取文件对象;`link(path, display?): Link` 创建链接。
- Duration 类型:两个日期相减结果是 Duration,不是数字;可用字段 `duration.days`、`duration.hours`、`duration.minutes`、`duration.seconds`、`duration.milliseconds`。
- Duration 类型:Duration 不支持直接 `.round()`、`.floor()`、`.ceil()`;先访问数字字段如 `.days`,再调用数字函数。正确:`(date(due_date) - today()).days`、`(now() - file.ctime).days`、`(date(due_date) - today()).days.round(0)`;错误:`((date(due) - today()) / 86400000).round(0)`。
- 日期算术:单位支持 `y/year/years`、`M/month/months`、`d/day/days`、`w/week/weeks`、`h/hour/hours`、`m/minute/minutes`、`s/second/seconds`;示例 `now() + "1 day"` 明天,`today() + "7d"` 一周后,`now() - file.ctime` 返回 Duration,`(now() - file.ctime).days` 取天数。
- Table 视图:`type: table`,用 `order` 列出列,用 `summaries` 汇总列,例如 `price: Sum`、`count: Average`。
- Cards 视图:`type: cards`,用 `order` 指定卡片显示属性,例如 `file.name`、`cover_image`、`description`。
- List 视图:`type: list`,用 `order` 指定列表显示属性,例如 `file.name`、`status`。
- Map 视图:`type: map`,需要 latitude/longitude 属性和 Maps 社区插件;设置地图专用 lat/lng 属性。
- 默认汇总公式:`Average` 数值平均;`Min` 最小值;`Max` 最大值;`Sum` 求和;`Range` 数值 Max - Min;`Median` 中位数;`Stddev` 标准差;`Earliest` 最早日期;`Latest` 最晚日期;`Range` 日期 Latest - Earliest;`Checked` 统计 true;`Unchecked` 统计 false;`Empty` 统计空值;`Filled` 统计非空值;`Unique` 统计唯一值。
- 完整示例 Task Tracker Base:用 `filters.and` 选 `file.hasTag("task")` 且 `file.ext == "md"`;定义 `days_until_due`、`is_overdue`、`priority_label` 公式;设置 `properties` 显示名;创建 `Active Tasks` 表格视图,`filters.and` 为 `status != "done"`,`order` 含 `file.name`、`status`、`formula.priority_label`、`due`、`formula.days_until_due`,`groupBy.property: status` 且 `direction: ASC`,`summaries.formula.days_until_due: Average`;创建 `Completed` 表格视图,`filters.and` 为 `status == "done"`,`order` 含 `file.name`、`completed_date`。
- 完整示例 Reading List Base:用 `filters.or` 选 `file.hasTag("book")` 或 `file.hasTag("article")`;定义 `reading_time`、`status_icon`、`year_read` 公式;设置 `properties` 显示名;创建 `Library` cards 视图,`order` 含 `cover`、`file.name`、`author`、`formula.status_icon`,`filters.not` 排除 `status == "dropped"`;创建 `Reading List` table 视图,`filters.and` 为 `status == "to-read"`,`order` 含 `file.name`、`author`、`pages`、`formula.reading_time`。
- 完整示例 Daily Notes Index:用 `filters.and` 选 `file.inFolder("Daily Notes")` 且 `/^\d{4}-\d{2}-\d{2}$/.matches(file.basename)`;定义 `word_estimate: '(file.size / 5).round(0)'` 和 `day_of_week: 'date(file.basename).format("dddd")'`;设置 `properties` 显示名;创建 `Recent Notes` table 视图,`limit: 30`,`order` 含 `file.name`、`formula.day_of_week`、`formula.word_estimate`、`file.mtime`。
- 在 Markdown 中嵌入 Base:使用 `![[MyBase.base]]`;嵌入特定视图使用 `![[MyBase.base#View Name]]`。
- YAML 引号规则:公式包含双引号时,整个公式用单引号包裹,例如 `'if(done, "Yes", "No")'`。
- YAML 引号规则:简单字符串用双引号,例如 `"My View Name"`。
- YAML 引号规则:复杂表达式中正确转义嵌套引号。
- 排错 YAML 语法错误:含 `:`、`{`、`}`、`[`、`]`、`,`、`&`、`*`、`#`、`?`、`|`、`-`、`<`、`>`、`=`、`!`、`%`、`@`、`` ` `` 的字符串必须加引号;错误 `displayName: Status: Active`,正确 `displayName: "Status: Active"`。
- 排错公式引号不匹配:公式内含双引号时,整个公式用单引号包裹;错误 `label: "if(done, "Yes", "No")"`,正确 `label: 'if(done, "Yes", "No")'`。
- 排错 Duration 数学:日期相减返回 Duration 不是数字,始终先访问 `.days`、`.hours` 等;错误 `"(now() - file.ctime).round(0)"`,正确 `"(now() - file.ctime).days.round(0)"`。
- 排错缺少空值检查:属性不一定存在于所有笔记,用 `if()` 保护;错误 `"(date(due_date) - today()).days"`,正确 `'if(due_date, (date(due_date) - today()).days, "")'`。
- 排错引用未定义公式:确保 `order` 或 `properties` 中每个 `formula.X` 都在 `formulas` 中有匹配定义;错误只写 `order: - formula.total`,修复为定义 `formulas: total: "price * quantity"`。
- 引用官方参考:Bases Syntax、Functions、Views、Formulas 和 `references/FUNCTIONS_REFERENCE.md` 获取完整函数参考。
适用边界 · 注意事项
- 不适用于不使用 Obsidian 或不需要 `.base` 文件的场景。
- 不适用于非 Obsidian 的 Markdown 数据库/查询需求。
- Map 视图在缺少 Maps 社区插件或 latitude/longitude 属性时不要使用。