For those looking for something similar, this works for me:
(async () => {
const synonyms = new Set(["mängd", "mängder", "mängden", "universala mängden", "universalmängden", "universumsmängden"]);
const pages = dv.pages(
'"Årskurs 2/Läsperiod 4/Sannolikhetsteori och statistisk signalbehandling/Föreläsningar"'
);
const hits = [];
for (const p of pages) {
const text = await dv.io.load(p.file.path);
const lines = text.split(/\r?\n/);
let section = "";
for (const line of lines) {
const headMatch = line.match(/^\s*#{1,6}\s+(.*)$/);
if (headMatch) {
section = headMatch[1].trim();
continue;
}
const linkRe = /\[\[([^\]|#]+)(?:#[^\]|]+)?(?:\|[^\]]+)?\]\]/g;
let m;
while ((m = linkRe.exec(line)) !== null) {
const rawTarget = m[1].trim();
const target = rawTarget.toLowerCase();
if (synonyms.has(target)) {
hits.push({
file: p.file,
section,
term: rawTarget
});
}
}
}
}
const unique = Array.from(
hits.reduce((map, h) => {
const key = `${h.file.path}#${h.section}#${h.term}`;
if (!map.has(key)) map.set(key, h);
return map;
}, new Map()).values()
);
dv.table(
["Föreläsning", "Rubrik", "Term"],
unique.map(h => [
h.file.link,
`[[${h.file.path}#${h.section}|${h.section}]]`,
h.term.toLowerCase()
])
);
})();
which gives