Last active
December 26, 2024 05:41
-
-
Save Moyf/b14c3374b49ce53e1be7ca939ced611e to your computer and use it in GitHub Desktop.
TP Script: Add done and finished date into Frontmatter (Property)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%* | |
/* | |
- Author: Moy | |
- Create Time: 2024-07-30 | |
- Description: 这是一个用来修改当前文件的元数据的范例脚本 | |
- Version: 1.0 | |
*/ | |
// 配置项 | |
// 两个元数据的属性名,一个是 bool 类型(打勾),另一个是日期类型 | |
const propDone = "done"; | |
const propFinishedDay = "finished_date"; | |
// 这里填写日记的格式 | |
const dateFormat = "YYYY-MM-DD"; | |
const todayDate = moment().format(dateFormat); | |
// 第一种获得笔记的方法,按照路径匹配,应该会更准确一些 | |
const notePath = tp.file.path(true); // 这里要使用相对路径,否则下面的函数读取不到的 | |
const fileObj = app.vault.getAbstractFileByPath(notePath); | |
// 第二种方法,用笔记名匹配 | |
const noteName = tp.file.title; | |
// const fileObj = tp.file.find_tfile(noteName); | |
function updateFM( fm ) { | |
fm[propDone] = true; | |
fm[propFinishedDay] = todayDate; | |
new Notice(`你在 ${todayDate} 完成了:\n${noteName}\n恭喜! 🎉`); | |
} | |
if (fileObj) { | |
app.fileManager.processFrontMatter(fileObj, (fm) => { | |
if (fm[propDone]) { | |
tp.system.prompt("已存在 Done 属性,是否刷新完成日期?", "是") | |
.then((confirm) => { | |
if (confirm == "是") { | |
updateFM(fm); | |
} else { | |
new Notice("取消更新完成时间"); | |
} | |
}); | |
} else { | |
updateFM(fm); | |
} | |
}); | |
} | |
%> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy this text into your TP folder and just call this template.
把这个文本复制到你的 TP 文件夹里,然后调用该模板即可