Hello everyone,
I tried writing code, with dataview, to count the number of links without associated files, and then display them with their source file. But it doesn’t seem to work.
Do you have a solution?
// Create a map: key = broken link, value = array of source files
let brokenLinksMap = new Map();
// Browse all pages
for (let page of dv.pages()) {
if (!page.file || !page.file.links) continue;
for (let link of page.file.links) {
const target = link.path;
// Checks if the target page exists
if (!dv.page(target)) {
// If this broken link is not yet in the map, initialize it
if (!brokenLinksMap.has(target)) {
brokenLinksMap.set(target, []);
}
// Adds the source file to the list
brokenLinksMap.get(target).push(page.file.name);
}
}
}
// Convert to table and sort by missing link name
const brokenArray = Array.from(brokenLinksMap.entries()).sort((a, b) => a[0].localeCompare(b[0]));
// Display
if (brokenArray.length === 0) {
dv.paragraph("✅ No link to a non-existent note found.");
} else {
dv.paragraph(`🧩 **${brokenArray.length}** missing files, referenced in :`);
// Display as a grouped list
for (let [missingNote, sources] of brokenArray) {
dv.header(4, `🔗 [[${missingNote}]]`);
dv.list(sources.map(src => `[[${src}]]`));
}
}