Skip to content

Home Word Count Dashboard There are two bugs in task counting and progress calculation #236

Description

@xulaoshicom

40 - Obsidian/主页/00. 主页.md 内嵌的 dataviewjs 仪表盘代码有两处 bug,导致多种场景下进度条无法正常显示。

Bug 1: 任务计数只扫正文 - [x] / - [ ],忽略 frontmatter 的 milestones[]
许多项目笔记(如 Projects/脱贫地区公共体育服务.md、Projects/完整社区体育服务标准化.md)
已经在 frontmatter 用结构化的 milestones: 块管理任务进度(含 done: true/false),
但仪表盘代码(40 - Obsidian/主页/00. 主页.md 第 428-454 行)只对正文的
markdown 复选框敏感:

let tasks = content.match(/- [ ] /g);
let finishedTasks = content.match(/- [x] /g)

结果:已有结构化进度的项目,仪表盘仍然显示 0/0 = NaN,进度条画不出来。

Bug 2: section.target 不是数字时直接算 NaN

else share = (wcCount / section.target);

很多项目/论文把 target 字段占用为「目标期刊名」(字符串)或留空,
导致 share = NaN,整个进度条空白。

触发条件
打开 40 - Obsidian/主页/00. 主页.md
滚到 PROJECT TRACKING 区段
任意带 milestones: 但正文无 - [x] / - [ ] 的项目,进度列空白
任意 target: 是字符串的论文,进度列空白
期望修复
任务计数改为「双源取数」:优先读 frontmatter 的 milestones[].done,
没有时回退到正文 - [x] / - [ ] 匹配
section.target 加类型判断:仅当它是正数时直接计算;
否则回落到页面级 target(默认 70000)
建议实现(参考,已在本仓库验证有效)

// 修复 1:双源取数
const sectionFile = app.vault.getAbstractFileByPath(section.file.path);
const sectionCache = app.metadataCache.getFileCache(sectionFile);
const sectionMilestones = sectionCache?.frontmatter?.milestones;

let taskNum = 0;
let finishedTasksNum = 0;

if (Array.isArray(sectionMilestones) && sectionMilestones.length > 0) {
for (const m of sectionMilestones) {
if (m && typeof m = "object" && "task" in m) {
if (m.done = true) finishedTasksNum++;
else taskNum++;
}
}
} else {
const tasks = content.match(/- [ ] /g);
const finishedTasks = content.match(/- [x] /g);
if (tasks) taskNum = tasks.length;
if (finishedTasks) finishedTasksNum = finishedTasks.length;
}

// 修复 2:三分支 fallback
if (section.target == "tasks") share = (finishedTasksNum / (taskNum + finishedTasksNum));
else if (typeof section.target === "number" && section.target > 0) share = (wcCount / section.target);
else share = (wcCount / target);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions