如题,我想在笔记最后放一个引用的部分。包含本文档中的内部链接和外部网址链接。我试了用ai帮我写dataview的代码块,但都读取不了外部网址链接
以下是用ds 写的,用dataviewjs写的统计本文件外部链接的代码
// 获取当前文件
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 + "个)");
dv.list(uniqueLinks.map(link => `[${link.text}](${link.url})`));
} else {
dv.paragraph("本文档没有包含外部链接。");
}
2 个赞
可以用!太感谢了,我用Gemini生成的不知道为啥不行。