Skip to content

Commit

Permalink
use metadatacache to get tags, match tag: in frontmatter also
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilldotdev committed Nov 9, 2021
1 parent 3b33286 commit 55c8330
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Obsidian Frontmatter Tag Suggest

Autocompletes tags in the YAML frontmatter. It will show an autocompletion menu if you are on a line starting with `tags:`
Autocompletes tags in the YAML frontmatter. It will show an autocompletion menu if you are on a line starting with `tags:` or `tag:`.

That's it. That's the plugin
19 changes: 6 additions & 13 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
EditorSuggest,
EditorSuggestContext,
EditorSuggestTriggerInfo,
getAllTags,
Plugin,
TFile,
} from "obsidian";
Expand All @@ -25,25 +24,19 @@ class TagSuggest extends EditorSuggest<string> {
}

getTags(): string[] {
const app = this.plugin.app;
let tags: string[] = [];
const files = app.vault.getMarkdownFiles();
files.forEach((p) => {
const cache = app.metadataCache.getFileCache(p);
tags.push(...getAllTags(cache));
});
return [...new Set(tags)].sort().map((p) => p.split("#").pop());
//@ts-expect-error, private method
const tags: any = this.plugin.app.metadataCache.getTags();
return [...Object.keys(tags)].map((p) => p.split("#").pop());
}

onTrigger(
cursor: EditorPosition,
editor: Editor,
_: TFile
): EditorSuggestTriggerInfo | null {
const onFrontmatterTagLine = editor
.getLine(cursor.line)
.toLowerCase()
.startsWith("tags:");
const lineContents = editor.getLine(cursor.line).toLowerCase();
const onFrontmatterTagLine =
lineContents.startsWith("tags:") || lineContents.startsWith("tag:");
if (onFrontmatterTagLine) {
const sub = editor.getLine(cursor.line).substring(0, cursor.ch);
const match = sub.match(/(?<= )\S+$/)?.first();
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-frontmatter-tag-suggest",
"name": "Frontmatter Tag Suggest",
"version": "0.1.0",
"version": "0.1.1",
"minAppVersion": "0.12.0",
"description": "Offer autocomplete suggestions for tags in frontmatter",
"author": "Jonathan Miller",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-frontmatter-tag-suggest",
"version": "0.1.0",
"version": "0.1.1",
"description": "Offer autocomplete suggestions for tags in frontmatter",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 55c8330

Please sign in to comment.