diff --git a/docs/data-sources/vault-data.md b/docs/data-sources/vault-data.md index 2406956..58212e0 100644 --- a/docs/data-sources/vault-data.md +++ b/docs/data-sources/vault-data.md @@ -56,3 +56,28 @@ Introduced in 0.20.0 | `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 | + + +#### 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' +``` + diff --git a/src/vaultSync/tables/linksTable.ts b/src/vaultSync/tables/linksTable.ts index a08ca47..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,26 +26,39 @@ export class LinksFileSyncTable extends AFileSyncTable { if (!cache) { return [] } - const tags = cache.links - if (!tags) { - return [] - } - 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 - } - return { - path: file.path, - target: target, - position: t.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 + } + }) } @@ -59,4 +72,4 @@ export class LinksFileSyncTable extends AFileSyncTable { this.db.createIndex(`links_${column}_idx`, this.tableName, [column]) )) } -} \ No newline at end of file +}