Skip to content

Instantly share code, notes, and snippets.

@jeetsukumaran
Created April 30, 2023 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeetsukumaran/784a3ea62a0cb83f02ecefe9de427999 to your computer and use it in GitHub Desktop.
Save jeetsukumaran/784a3ea62a0cb83f02ecefe9de427999 to your computer and use it in GitHub Desktop.
"Lightning rods of thought using Dataview" -- Filter paths in links as well
function runQuery(options) {
options = options || {}
// const excludePatterns = options.excludePatterns ?? []
// const includePatterns = options.includePatterns ?? []
const excludePaths = options.excludePaths ?? []
const includePaths = options.includePaths ?? []
const limit = options.limit ?? 50
const allPages = dv.pages()
let sourcePages = null
if (includePaths.length) {
for (let pathStr of includePaths) {
let filteredPages = allPages
.where(p => p.file.path.contains(pathStr))
.mutate(p => p.file.inlinks = p.file.inlinks.filter(link => link.path.contains(pathStr)))
.mutate(p => p.file.outlinks = p.file.outlinks.filter(link => link.path.contains(pathStr)))
if (filteredPages) {
if (sourcePages) {
sourcePages = sourcePages.concat(filteredPages)
} else {
sourcePages = filteredPages
}
}
}
} else {
sourcePages = allPages
}
for (let pathStr of excludePaths) {
sourcePages = sourcePages.where(p => !p.file.path.contains(pathStr))
.mutate(p => p.file.inlinks = p.file.inlinks.filter(link => link.path.contains(pathStr)))
.mutate(p => p.file.outlinks = p.file.outlinks.filter(link => link.path.contains(pathStr)))
}
if (options.sort == "inlinks") {
sourcePages = sourcePages.sort(p => p.file.inlinks.length, options.sortDir ?? "desc")
} else {
sourcePages = sourcePages.sort(p => p.file.outlinks.length, options.sortDir ?? "desc")
}
if (limit) {
sourcePages = sourcePages.limit(limit)
}
dv.table(
[
"Note",
"Inlinks",
"Outlinks",
],
sourcePages.map(p => [
p.file.link,
p.file.inlinks.length,
p.file.outlinks.length,
] )
)
}
runQuery(input)
await dv.view("__annex__/resources/obsidian/plugins/dataview/views/noteweights.dataview", {includePaths: ["knowledge"]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment