-
-
Save Moyf/7f7f740845e8db8b8e423b37b5afd573 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 统计当前笔记所在的文件夹下,有哪些笔记还没出现在当前笔记内 | |
| // 可以通过修改 folderDepth 参数来设置搜索的目录层级 | |
| // 设置参数 | |
| const folderDepth = 1; // 设置搜索的目录层级,1表示只搜索当前目录,2表示搜索当前目录及其子目录,以此类推 | |
| const ignoreRules = input?.ignore || []; // 忽略规则,可以是正则表达式或字符串 | |
| // 获取当前文件 | |
| const currentFile = dv.current(); | |
| // 获取当前文件的路径 | |
| const currentFilePath = currentFile.file.path; | |
| // 获取当前文件所在的文件夹路径 | |
| const folderPath = currentFilePath.substring(0, currentFilePath.lastIndexOf('/')); | |
| // 如果路径中没有'/',说明文件在根目录下 | |
| const currentFolder = folderPath === currentFilePath ? '' : folderPath; | |
| // 获取当前文件的内容 | |
| const content = await dv.io.load(currentFilePath); | |
| // 获取指定文件夹下的所有笔记 | |
| let notesInFolder = []; | |
| if (folderDepth === 1) { | |
| // 只搜索当前目录 | |
| notesInFolder = dv.pages() | |
| .where(p => p.file.path.startsWith(currentFolder) && | |
| p.file.path !== currentFilePath && | |
| p.file.path.substring(currentFolder.length + 1).indexOf('/') === -1); | |
| } else { | |
| // 搜索多级目录 | |
| notesInFolder = dv.pages() | |
| .where(p => { | |
| if (!p.file.path.startsWith(currentFolder) || p.file.path === currentFilePath) { | |
| return false; | |
| } | |
| // 计算路径中的目录层级 | |
| const relativePath = p.file.path.substring(currentFolder.length + 1); | |
| const slashCount = (relativePath.match(/\//g) || []).length; | |
| return slashCount < folderDepth; | |
| }); | |
| } | |
| // 应用忽略规则,过滤掉需要忽略的笔记 | |
| let ignoredCount = 0; | |
| const filteredNotes = notesInFolder.filter(note => { | |
| // 检查是否符合忽略规则 | |
| for (const rule of ignoreRules) { | |
| if (rule instanceof RegExp) { | |
| // 正则表达式规则 | |
| if (rule.test(note.file.name) || rule.test(note.file.path)) { | |
| ignoredCount++; | |
| return false; | |
| } | |
| } else if (typeof rule === 'string') { | |
| // 字符串规则 | |
| if (note.file.name.includes(rule) || note.file.path.includes(rule)) { | |
| ignoredCount++; | |
| return false; | |
| } | |
| } | |
| } | |
| return true; | |
| }); | |
| // 检查每个笔记是否在当前文件中被引用 | |
| const unreferencedNotes = filteredNotes.filter(note => { | |
| // 构建链接格式,检查是否在内容中出现 | |
| const linkFormats = [ | |
| `[[${note.file.name}]]`, // 标准链接格式 | |
| `![[${note.file.name}]]`, // 嵌入格式 | |
| `[[${note.file.path}]]`, // 完整路径链接格式 | |
| `![[${note.file.path}]]`, // 完整路径嵌入格式 | |
| new RegExp(`\\[\\[${escapeRegExp(note.file.name)}\\|.*?\\]\\]`), // 带别名的链接格式 [[文件名|别名]] | |
| new RegExp(`\\[\\[${escapeRegExp(note.file.path)}\\|.*?\\]\\]`) // 带别名的完整路径链接格式 [[路径|别名]] | |
| ]; | |
| // 如果笔记有别名,也检查别名链接 | |
| if (note.aliases) { | |
| note.aliases.forEach(alias => { | |
| linkFormats.push(`[[${alias}]]`); | |
| linkFormats.push(`![[${alias}]]`); | |
| }); | |
| } | |
| // 检查所有可能的链接格式 | |
| return !linkFormats.some(format => { | |
| if (format instanceof RegExp) { | |
| return format.test(content); | |
| } | |
| return content.includes(format); | |
| }); | |
| }); | |
| // 用于转义正则表达式中的特殊字符 | |
| function escapeRegExp(string) { | |
| return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& 表示整个匹配的字符串 | |
| } | |
| // 显示结果 | |
| if (unreferencedNotes.length > 0) { | |
| // dv.header(3, "未引用的笔记列表"); | |
| dv.paragraph(`共计 ${unreferencedNotes.length} 个未引用的笔记${ignoredCount > 0 ? ` *(${ignoredCount} 个笔记被忽略规则过滤)*` : ''}`) | |
| dv.paragraph("#### 链接格式") | |
| dv.paragraph( | |
| unreferencedNotes.map(note => | |
| dv.fileLink(note.file.path, false, note.file.name) | |
| ) | |
| ); | |
| dv.paragraph("\n#### 纯文本") | |
| // 拼接成一个纯文本,包在 > 中 | |
| const pureText = "```txt\n" + unreferencedNotes.map(note => `[[${note.file.name}]]`).join('\n') + "\n```"; | |
| dv.paragraph(pureText); | |
| } else { | |
| dv.paragraph(`✅ 所有笔记都已被引用${ignoredCount > 0 ? `,${ignoredCount} 个笔记被忽略规则过滤` : ''}`); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment