1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| // 获取当前文件 const currentFile = dv.current().file
// 读取文件内容 const content = await dv.io.load(currentFile.path)
// 匹配外部链接的正则表达式(匹配[text](url)和<url>格式) const linkRegex = /\[([^\]]+)\]\((https?:\/\/[^\)]+)\)|(https?:\/\/[^\s>]+)/g
// 存储找到的链接 let links = [] let match
// 查找所有匹配的链接 while ((match = linkRegex.exec(content)) !== null) { if (match[1] && match[2]) { // [text](url) 格式 links.push({text: match[1], url: match[2]}) } else if (match[3]) { // 纯URL格式 links.push({text: match[3], url: match[3]}) } }
// 去重 const uniqueLinks = [...new Map(links.map(item => [item.url, item])).values()]
// 显示结果 if (uniqueLinks.length > 0) { dv.header(3, "🔗外部链接统计 (" + uniqueLinks.length + "个)"); // 创建带序号的列表(顶格显示) const numberedList = uniqueLinks.map((link, index) => { return `${index + 1}. [${link.text}](${link.url})`; }).join("\n"); // 用换行符连接 dv.paragraph(numberedList); // 使用paragraph保持顶格格式 } else { dv.paragraph("本文档没有包含外部链接。"); }
|