复盘
// 使用 Luxon 处理日期(Obsidian 内置了 Luxon)
const { DateTime } = this.app.plugins.plugins['dataview'].api.luxon;
// 从当前文件名中提取周数(格式:2025-W33)
const currentFile = dv.current().file;
const fileName = currentFile.name;
const weekMatch = fileName.match(/(\d{4}-W\d{2})/);
let weekStart, weekEnd;
if (weekMatch) {
// 从文件名中提取周数
const targetWeek = weekMatch[1];
const [year, weekNumber] = targetWeek.split('-W').map(Number);
// 使用 Luxon 正确处理 ISO 周
weekStart = DateTime.fromObject({
weekYear: year,
weekNumber: weekNumber
}).startOf('week'); // 周一
weekEnd = DateTime.fromObject({
weekYear: year,
weekNumber: weekNumber
}).endOf('week'); // 周日
// 显示日期范围确认
const days = ["周一", "周二", "周三", "周四", "周五", "周六","周日"];
dv.el("p", `本周日期范围: ${weekStart.toFormat('yyyy-MM-dd')} (${days[weekStart.weekday-1]}) 到 ${weekEnd.toFormat('yyyy-MM-dd')} (${days[weekEnd.weekday-1]})`);
} else {
dv.el("p", "❌ 文件名格式不正确");
// 设置无效日期范围
weekStart = DateTime.fromMillis(0);
weekEnd = DateTime.fromMillis(0);
}
// 2. 搜索所有包含 "Daily" 的文件
const allDailyNotes = dv.pages('"001日记"')
.where(p => p.file.path.includes("Daily"));
// 3. 筛选本周的文件
const dailyNotes = allDailyNotes
.where(p => {
try {
const fileName = p.file.name;
const noteDate = dv.date(fileName);
return noteDate >= weekStart && noteDate <= weekEnd;
} catch (e) {
return false;
}
});
if (dailyNotes.length === 0) {
dv.el("p", "未找到本周的日记文件。");
} else {
// 按日期分组高亮内容
let dailyHighlights = {};
for (let note of dailyNotes) {
const day = note.file.name; // 日期作为文件名
dailyHighlights[day] = dailyHighlights[day] || [];
// 方法1: 使用 Dataview 的 file.tasks 和 file.lists
const tasks = note.file.tasks || [];
const lists = note.file.lists || [];
// 检查任务中的加粗内容
for (let task of tasks) {
if (task.text && task.text.includes("**")) {
const boldMatches = task.text.match(/\*\*([^*]+?)\*\*/g);
if (boldMatches) {
boldMatches.forEach(match => {
const cleanMatch = match.replace(/\*\*/g, '').trim();
dailyHighlights[day].push(cleanMatch);
});
}
}
}
// 检查列表中的加粗内容
for (let list of lists) {
if (list.text && list.text.includes("**")) {
const boldMatches = list.text.match(/\*\*([^*]+?)\*\*/g);
if (boldMatches) {
boldMatches.forEach(match => {
const cleanMatch = match.replace(/\*\*/g, '').trim();
dailyHighlights[day].push(cleanMatch);
});
}
}
}
// 方法2: 尝试使用 app.vault 直接读取文件内容
try {
const fileContent = await dv.io.load(note.file.path);
if (fileContent && fileContent.includes("**")) {
const boldMatches = fileContent.match(/\*\*([^*]+?)\*\*/g);
if (boldMatches) {
boldMatches.forEach((match, index) => {
const cleanMatch = match.replace(/\*\*/g, '').trim();
// 过滤掉太短或格式化的内容
if (cleanMatch.length > 2 && !cleanMatch.startsWith("---")) {
dailyHighlights[day].push(cleanMatch);
}
});
}
}
} catch (e) {
console.log(`无法读取文件 ${note.file.path}: ${e.message}`);
}
// 对当天的内容去重
dailyHighlights[day] = [...new Set(dailyHighlights[day])];
}
dv.header(4, `本周高亮内容`);
// 按日期顺序显示高亮内容
let hasHighlights = false;
// 按日期排序(从新到旧)
const sortedDays = Object.keys(dailyHighlights).sort((a, b) => dv.date(b) - dv.date(a));
for (let day of sortedDays) {
if (dailyHighlights[day].length > 0) {
hasHighlights = true;
// 显示日期标题
dv.el("p", `**${day}**`, { attr: { style: "margin: 16px 0 8px 0; font-weight: bold;" } });
// 显示该日的高亮内容
dailyHighlights[day].forEach(item => {
dv.el("p", `• ${item}`, { attr: { style: "margin: 4px 0; line-height: 1.6; padding-left: 12px;" } });
});
}
}
if (!hasHighlights) {
dv.el("p", "本周日记中没有找到加粗的高亮内容。");
}
}
// 使用 Luxon 处理日期(Obsidian 内置了 Luxon)
const { DateTime } = this.app.plugins.plugins['dataview'].api.luxon;
// 从当前文件名中提取周数(格式:2025-W33)
const currentFile = dv.current().file;
const fileName = currentFile.name;
const weekMatch = fileName.match(/(\d{4}-W\d{2})/);
let weekStart, weekEnd;
if (weekMatch) {
// 从文件名中提取周数
const targetWeek = weekMatch[1];
const [year, weekNumber] = targetWeek.split('-W').map(Number);
// 使用 Luxon 正确处理 ISO 周
weekStart = DateTime.fromObject({
weekYear: year,
weekNumber: weekNumber
}).startOf('week'); // 周一
weekEnd = DateTime.fromObject({
weekYear: year,
weekNumber: weekNumber
}).endOf('week'); // 周日
// 显示日期范围确认
const days = ["周一", "周二", "周三", "周四", "周五", "周六","周日"];
//dv.el("p", `本周日期范围: ${weekStart.toFormat('yyyy-MM-dd')} (${days[weekStart.weekday-1]}) 到 ${weekEnd.toFormat('yyyy-MM-dd')} (${days[weekEnd.weekday-1]})`);
} else {
dv.el("p", "❌ 文件名格式不正确");
// 设置无效日期范围
weekStart = DateTime.fromMillis(0);
weekEnd = DateTime.fromMillis(0);
}
// 如果日期范围无效,就不执行后续代码
if (weekStart.toMillis() === 0 && weekEnd.toMillis() === 0) {
// 什么都不做,直接结束
} else {
// 搜索所有包含 "Daily" 的文件
const allDailyNotes = dv.pages('"001日记"')
.where(p => p.file.path.includes("Daily"));
// 筛选本周的文件
const dailyNotes = allDailyNotes
.where(p => {
try {
const fileName = p.file.name;
const noteDate = dv.date(fileName);
return noteDate >= weekStart && noteDate <= weekEnd;
} catch (e) {
return false;
}
});
if (dailyNotes.length === 0) {
dv.el("p", "未找到本周的日记文件。");
} else {
let allTasks = [];
for (let note of dailyNotes) {
// 获取文件中的所有任务
const tasks = note.file.tasks || [];
// 为每个任务添加日期信息
tasks.forEach(task => {
// 过滤掉空任务并移除状态标记
if (task.text && task.text.trim()) {
// 移除任务状态标记 [ ] 或 [x]
const cleanText = task.text.replace(/^\[[ x]\]\s*/, '').trim();
if (cleanText) {
allTasks.push({
text: cleanText,
completed: task.completed,
created: dv.date(note.file.name),
completedDate: task.completed ? dv.date(note.file.name) : null,
sourceFile: note.file.name
});
}
}
});
}
// 去重:基于任务文本内容
const uniqueTasks = [];
const seenTexts = new Set();
for (let task of allTasks) {
if (!seenTexts.has(task.text)) {
seenTexts.add(task.text);
uniqueTasks.push(task);
}
}
// 按完成状态和日期排序
uniqueTasks.sort((a, b) => {
if (a.completed && !b.completed) return -1;
if (!a.completed && b.completed) return 1;
return b.created - a.created;
});
// 计算完成率
const totalTasks = uniqueTasks.length;
const completedTasks = uniqueTasks.filter(t => t.completed).length;
const completionRate = totalTasks > 0 ? ((completedTasks / totalTasks) * 100).toFixed(0) : 0;
// 渲染统计信息
dv.header(4, `任务统计:共 ${totalTasks} 项,完成 ${completedTasks} 项,完成率 ${completionRate}%`);
if (uniqueTasks.length > 0) {
// 直接使用dv.list,但设置简单的格式
const taskList = uniqueTasks.map(task =>
`${task.completed ? "✅" : "⭐"} ${task.text} ${task.completed ? `(完成于: ${task.completedDate.toISODate()})` : ""}`
);
// 创建一个简单的容器,去除默认样式
dv.el("div", taskList.join('<br>'));
} else {
dv.el("p", "📋 本周没有收集到任务。");
}
}
}