【QuickAdd脚本】通过wiki链接删除并对应文件,适用于笔记重组

该脚本的功能是通过自动选中鼠标所在行的 [[]] 文本,然后将对应文档的内容(不包括 Yaml)复制到剪贴板,并删除该文档。

2024-03-13_QuickAdd脚本-复制wiki对应文档内容并删除文件_IMG-1

适用于快速将子笔记内容整理到剪切板,以及删除子文档的操作。

QuickAdd Macro 设置

配置下面的脚本,即将下面代码复制到 js 文件 (如:复制并删除文件.js) 中,并存放到 QuickAdd 对应的脚本文件夹下,即可在 User Scripts 里面找到并添加。

脚本

const fs = require('fs');
const path = require('path');
const process = require('process');

module.exports = {
    entry: async (QuickAdd, settings, params) => {
        // 获取选中的文本
        const editor = app.workspace.activeEditor.editor;
        // 选择所在的一行
        const line = editor.getLine(editor.getCursor().line);
        // 获取选中的文本否则自动获取当前行的文本
        const selection = editor.getSelection() ? editor.getSelection() : line;

        let selectionEmbed = matchSelectionEmbed(selection);
        console.log(selectionEmbed);
        const files = app.vault.getFiles();

        // Wiki: 获取库所有文件列表
        wikiPath = getFilePath(files, selectionEmbed); // 匹配Wiki链接
        console.log(wikiPath);
        if (!wikiPath) {
            new Notice("❌未找到对应文件");
            return;
        };

        // 复制并删除文件
        const types = String(settings["Types"]).split(",");
        if (wikiPath.endsWith(".md") || types.includes(path.extname(wikiPath).slice(1))) {
            let markdownText = getMarkdownText(wikiPath);
            copyToClipboard(markdownText);
            await app.vault.trash(app.vault.getAbstractFileByPath(wikiPath));
            new Notice("💡已复制内容到剪切板,并删除文件");
        } else {
            new Notice("❌已删除文件");
            await app.vault.trash(app.vault.getAbstractFileByPath(wikiPath));
            editor.replaceSelection("");
        }
        return;
    },
    settings: {
        name: "复制并删除",
        author: "熊猫别熬夜",
        options: {
            "Types": {
                type: "text",
                defaultValue: "md,txt,js,py",
                description: "可复制文件的类型,多个以,分离",
            },
        }
    }
};

function matchSelectionEmbed(text) {
  const regex = /\[\[?([^\]]*?)(\|.*)?\]\]?\(?([^)\n]*)\)?/;
  const matches = text.match(regex);
  if (!matches) return;
  if (matches[3]) return decodeURIComponent(matches[3]);
  if (matches[1]) return decodeURIComponent(matches[1]);
}

function getFilePath(files, baseName) {
    let files2 = files.filter(f => path.basename(f.path).replace(".md", "") === path.basename(baseName).replace(".md", ""));
    let filePath = files2.map((f) => f.path);
    return filePath[0];
}

function copyToClipboard(extrTexts) {
    const txtArea = document.createElement('textarea');
    txtArea.value = extrTexts;
    document.body.appendChild(txtArea);
    txtArea.select();
    if (document.execCommand('copy')) {
        console.log('copy to clipboard.');
    } else {
        console.log('fail to copy.');
    }
    document.body.removeChild(txtArea);
}

// 获取文件路径下的 md 中的文本(排除 Yaml)
function getMarkdownText(filePath) {
    // 获取文件的完整路径
    const fileFullPath = app.vault.adapter.getFullPath(filePath);
    // 读取文件内容
    const fileContent = fs.readFileSync(fileFullPath, 'utf8');
    // 排除首行YAML区域
    const markdownText = fileContent.replace(/---[\s\S]*?---\n*/, '').replace(/\n*/, '');
    return markdownText;
}

ChangeLog

  • 添加可复制类型,其他类型则直接删除 :white_check_mark: 2024-03-19
    • image
  • 适配wiki或md的链接格式 :white_check_mark: 2024-03-28
1 个赞

推荐设置快捷键:Ctrl + Shift + X
相当于剪切是文件内容或者删除附件的功能

1 个赞

熊猫别熬夜大佬
多謝分享
我覺得您的創意跟實作,都讓Obsidian變得更好用
:+1::+1::+1::+1::+1:
謝謝

1 个赞

非常感谢您的认可 :kissing_heart:

1 个赞

#7的需求,适配了库的绝对路径以及其他格式的wiki链接。