Merge pull request #108 from kDCYorke/feature/frontmatterLinks

feat: Add frontmatter links to the links table
This commit is contained in:
Kacper Kula 2025-03-20 09:02:41 +00:00 committed by GitHub
commit fba988511c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 22 deletions

View file

@ -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'
```

View file

@ -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])
))
}
}
}