From d38ddb8410b19c53d5842c9dba0b6b9102bbe0ab Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Mon, 9 Dec 2024 12:38:48 +0100 Subject: [PATCH] poc for suggesting dataview fields --- main.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 674ef19..955e987 100644 --- a/main.ts +++ b/main.ts @@ -63,6 +63,7 @@ class SampleSettingTab extends PluginSettingTab { } class DataviewSuggester extends EditorSuggest { + app: App constructor(plugin: Plugin) { super(plugin.app) @@ -74,7 +75,7 @@ class DataviewSuggester extends EditorSuggest { file: TFile, ): EditorSuggestTriggerInfo | null { const line = editor.getLine(cursor.line); - const regex = /\((.*?)\)/g + const regex = /\((.*?)\)/g // todo min characters for match? -> research on best practices let match while((match = regex.exec(line)) !== null) { @@ -95,7 +96,32 @@ class DataviewSuggester extends EditorSuggest { } getSuggestions(context: EditorSuggestContext): string[] | Promise { - return ["hello", "world"] + const suggestions: string[] = [] + // TODO use official dv api? + // @ts-ignore + for (const page of this.app.plugins.plugins.dataview.index.pages) { + const fields = page[1].fields + for (let [key, val] of fields) { + let arrayVal + if (!Array.isArray(val)) { + arrayVal = [val] + } else { + arrayVal = val + } + + for (const value of arrayVal) { + // TODO whitespace in match + // Fuzzy matching? + const suggestion = key + ":: " + value + if (suggestion.includes(context.query) && !suggestions.includes(suggestion)) { + suggestions.push(key+ ":: " + value) + } + } + } + + } + console.log(suggestions) + return suggestions } renderSuggestion(value: string, el: HTMLElement): void {