菜单

8.1.20250722 按照标签自动更新标题编码

#templater #编码

使用[[kimi]]优化编码的机制,调试了接近2个小时,发现一个技巧,就是每次都要完整的写提示词,不要只写一部分,不然大模型会自我发挥:

编码的标准基于:[[编码体系]]

1.  如果笔记标签与对应前缀不一致,都要改成对应的前缀。要体验掉之前的(数字+.)的内容
2.  如果前缀与标签一致,1位数字开头的,修改为对应的前缀,前缀和标题之间只有一个空格
3.  如果前缀与标签一致,X.X.X结构(只有数字和.), 前缀保存不变,前缀和标题只有一个空格
为什么 1.1.1 企业-零售-美团 会变成1.1.20250722 企业-零售-美团

最终生成的代码

/* setTitleFromFilename.js */
async function setTitleFromFilename() {
  const file = app.workspace.getActiveFile();
  if (!file) return;

  /* ========== 1. 读取并标准化标签 ========== */
  const cache = app.metadataCache.getFileCache(file);
  const rawTags = cache?.frontmatter?.tags ?? cache?.frontmatter?.tag ?? [];
  const tags = Array.isArray(rawTags)
    ? rawTags.map(t => String(t).toLowerCase())
    : [String(rawTags).toLowerCase()];

  /* ========== 2. 标签 → 前缀 映射表(按优先级排序) ========== */
  const tagRules = [
    { match: ['人物', '角色'], prefix: '3.1' },
    { match: ['企业'],        prefix: '1.1' },
    { match: ['酷口数字花园'],        prefix: '0.0' },
    { match: ['思维模型'],    prefix: '2.1' },
    { match: ['技术'],        prefix: '2.2' },
    { match: ['城市', '地名'], prefix: '4.1' },
    { match: ['app'],         prefix: '5.1' },
    { match: ['功法'],        prefix: '5.2' },
    { match: ['书名'],        prefix: '6.1' },
    { match: ['法宝'],        prefix: '5.1' },
    { match: ['清单'],        prefix: '7.1' },
    { match: ['门派'],        prefix: '3.2' },
    { match: ['方法论'],      prefix: '2.2' },
    { match: ['参考资料','书名'], prefix: '9.1' },
    { match: ['自媒体'],      prefix: '9.2' },
    { match: ['插件'],        prefix: '5.2' },
    { match: ['行业'],        prefix: '1.0' }
  ];

  /* ========== 3. 根据标签确定前缀(命中即停) ========== */
  let prefixBase = '8.1';
  for (const rule of tagRules) {
    if (rule.match.some(k => tags.includes(k))) {
      prefixBase = rule.prefix;
      break;
    }
  }

  /* ========== 4. 拼完整前缀:基础 + 年月日 ========== */
  const today = new Date();
  const y = today.getFullYear();
  const m = String(today.getMonth() + 1).padStart(2, '0');
  const d = String(today.getDate()).padStart(2, '0');
  const datePart = `${y}${m}${d}`;
  const datePrefix = `${prefixBase}.${datePart}`;


/* ========== 5. 处理 title ========== */
const currentTitle = String(cache?.frontmatter?.title ?? '').trim();
const shouldPrefix = prefixBase;                   // 不含日期
const shouldFull   = `${prefixBase}.${datePart}`;  // 含今天日期

const parts = currentTitle.match(/^([\d.]+)\s*(.*)$/);
const oldPrefixRaw = parts ? parts[1] : '';
const body         = parts ? parts[2] : (currentTitle || file.basename);

// 只取前两段,忽略第三段及以后
const pureOld = oldPrefixRaw.replace(/\.\d{8}$/, '').split('.').slice(0, 2).join('.');
let newTitle = '';

if (pureOld !== shouldPrefix) {
  // 规则1:标签不一致 → 重拼
  newTitle = `${shouldFull} ${body.trim()}`;
} else if (/^\d$/.test(pureOld)) {
  // 规则2:单数字且一致 → 重拼
  newTitle = `${shouldFull} ${body.trim()}`;
} else {
  // 规则3:X.X.X 且一致 → 仅补空格
  newTitle = `${oldPrefixRaw} ${body.trim()}`;
}

if (newTitle !== currentTitle) {
  await app.fileManager.processFrontMatter(file, fm => fm.title = newTitle);
  new Notice(`✅ 已更新:${newTitle}`);
}

  /* ========== 6. 写回 frontmatter ========== */
  await app.fileManager.processFrontMatter(file, fm => {
    fm.title = newTitle;
  });
  new Notice(`✅ title 已更新:${newTitle}`);
}

module.exports = setTitleFromFilename;