From 46643a71111511550d7a780c35b357bd0930f9c1 Mon Sep 17 00:00:00 2001 From: Kai Yorke Date: Tue, 18 Mar 2025 10:42:10 +1300 Subject: [PATCH 1/3] feat: Add frontmatter links to the links table --- src/vaultSync/tables/linksTable.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/vaultSync/tables/linksTable.ts b/src/vaultSync/tables/linksTable.ts index a08ca47..f09e2a8 100644 --- a/src/vaultSync/tables/linksTable.ts +++ b/src/vaultSync/tables/linksTable.ts @@ -26,10 +26,7 @@ export class LinksFileSyncTable extends AFileSyncTable { if (!cache) { return [] } - const tags = cache.links - if (!tags) { - return [] - } + const tags = (cache.links || []).concat(cache.frontmatterLinks || []); return tags.map((t) => { const targetFile = this.app.metadataCache.getFirstLinkpathDest(t.link, file.path) let targetExists = false @@ -38,10 +35,16 @@ export class LinksFileSyncTable extends AFileSyncTable { target = targetFile.path targetExists = true } + let position = t.position; + if (t.key) { + position = { + frontmatterKey: t.key + } + } return { path: file.path, target: target, - position: t.position, + position: position, display_text: t.displayText, target_exists: targetExists } @@ -59,4 +62,4 @@ export class LinksFileSyncTable extends AFileSyncTable { this.db.createIndex(`links_${column}_idx`, this.tableName, [column]) )) } -} \ No newline at end of file +} From eb31360dad6b1a6d2b79eb3355c698defbe0c228 Mon Sep 17 00:00:00 2001 From: Kai Yorke Date: Thu, 20 Mar 2025 13:43:40 +1300 Subject: [PATCH 2/3] Make typecheck happy --- src/vaultSync/tables/linksTable.ts | 58 +++++++++++++++++------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/vaultSync/tables/linksTable.ts b/src/vaultSync/tables/linksTable.ts index f09e2a8..5973db8 100644 --- a/src/vaultSync/tables/linksTable.ts +++ b/src/vaultSync/tables/linksTable.ts @@ -1,4 +1,4 @@ -import { TFile } from "obsidian"; +import { FrontmatterLinkCache, LinkCache, TFile } from "obsidian"; import { AFileSyncTable } from "./abstractFileSyncTable"; export class LinksFileSyncTable extends AFileSyncTable { @@ -26,29 +26,39 @@ export class LinksFileSyncTable extends AFileSyncTable { if (!cache) { return [] } - const tags = (cache.links || []).concat(cache.frontmatterLinks || []); - return tags.map((t) => { - const targetFile = this.app.metadataCache.getFirstLinkpathDest(t.link, file.path) - let targetExists = false - let target = t.link - if (targetFile) { - target = targetFile.path - targetExists = true - } - let position = t.position; - if (t.key) { - position = { - frontmatterKey: t.key - } - } - return { - path: file.path, - target: target, - position: position, - display_text: t.displayText, - target_exists: targetExists - } - }) + + const links: any[] = (cache.links || []).map((l: LinkCache) => { + return { + targetLink: l.link, + position: l.position, + display_text: l.displayText + } + }); + + const frontmatterLinks: any[] = (cache.frontmatterLinks || []).map((l: FrontmatterLinkCache) => { + return { + targetLink: l.link, + position: { frontmatterKey: l.key }, + display_text: l.displayText + } + }); + + return links.concat(frontmatterLinks).map(({ targetLink, ...reference }) => { + const targetFile = this.app.metadataCache.getFirstLinkpathDest(targetLink, file.path) + let targetExists = false + let target = targetLink + if (targetFile) { + target = targetFile.path + targetExists = true + } + + return { + path: file.path, + target: target, + ...reference, + target_exists: targetExists + } + }) } From adaec2ca5820ea6d5f4d26ad85f50c9308de2150 Mon Sep 17 00:00:00 2001 From: Kai Yorke Date: Thu, 20 Mar 2025 14:01:50 +1300 Subject: [PATCH 3/3] Add examples to documentation --- docs/data-sources/vault-data.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/data-sources/vault-data.md b/docs/data-sources/vault-data.md index 4e618df..56bb70c 100644 --- a/docs/data-sources/vault-data.md +++ b/docs/data-sources/vault-data.md @@ -51,4 +51,27 @@ Introduced in 0.20.0 | `target` | Full path of the target of the link | | `position` | JSON object containing information about location of the link | | `display_text` | Text displayed on the page for that link | -| `target_exists` | information if the target file exists. 1 if it exists, 0 otherwise | \ No newline at end of file +| `target_exists` | information if the target file exists. 1 if it exists, 0 otherwise | + +#### Frontmatter links + +Links that appear in a file's frontmatter (Obsidian properties) contain a `frontmatterKey` property in the `position` +JSON object. This can be used to identify links that are in the note body or within a specific frontmatter property. + +For instance, to query all links to the current file that appear in the body of a note: + +```sql +SELECT * FROM links +WHERE target = @path +AND json_extract(position, '$.frontmatterKey') IS NULL +``` + +`frontmatterKey` can be used to select links within a specific property. A Map of Content, for instance, may wish to +show a list of files that list the MOC as a type: + +```sql +LIST +SELECT a(path) FROM links +WHERE target = @path +AND json_extract(position, '$.frontmatterKey') = 'type' +```