Skip to content

Commit

Permalink
Merge pull request #6 from valentine195/master
Browse files Browse the repository at this point in the history
Adds multiline YAML tag support, fixes mobile, and inserts comma
  • Loading branch information
jmilldotdev authored Feb 23, 2022
2 parents 33fd995 + 79a377d commit 768bb76
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@ class TagSuggest extends EditorSuggest<string> {
const tags: any = this.plugin.app.metadataCache.getTags();
return [...Object.keys(tags)].map((p) => p.split("#").pop());
}

inRange(range: string) {
if (!range || !range.length) return false;
if (range.match(/^---\n/gm)?.length != 1) return false;
if (!/^tags?:/gm.test(range)) return false;
const split = range.split(/(^\w+:?\s*\n?)/gm);
for (let i = split.length - 1; i >= 0; i--) {
if (/(^\w+:?\s*\n?)/gm.test(split[i]))
return split[i].startsWith("tags:");
}
return false;
}
inline = false;
onTrigger(
cursor: EditorPosition,
editor: Editor,
_: TFile
): EditorSuggestTriggerInfo | null {
const lineContents = editor.getLine(cursor.line).toLowerCase();
const onFrontmatterTagLine =
lineContents.startsWith("tags:") || lineContents.startsWith("tag:");
lineContents.startsWith("tags:") ||
lineContents.startsWith("tag:") ||
this.inRange(editor.getRange({ line: 0, ch: 0 }, cursor));
if (onFrontmatterTagLine) {
this.inline =
lineContents.startsWith("tags:") ||
lineContents.startsWith("tag:");
const sub = editor.getLine(cursor.line).substring(0, cursor.ch);
const match = sub.match(/(?<= )\S+$/)?.first();
const match = sub.match(/(\S+)$/)?.first();
if (match) {
this.tags = this.getTags();
const matchData = {
Expand Down Expand Up @@ -70,6 +86,11 @@ class TagSuggest extends EditorSuggest<string> {

selectSuggestion(suggestion: string): void {
if (this.context) {
if (this.inline) {
suggestion = `${suggestion},`;
} else {
suggestion = `${suggestion}\n -`;
}
(this.context.editor as Editor).replaceRange(
`${suggestion} `,
this.context.start,
Expand Down

0 comments on commit 768bb76

Please sign in to comment.