Skip to content

Commit

Permalink
Update StarredCache to work with the bookmarks core plugin (#1959)
Browse files Browse the repository at this point in the history
* Update StarredCache to work with the bookmarks core plugin

Resolves #1904

* Update documentation to reflect changes to `file.starred`
  • Loading branch information
mjocc authored Sep 15, 2023
1 parent 9a34020 commit 6e1c467
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/docs/annotation/metadata-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Dataview automatically adds a large amount of metadata to each page. These impli
| `file.lists` | List | A list of all list elements in the file (including tasks); these elements are effectively tasks and can be rendered in task views. |
| `file.frontmatter` | List | Contains the raw values of all frontmatter in form of `key | value` text values; mainly useful for checking raw frontmatter values or for dynamically listing frontmatter keys. |
| `file.day` | Date | Only available if the file has a date inside its file name (of form `yyyy-mm-dd` or `yyyymmdd`), or has a `Date` field/inline field. |
| `file.starred` | Boolean | if this file has been starred via the Obsidian Core Plugin "Starred Files". |
| `file.starred` | Boolean | If this file has been bookmarked via the Obsidian Core Plugin "Bookmarks". |

## Example page

Expand Down Expand Up @@ -58,4 +58,4 @@ You can query part of the above information with following query, for example:
TABLE file.ctime, length, rating, reviewed
FROM #movies
```
~~~
~~~
18 changes: 16 additions & 2 deletions src/data-index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class CsvCache extends Component {
}
}

export type StarredEntry = { type: "file"; path: string; title: string } | { type: "folder" } | { type: "query" };
export type StarredEntry = { type: "group"; items: StarredEntry[]; title: string } | { type: "file"; path: string; title: string } | { type: "folder" } | { type: "query" };

/** Optional connector to the Obsidian 'Starred' plugin which allows for efficiently querying if a file is starred or not. */
export class StarredCache extends Component {
Expand Down Expand Up @@ -420,9 +420,23 @@ export class StarredCache extends Component {

/** Fetch all starred files from the stars plugin, if present. */
private static fetch(app: App): Set<string> {
let items = (app as any)?.internalPlugins?.plugins?.starred?.instance?.items as StarredEntry[];
let items = (app as any)?.internalPlugins?.plugins?.bookmarks?.instance?.items as StarredEntry[];
if (items == undefined) return new Set();

// Retrieve all grouped (nested) items, returning a flat array
const flattenItems = (items: StarredEntry[]): StarredEntry[] => {
let children: StarredEntry[] = [];

return items.map(i => {
if (i.type == "group" && i.items && i.items.length) {
children = [...children, ...i.items];
}
return i;
}).concat(children.length ? flattenItems(children) : children);
};

items = flattenItems(items)

return new Set(
items.filter((l): l is { type: "file"; path: string; title: string } => l.type === "file").map(l => l.path)
);
Expand Down

0 comments on commit 6e1c467

Please sign in to comment.