关于Templater创建笔记时已存在笔记解决问题

使用模板创建笔记时,如何解决已存在的笔记,在该笔记后面直接插入内容


描述图片

用 quickadd 的 capture 可以实现吧?楼主搜搜相关教程,quickadd调用templater

好的感谢建议,我看看

先查询出所有笔记名称,判断笔记名称是否重复,根据这个布尔值走插入或这追加就行

这是我之前写过的获取唯一文件路径的代码,你可以拆取部分方法加到你的代码中

<%*
/* ========== 配置区 - 可扩展的常量配置 ========== */
const CONFIG = {
  BASE_FOLDER: "",
  EXCLUDE_FOLDERS: new Set(["papers", "attachments"]),
  FILE_EXT: ".md",
};

/* ========== 核心功能函数 ========== */

/* 获取分层文件夹 */
async function getHierarchicalFolders() {
  let currentPath = CONFIG.BASE_FOLDER;
  let finalPath = "";

  while (true) {
    const { folders } = await app.vault.adapter.list(currentPath);
    const validFolders = folders
      .map((fullPath) => fullPath.split("/").pop())
      .filter((folder) => !CONFIG.EXCLUDE_FOLDERS.has(folder.toLowerCase()));

    const options = [];
    if (currentPath !== CONFIG.BASE_FOLDER) options.push("🔙");
    options.push(...validFolders, "✔️");

    const chosen = await tp.system.suggester(
      options,
      options,
      false,
      `选择目录 📁 当前路径: ${currentPath.replace(
        CONFIG.BASE_FOLDER,
        "根目录"
      )}`
    );

    if (!chosen) return null;

    if (chosen === "✔️") {
      finalPath = currentPath.replace(CONFIG.BASE_FOLDER + "/", "");
      break;
    } else if (chosen === "🔙") {
      currentPath = currentPath.split("/").slice(0, -1).join("/");
    } else {
      currentPath = `${currentPath}/${chosen}`;
    }
  }
  return finalPath;
}

/* 判读文件是否存在 */
async function isFileExists(folderPath, fileName) {
  const fullPath =
    `${CONFIG.BASE_FOLDER}/${folderPath}/${fileName}${CONFIG.FILE_EXT}`.replace(
      /\/+/g,
      "/"
    );
  return await app.vault.adapter.exists(fullPath);
}

/* 获取唯一文件名 */
async function getUniqueFileName(chosenFolder) {
  while (true) {
    const titleName = await tp.system.prompt("请输入笔记标题 🗒️", "未命名笔记");
    if (!titleName) return false;
    if (!(await isFileExists(chosenFolder, titleName))) return titleName;
    new Notice(`⚠️ "${titleName}" 已存在,请换用其他名称`);
  }
}

/* 新增文件夹流程 */
async function createNewFolder() {
  const basePath = await getHierarchicalFolders();
  if (!basePath) return null;

  while (true) {
    const folderName = await tp.system.prompt(
      "请输入新增文件夹名称 📁",
      "新文件夹"
    );
    if (!folderName) return null;

    const fullPath = `${CONFIG.BASE_FOLDER}/${basePath}/${folderName}`.replace(
      /\/+/g,
      "/"
    );

    if (!(await app.vault.adapter.exists(fullPath))) {
      await app.vault.createFolder(fullPath);
      return `${basePath}/${folderName}`;
    }
    new Notice(`⚠️ 文件夹 "${folderName}" 已存在!`);
  }
}

/* 主流程 */
// 判断使用现有目录还是新建目录
let isNewFolder;
try {
  isNewFolder = await tp.system.suggester(
    ["使用现有目录", "创建新文件夹"],
    [false, true],
    true,
    "选择文件夹操作 🗃️"
  );
} catch (error) {
  new Notice("🛑 操作已取消");
  return;
}

// 获取新建笔记的路径
let chosenFolder;
if (isNewFolder) {
  // 新建目录,调用新建目录方法
  chosenFolder = await createNewFolder();
} else {
  // 选择原有目录,调用获取分层文件夹方法
  chosenFolder = await getHierarchicalFolders();
}
if (!chosenFolder) {
  new Notice("🛑 操作已取消");
  return;
}

// 获取唯一笔记标题
const titleName = await getUniqueFileName(chosenFolder);
if (!titleName) {
  new Notice("🛑 操作已取消");
  return;
}

const mdPath = `${
  CONFIG.BASE_FOLDER ? CONFIG.BASE_FOLDER + "/" : ""
}${chosenFolder}/${titleName}`;

// 最终输出格式 BASE_FOLDER/md/learn/未命名笔记
tp.config.mdPath = mdPath;

// 调试信息
// tR += mdPath;
// console.log("唯一文件夹名称:", chosenFolder);
// console.log("唯一笔记标题:", titleName);
// console.log("md文件路径: ", mdPath);

-%>

666 ,感谢了!拿去研究研究…