tp_new 更新:PARA改造
模板代码
<%*
/* ==================== 配置区 ==================== */
const CONFIG = {
AUTHOR: "lspzc",
BASE_FOLDER: "md",
DOC_TYPES: ["md", "index"],
STATE_OPTIONS: ["doing", "todo", "update", "done"],
INDEX_TYPE: ["Projects", "Areas", "Resources", "Archives"],
};
/* ==================== 工具函数 ==================== */
// 生成 YYYYMMDD_ 格式日期前缀
const getDatePrefix = () => {
const pad = (n) => String(n).padStart(2, "0");
const now = new Date();
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_`;
};
// 构建 Frontmatter 的链式对象
const createFrontmatterBuilder = () => {
const properties = [];
let closed = false;
const builder = {
add(key, value, condition = true) {
if (!closed && condition) {
properties.push(`${key}: ${value}`);
}
return builder;
},
close() {
closed = true;
return `---\n${properties.join("\n")}\n---\n\n`;
},
};
return builder;
};
// 获取标题逻辑
const promptForTitle = async (indexType) => {
if (indexType) {
const indexTitle = await tp.system.prompt("请输入索引名称");
return indexTitle ? `${indexType}_${indexTitle}` : null;
}
const title = await tp.system.prompt("请输入文档标题", getDatePrefix());
return title ? title : null;
};
// 构建内容区域
const buildContent = (title) => {
let content = "";
const regex = /^(\d{8}|Projects|Areas|Resources|Archives)_/;
const cleanTitle = title.replace(regex, "");
content += `# ${cleanTitle}\n\n`;
return content;
};
/* ==================== 主流程 ==================== */
try {
// 步骤1: 选择文档类型
const mdType = await tp.system.suggester(
CONFIG.DOC_TYPES,
CONFIG.DOC_TYPES,
true,
"新建文件类型"
);
// 步骤2: 获取标题
let indexType = false;
if (mdType == "index") {
indexType = await tp.system.suggester(
CONFIG.INDEX_TYPE,
CONFIG.INDEX_TYPE,
true,
"选择索引领域"
);
}
const title = await promptForTitle(indexType);
if (!title) return;
// 步骤3: 处理 md 类型文档
if (mdType == "md") {
const state = await tp.system.suggester(
CONFIG.STATE_OPTIONS,
CONFIG.STATE_OPTIONS,
true,
"选择笔记状态"
);
const addNumbering = await tp.system.suggester(
["添加标题编号", "不添加编号"],
[true, false],
true,
"标题编号设置"
);
await tp.file.include("[[module_getTags]]");
const tags = tp.config.selectedTags;
// 构建 frontmatter
const frontmatter = createFrontmatterBuilder()
.add("author", CONFIG.AUTHOR)
.add("tags", JSON.stringify(tags))
.add("type", mdType)
.add("state", state)
.add("number headings", "auto, first-level 1, max 6, _.1.1", addNumbering)
.add("created", tp.date.now("yyyy-MM-DD HH:mm"))
.add("updated", tp.date.now("yyyy-MM-DD HH:mm"))
.close();
tR += frontmatter;
}
// 步骤4: 处理 index 类型文档
if (mdType == "index") {
// 构建 frontmatter
const frontmatter = createFrontmatterBuilder()
.add("author", CONFIG.AUTHOR)
.add("type", mdType)
.add("indexType", indexType)
.add("created", tp.date.now("yyyy-MM-DD HH:mm"))
.add("updated", tp.date.now("yyyy-MM-DD HH:mm"))
.close();
tR += frontmatter;
}
// 步骤5: 构建内容主体
tR += buildContent(title);
// 步骤6: 插入 index 模板
if (mdType === "index") {
await tp.file.include("[[module_getIndex]]");
tR += tp.config.indexTemplate;
}
// 步骤6: 移动文件
await tp.file.move(`${CONFIG.BASE_FOLDER}/${title}`);
} catch (error) {
console.error("模板执行错误:", error);
new Notice("🛑 模板执行失败");
return;
}
-%>
<% tp.file.cursor() %>