菜单

8.1.20250720 “000”开头的笔记编码规则调整

#编码 #templater

针对笔记的编码,新增了一个条件,如果是000开通的笔记,000要被替换为编码

使用场景是,Dinox新建的笔记,如果希望发布,就可以笔记标题增加这个前缀

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

  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 datePrefix = `8.1.${y}${m}${d}`;

  const cache = app.metadataCache.getFileCache(file);
  const currentTitle = cache?.frontmatter?.title;

  let newTitle;
  if (!currentTitle) {
    newTitle = `${datePrefix} ${file.basename}`;
  } else if (String(currentTitle).trim().startsWith("000")) {
    // 去掉开头的 000(含空格)
    const cleanTitle = String(currentTitle).trim().replace(/^000\s*/, '');
    newTitle = `${datePrefix} ${cleanTitle}`;
  } else if (!/^\d/.test(String(currentTitle).trim())) {
    newTitle = `${datePrefix} ${String(currentTitle).trim()}`;
  } else {
    new Notice("title 已符合规则,未变动");
    return;
  }

  await app.fileManager.processFrontMatter(file, fm => {
    fm.title = newTitle;
  });
  new Notice(`✅ title 已设为:${newTitle}`);
}

module.exports = setTitleFromFilename;

ob地址:笔记