mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 22:25:29 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
314f99f462 | ||
|
|
064320bc5b | ||
|
|
5f7fdb244a | ||
|
|
a820b4453d | ||
|
|
4d9673eff7 | ||
|
|
ddedb4e8c1 | ||
|
|
41c9e60615 | ||
|
|
70d0b94a62 | ||
|
|
b10656c2c3 | ||
|
|
cc078fae6a | ||
|
|
b6cdf6cb47 | ||
|
|
5bfb3116bc | ||
|
|
dac07242cd | ||
|
|
c3f44af074 | ||
|
|
57be38b005 | ||
|
|
e744b48536 | ||
|
|
b79d363c9f | ||
|
|
2d9194265a | ||
|
|
27f2e64884 | ||
|
|
779bd0bd68 | ||
|
|
910d343948 | ||
|
|
5802a6cd5d | ||
|
|
d85c07126f | ||
|
|
020054857b | ||
|
|
f58f736df4 |
22
README.md
22
README.md
@@ -1,11 +1,25 @@
|
|||||||
# Obsidian Dataview Autocompletion
|
# Dataview Autocompletion
|
||||||
|
|
||||||
This is a plugin for Obsidian (https://obsidian.md).
|
This is a plugin for Obsidian (https://obsidian.md).
|
||||||
|
|
||||||
Obsidian Dataview Autocomplete enhances the functionality of the popular [Dataview plugin](https://github.com/blacksmithgu/obsidian-dataview)
|
Dataview Autocomplete enhances the functionality of the popular [Dataview plugin](https://github.com/blacksmithgu/obsidian-dataview)
|
||||||
by providing autocomplete suggestions for metadata fields in the editor.
|
by providing autocomplete suggestions for metadata fields in the editor. This makes it easy to reuse existing fields and reduces typing errors.
|
||||||
|
|
||||||

|
<img src="./assets/demo.gif" alt="Demo" width="400" >
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Provides autocomplete suggestions for inline metadata fields used by Dataview.
|
||||||
|
- Customizable ignored fields and files for suggestions.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
While plugin is [waiting](https://github.com/obsidianmd/obsidian-releases/pull/4913) to be reviewed and added to the community plugin list, you can install it manually following the steps below:
|
||||||
|
|
||||||
|
1. Download the latest release files (manifest.json, styles.css, main.js) from the Releases page.
|
||||||
|
2. Create a folder named "dataview-autocompletion" in the Obsidian plugins folder (.obsidian/plugins).
|
||||||
|
3. Copy the files from step 1 into the new folder.
|
||||||
|
4. Enable the plugin in the Obsidian settings under the "Community plugins" section. You might have to restart Obsidian to rsee the plugin.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
BIN
assets/demo.gif
BIN
assets/demo.gif
Binary file not shown.
|
Before Width: | Height: | Size: 853 KiB After Width: | Height: | Size: 1.5 MiB |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"id": "dataview-autocompletion",
|
"id": "dataview-autocompletion",
|
||||||
"name": "Dataview Autocompletion",
|
"name": "Dataview Autocompletion",
|
||||||
"version": "0.8.2",
|
"version": "0.9.3",
|
||||||
"minAppVersion": "0.15.0",
|
"minAppVersion": "0.15.0",
|
||||||
"description": "Adds autocompletion to Dataview metadata fields",
|
"description": "Adds autocompletion to Dataview metadata fields",
|
||||||
"author": "Daniel Bauer",
|
"author": "Daniel Bauer",
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "obsidian-dataview-autocompletion",
|
"name": "obsidian-dataview-autocompletion",
|
||||||
"version": "0.8.2",
|
"version": "0.9.3",
|
||||||
"description": "This is a plugin for Obsidian that provides autocompletion for Dataview metadata fields",
|
"description": "This is a plugin for Obsidian that provides autocompletion for Dataview metadata fields",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "npx rollup --config rollup.config.js -w",
|
"dev": "npx rollup --config rollup.config.js -w",
|
||||||
"build": "npx rollup --config rollup.config.js --environment BUILD:production",
|
"build": "npx rollup --config rollup.config.js --environment BUILD:production",
|
||||||
"check-format": "npx prettier --check src",
|
"check-format": "npx prettier --check src",
|
||||||
|
"fix-format": "npx prettier --write src",
|
||||||
"test": "npx jest",
|
"test": "npx jest",
|
||||||
|
"bdd": "npx jest -i --watch --no-cache",
|
||||||
"version": "node ersion-bump.mjs && git add manifest.json versions.json"
|
"version": "node ersion-bump.mjs && git add manifest.json versions.json"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ const createBaseConfig = (outdir) => ({
|
|||||||
src: "manifest.json",
|
src: "manifest.json",
|
||||||
dest: outdir,
|
dest: outdir,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
src: "styles.css",
|
||||||
|
dest: outdir,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -5,23 +5,24 @@ import {
|
|||||||
EditorSuggest,
|
EditorSuggest,
|
||||||
EditorSuggestContext,
|
EditorSuggestContext,
|
||||||
EditorSuggestTriggerInfo,
|
EditorSuggestTriggerInfo,
|
||||||
|
htmlToMarkdown,
|
||||||
|
MarkdownRenderer,
|
||||||
|
MarkdownView,
|
||||||
TFile,
|
TFile,
|
||||||
} from "obsidian";
|
} from "obsidian";
|
||||||
import { getTriggerText } from "./trigger";
|
import { getTriggerText } from "./trigger";
|
||||||
import uFuzzy from "@leeoniya/ufuzzy";
|
import uFuzzy from "@leeoniya/ufuzzy";
|
||||||
import { getAPI, DataviewApi } from "obsidian-dataview";
|
|
||||||
import DataviewAutocompletePlugin from "./main";
|
import DataviewAutocompletePlugin from "./main";
|
||||||
|
import { SuggestionIndex } from "./SuggestionIndex";
|
||||||
|
|
||||||
export class DataviewSuggester extends EditorSuggest<String> {
|
export class DataviewSuggester extends EditorSuggest<String> {
|
||||||
plugin: DataviewAutocompletePlugin;
|
plugin: DataviewAutocompletePlugin;
|
||||||
|
|
||||||
maxSuggestions: number;
|
maxSuggestions: number;
|
||||||
searcher: uFuzzy;
|
searcher: uFuzzy;
|
||||||
|
suggestionsIndex: SuggestionIndex;
|
||||||
|
|
||||||
initialized: boolean = false;
|
initialized: boolean = false;
|
||||||
suggestionsList: string[] = [];
|
|
||||||
suggestionsRefs: Map<string, string[]> = new Map(); // maps file paths to included suggestions
|
|
||||||
suggestionsRefCount: Map<string, number> = new Map(); // maps suggestions to number of files including them
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
plugin: DataviewAutocompletePlugin,
|
plugin: DataviewAutocompletePlugin,
|
||||||
@@ -36,9 +37,10 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
intraMode: singleErrorMode ? 1 : 0,
|
intraMode: singleErrorMode ? 1 : 0,
|
||||||
intraIns: allowExtraChars ? 1 : 0,
|
intraIns: allowExtraChars ? 1 : 0,
|
||||||
});
|
});
|
||||||
|
this.suggestionsIndex = new SuggestionIndex(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null {
|
override onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null {
|
||||||
const line = editor.getLine(cursor.line);
|
const line = editor.getLine(cursor.line);
|
||||||
|
|
||||||
let trigger = getTriggerText(line, cursor.ch);
|
let trigger = getTriggerText(line, cursor.ch);
|
||||||
@@ -52,42 +54,89 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
override getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
||||||
const idxs = this.searcher.filter(this.suggestionsList, context.query);
|
const suggestionsList = this.suggestionsIndex.suggestionsList;
|
||||||
|
|
||||||
|
const idxs = this.searcher.filter(suggestionsList, context.query);
|
||||||
if (idxs != null && idxs.length > 0) {
|
if (idxs != null && idxs.length > 0) {
|
||||||
let info = this.searcher.info(idxs, this.suggestionsList, context.query);
|
let info = this.searcher.info(idxs, suggestionsList, context.query);
|
||||||
let order = this.searcher.sort(info, this.suggestionsList, context.query);
|
let order = this.searcher.sort(info, suggestionsList, context.query);
|
||||||
|
|
||||||
// return top N suggestions with marks
|
// return top N suggestions with marks
|
||||||
return order
|
return order
|
||||||
.slice(0, this.maxSuggestions)
|
.slice(0, this.maxSuggestions)
|
||||||
.map((idx) => [idx, this.suggestionsList[info.idx[idx]]])
|
.map((idx) => [idx, suggestionsList[info.idx[idx]]])
|
||||||
.map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]));
|
.map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]));
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
renderSuggestion(value: string, el: HTMLElement): void {
|
/**
|
||||||
// replace marks with bold
|
* Renders a suggestion string to the HTML element using a markdown renderer
|
||||||
const formattedHtml = value.replace(/<mark>(.*?)<\/mark>/g, '<span style="font-weight: bold;">$1</span>');
|
* Uses the context to get the current editor and cursor position and
|
||||||
el.innerHTML = formattedHtml;
|
* finds out if the suggestion should be rendered with or without the
|
||||||
|
* field name.
|
||||||
|
*/
|
||||||
|
renderMarkdownSuggestion(value: string, el: HTMLElement): void {
|
||||||
|
// render markdown preview of inline dataview metadata field
|
||||||
|
let suggestionText = this.context!.editor.getLine(this.context!.start.line).slice(
|
||||||
|
this.context!.start.ch - 1,
|
||||||
|
this.context!.start.ch,
|
||||||
|
)!;
|
||||||
|
suggestionText += value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
||||||
|
suggestionText += suggestionText.startsWith("(") ? ")" : "]";
|
||||||
|
|
||||||
|
MarkdownRenderer.render(
|
||||||
|
this.app,
|
||||||
|
htmlToMarkdown(suggestionText),
|
||||||
|
el,
|
||||||
|
this.context!.file.path,
|
||||||
|
this.app.workspace.getActiveViewOfType(MarkdownView)!,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
/**
|
||||||
const { editor, start, end } = this.context!;
|
* Render the suggestion as markdown source code with highlights
|
||||||
|
*/
|
||||||
|
renderSourceSuggestion(value: string, el: HTMLElement): void {
|
||||||
|
// render source text with highlights
|
||||||
|
// replaces <mark>...</mark> with <span>...</span>
|
||||||
|
const parts = value.split(/(<mark>.*?<\/mark>)/g);
|
||||||
|
parts.forEach((textPart) => {
|
||||||
|
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
||||||
|
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
||||||
|
el.createEl("span", { text, cls: "dataview-suggestion-highlight" });
|
||||||
|
} else {
|
||||||
|
el.appendText(textPart);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
override renderSuggestion(value: string, el: HTMLElement): void {
|
||||||
|
const container = el.createDiv("dataview-suggestion-content");
|
||||||
|
const titleDiv = container.createDiv("dataview-suggestion-title");
|
||||||
|
const noteDiv = container.createDiv("dataview-suggestion-note");
|
||||||
|
|
||||||
|
this.renderMarkdownSuggestion(value, titleDiv);
|
||||||
|
this.renderSourceSuggestion(value, noteDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
override selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
||||||
// remove marks from selection
|
// remove marks from selection
|
||||||
value = value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
value = value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
||||||
|
|
||||||
|
const { editor, start, end } = this.context!;
|
||||||
editor.replaceRange(value, start, end);
|
editor.replaceRange(value, start, end);
|
||||||
|
|
||||||
const newCursorPos = {
|
const newCursorPos = {
|
||||||
line: end.line + 1,
|
line: end.line,
|
||||||
ch: 0,
|
ch: start.ch + value.length + 1,
|
||||||
};
|
};
|
||||||
editor.setCursor(newCursorPos);
|
editor.setCursor(newCursorPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public onDataviewIndexReady() {
|
public onDataviewIndexReady() {
|
||||||
this.buildNewIndex();
|
this.suggestionsIndex.buildNewIndex();
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,216 +146,13 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
// console.warn("Dataview Autocompletion index not ready yet. Skipping index update");
|
// console.warn("Dataview Autocompletion index not ready yet. Skipping index update");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.updateIndex(type, file, oldPath);
|
this.suggestionsIndex.updateIndex(type, file, oldPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a Dataview value to a string
|
* Trigger a full rebuild of the suggestion index
|
||||||
* returns the composite value in format "key:: value"
|
|
||||||
*/
|
*/
|
||||||
formatCompositeValue(key: string, value: any): string {
|
public rebuildIndex() {
|
||||||
const dataviewAPI = getAPI(this.app);
|
this.suggestionsIndex.buildNewIndex();
|
||||||
|
|
||||||
let stringValue: string;
|
|
||||||
|
|
||||||
// If the value is a string, number or boolean, we can simply convert it to a string
|
|
||||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
||||||
stringValue = value.toString();
|
|
||||||
} else if (dataviewAPI.value.typeOf(value) === "link" && value.type === "file" && value.display !== undefined) {
|
|
||||||
// value.toString always adds a display value to wiki-style links.
|
|
||||||
// parse wiki-style links without display value manually here to prevent this from happening for [[filename]]
|
|
||||||
stringValue = `[[${value.path.split("/").pop().replace(".md", "")}]]`;
|
|
||||||
} else {
|
|
||||||
stringValue = dataviewAPI.value.toString(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${key}:: ${stringValue}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
filterCompositeValue(compositeValue: string): boolean {
|
|
||||||
for (const filterPattern of this.plugin.settings.ignoredFields) {
|
|
||||||
const regex = new RegExp(`^(${filterPattern})::.*`);
|
|
||||||
if (regex.test(compositeValue)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
filterFile(filepath: string): boolean {
|
|
||||||
for (const filterPattern of this.plugin.settings.ignoredFiles) {
|
|
||||||
const regex = new RegExp(filterPattern);
|
|
||||||
if (regex.test(filepath)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
buildNewIndex() {
|
|
||||||
console.log("Begin Rebuilding dataview suggestion index");
|
|
||||||
const startTime = performance.now();
|
|
||||||
const dataviewApi = getAPI(this.app);
|
|
||||||
|
|
||||||
const newSuggestions: string[] = [];
|
|
||||||
const newSuggestionsRefs: Map<string, string[]> = new Map();
|
|
||||||
const newSuggestionsRefCount: Map<string, number> = new Map();
|
|
||||||
|
|
||||||
const files = this.app.vault.getFiles();
|
|
||||||
for (const file of files) {
|
|
||||||
if (!this.filterFile(file.path)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const page = dataviewApi.page(file.path);
|
|
||||||
if (page === undefined) continue; // not a markdown file
|
|
||||||
|
|
||||||
const fields = Object.keys(page)
|
|
||||||
.filter((k) => k !== "file")
|
|
||||||
.map((k) => [k, page[k]]);
|
|
||||||
|
|
||||||
const pageRefs = [];
|
|
||||||
|
|
||||||
for (let [key, val] of fields) {
|
|
||||||
// fields can be a single value or a dict, so we need to handle both
|
|
||||||
let arrayVal;
|
|
||||||
if (!Array.isArray(val)) {
|
|
||||||
arrayVal = [val];
|
|
||||||
} else {
|
|
||||||
arrayVal = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add composite value "key:: value" to suggestions list
|
|
||||||
for (const value of arrayVal) {
|
|
||||||
if (value === null || value === undefined) continue; // skip empty fields
|
|
||||||
|
|
||||||
let compositeValue = this.formatCompositeValue(key, value);
|
|
||||||
if (!this.filterCompositeValue(compositeValue)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newSuggestions.indexOf(compositeValue) === -1) {
|
|
||||||
// suggestion not seen on any page yet
|
|
||||||
pageRefs.push(compositeValue);
|
|
||||||
newSuggestionsRefCount.set(compositeValue, 1);
|
|
||||||
newSuggestions.push(compositeValue);
|
|
||||||
} else if (pageRefs.indexOf(compositeValue) === -1) {
|
|
||||||
// suggestion not seen on this page, but on another
|
|
||||||
pageRefs.push(compositeValue);
|
|
||||||
newSuggestionsRefCount.set(compositeValue, newSuggestionsRefCount.get(compositeValue)! - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newSuggestionsRefs.set(file.path, pageRefs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// replace old index
|
|
||||||
this.suggestionsList = newSuggestions;
|
|
||||||
this.suggestionsRefCount = newSuggestionsRefCount;
|
|
||||||
this.suggestionsRefs = newSuggestionsRefs;
|
|
||||||
|
|
||||||
const endTime = performance.now();
|
|
||||||
console.log(
|
|
||||||
`Rebuilt dataview autocomplete index (${this.suggestionsList.length} elements, ${(endTime - startTime).toFixed(2)}ms)`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
updateIndex(type: string, file: TFile, oldPath?: string) {
|
|
||||||
// also triggers on create!
|
|
||||||
if (type === "update") {
|
|
||||||
if (!this.filterFile(file.path)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const updateCompositeValues = [];
|
|
||||||
|
|
||||||
const page = getAPI(this.app).page(file.path);
|
|
||||||
if (page === undefined) return; // not a markdown file
|
|
||||||
|
|
||||||
const fields = Object.keys(page)
|
|
||||||
.filter((k) => k !== "file")
|
|
||||||
.map((k) => [k, page[k]]);
|
|
||||||
for (let [key, val] of fields) {
|
|
||||||
// fields can be a single value or a dict, so we need to handle both
|
|
||||||
let arrayVal;
|
|
||||||
if (!Array.isArray(val)) {
|
|
||||||
arrayVal = [val];
|
|
||||||
} else {
|
|
||||||
arrayVal = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const value of arrayVal) {
|
|
||||||
if (value === null || value === undefined) continue; // skip empty fields
|
|
||||||
|
|
||||||
let compositeValue = this.formatCompositeValue(key, value);
|
|
||||||
if (!this.filterCompositeValue(compositeValue)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
|
||||||
updateCompositeValues.push(compositeValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const oldCompositeValues = this.suggestionsRefs.get(file.path) || [];
|
|
||||||
|
|
||||||
// deleting value from index if update reduces refcount to 0
|
|
||||||
for (const oldCompositeValue of oldCompositeValues) {
|
|
||||||
if (updateCompositeValues.indexOf(oldCompositeValue) === -1) {
|
|
||||||
// delete value
|
|
||||||
this.suggestionsRefCount.set(
|
|
||||||
oldCompositeValue,
|
|
||||||
this.suggestionsRefCount.get(oldCompositeValue)! - 1,
|
|
||||||
);
|
|
||||||
if (this.suggestionsRefCount.get(oldCompositeValue) === 0) {
|
|
||||||
this.suggestionsList.splice(this.suggestionsList.indexOf(oldCompositeValue), 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// adding value to index if not present in index
|
|
||||||
for (const newCompositeValue of updateCompositeValues) {
|
|
||||||
if (!this.suggestionsRefCount.has(newCompositeValue)) {
|
|
||||||
// not seen in this or other files
|
|
||||||
this.suggestionsList.push(newCompositeValue);
|
|
||||||
this.suggestionsRefCount.set(newCompositeValue, 1);
|
|
||||||
} else if (oldCompositeValues.indexOf(newCompositeValue) === -1) {
|
|
||||||
this.suggestionsRefCount.set(
|
|
||||||
newCompositeValue,
|
|
||||||
this.suggestionsRefCount.get(newCompositeValue)! + 1,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.suggestionsRefs.set(file.path, updateCompositeValues);
|
|
||||||
} else if (type === "rename") {
|
|
||||||
if (this.filterFile(file.path) && this.filterFile(oldPath!)) {
|
|
||||||
// both not ignored -> move refs
|
|
||||||
this.suggestionsRefs.set(file.path, this.suggestionsRefs.get(oldPath!)!);
|
|
||||||
this.suggestionsRefs.delete(oldPath!);
|
|
||||||
} else if (this.filterFile(file.path)) {
|
|
||||||
// old path ignored, new path not ignored -> upsert
|
|
||||||
this.updateIndex("update", file, undefined);
|
|
||||||
} else if (this.filterFile(oldPath!)) {
|
|
||||||
// old path not ignored, new path ignored -> delete
|
|
||||||
for (const value of this.suggestionsRefs.get(oldPath!)!) {
|
|
||||||
this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1);
|
|
||||||
if (this.suggestionsRefCount.get(value) === 0) {
|
|
||||||
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.suggestionsRefs.delete(oldPath!);
|
|
||||||
}
|
|
||||||
} else if (type === "delete") {
|
|
||||||
// iterate suggestion refs in deleted file and decrement their ref count
|
|
||||||
// if the ref count reaches 0, remove the suggestion from the list
|
|
||||||
for (const value of this.suggestionsRefs.get(file.path)!) {
|
|
||||||
this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1);
|
|
||||||
if (this.suggestionsRefCount.get(value) === 0) {
|
|
||||||
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.suggestionsRefs.delete(file.path);
|
|
||||||
} else {
|
|
||||||
console.debug("Unknown update type:", type, file, oldPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
.map((val) => val.trim())
|
.map((val) => val.trim())
|
||||||
.filter((val) => val.length > 0);
|
.filter((val) => val.length > 0);
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
this.plugin.suggester?.buildNewIndex();
|
this.plugin.suggester?.rebuildIndex();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
@@ -50,7 +50,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
.map((val) => val.trim())
|
.map((val) => val.trim())
|
||||||
.filter((val) => val.length > 0);
|
.filter((val) => val.length > 0);
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
this.plugin.suggester?.buildNewIndex();
|
this.plugin.suggester?.rebuildIndex();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
216
src/SuggestionIndex.ts
Normal file
216
src/SuggestionIndex.ts
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
import { App, TFile } from "obsidian";
|
||||||
|
import { DataObject, getAPI } from "obsidian-dataview";
|
||||||
|
import DataviewAutocompletePlugin from "./main";
|
||||||
|
|
||||||
|
export class SuggestionIndex {
|
||||||
|
plugin: DataviewAutocompletePlugin;
|
||||||
|
|
||||||
|
suggestionsList: string[] = [];
|
||||||
|
private suggestionsRefs: Map<string, string[]> = new Map(); // maps file paths to included suggestions
|
||||||
|
private suggestionsRefCount: Map<string, number> = new Map(); // maps suggestions to number of files including them
|
||||||
|
|
||||||
|
constructor(plugin: DataviewAutocompletePlugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public buildNewIndex() {
|
||||||
|
// console.log("Begin Rebuilding dataview suggestion index");
|
||||||
|
const startTime = performance.now();
|
||||||
|
const dataviewApi = getAPI(this.plugin.app);
|
||||||
|
|
||||||
|
const newSuggestions: string[] = [];
|
||||||
|
const newSuggestionsRefs: Map<string, string[]> = new Map();
|
||||||
|
const newSuggestionsRefCount: Map<string, number> = new Map();
|
||||||
|
|
||||||
|
const files = this.plugin.app.vault.getFiles();
|
||||||
|
for (const file of files) {
|
||||||
|
if (!this.filterFile(file.path)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = dataviewApi.page(file.path);
|
||||||
|
if (page === undefined) continue; // not a markdown file
|
||||||
|
|
||||||
|
const compositeValues = this.extractCompositeValuesFromPage(page);
|
||||||
|
const pageRefs = [];
|
||||||
|
|
||||||
|
for (let compositeValue of compositeValues) {
|
||||||
|
if (newSuggestions.indexOf(compositeValue) === -1) {
|
||||||
|
// suggestion not seen on any page yet
|
||||||
|
pageRefs.push(compositeValue);
|
||||||
|
newSuggestionsRefCount.set(compositeValue, 1);
|
||||||
|
newSuggestions.push(compositeValue);
|
||||||
|
} else if (pageRefs.indexOf(compositeValue) === -1) {
|
||||||
|
// suggestion not seen on this page, but on another
|
||||||
|
pageRefs.push(compositeValue);
|
||||||
|
newSuggestionsRefCount.set(compositeValue, newSuggestionsRefCount.get(compositeValue)! - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSuggestionsRefs.set(file.path, pageRefs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace old index
|
||||||
|
this.suggestionsList = newSuggestions;
|
||||||
|
this.suggestionsRefCount = newSuggestionsRefCount;
|
||||||
|
this.suggestionsRefs = newSuggestionsRefs;
|
||||||
|
|
||||||
|
const endTime = performance.now();
|
||||||
|
console.log(`Rebuilt dataview autocomplete index (${this.suggestionsList.length} elements, ${(endTime - startTime).toFixed(2)}ms)`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateIndex(type: string, file: TFile, oldPath?: string) {
|
||||||
|
// also triggers on create!
|
||||||
|
if (type === "update") {
|
||||||
|
if (!this.filterFile(file.path)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = getAPI(this.plugin.app).page(file.path);
|
||||||
|
if (page === undefined) return; // not a markdown file
|
||||||
|
const updateCompositeValues = this.extractCompositeValuesFromPage(page);
|
||||||
|
|
||||||
|
const oldCompositeValues = this.suggestionsRefs.get(file.path) || [];
|
||||||
|
|
||||||
|
// deleting value from index if update reduces refcount to 0
|
||||||
|
for (const oldCompositeValue of oldCompositeValues) {
|
||||||
|
if (updateCompositeValues.indexOf(oldCompositeValue) === -1) {
|
||||||
|
// delete value
|
||||||
|
this.suggestionsRefCount.set(
|
||||||
|
oldCompositeValue,
|
||||||
|
this.suggestionsRefCount.get(oldCompositeValue)! - 1,
|
||||||
|
);
|
||||||
|
if (this.suggestionsRefCount.get(oldCompositeValue) === 0) {
|
||||||
|
this.suggestionsList.splice(this.suggestionsList.indexOf(oldCompositeValue), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding value to index if not present in index
|
||||||
|
for (const newCompositeValue of updateCompositeValues) {
|
||||||
|
if (
|
||||||
|
!this.suggestionsRefCount.has(newCompositeValue) ||
|
||||||
|
this.suggestionsRefCount.get(newCompositeValue) === 0
|
||||||
|
) {
|
||||||
|
// not seen in this or other files
|
||||||
|
this.suggestionsList.push(newCompositeValue);
|
||||||
|
this.suggestionsRefCount.set(newCompositeValue, 1);
|
||||||
|
} else if (oldCompositeValues.indexOf(newCompositeValue) === -1) {
|
||||||
|
this.suggestionsRefCount.set(
|
||||||
|
newCompositeValue,
|
||||||
|
this.suggestionsRefCount.get(newCompositeValue)! + 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.suggestionsRefs.set(file.path, updateCompositeValues);
|
||||||
|
} else if (type === "rename") {
|
||||||
|
if (this.filterFile(file.path) && this.filterFile(oldPath!)) {
|
||||||
|
// both not ignored -> move refs
|
||||||
|
this.suggestionsRefs.set(file.path, this.suggestionsRefs.get(oldPath!)!);
|
||||||
|
this.suggestionsRefs.delete(oldPath!);
|
||||||
|
} else if (this.filterFile(file.path)) {
|
||||||
|
// old path ignored, new path not ignored -> upsert
|
||||||
|
this.updateIndex("update", file, undefined);
|
||||||
|
} else if (this.filterFile(oldPath!)) {
|
||||||
|
// old path not ignored, new path ignored -> delete
|
||||||
|
for (const value of this.suggestionsRefs.get(oldPath!)!) {
|
||||||
|
this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1);
|
||||||
|
if (this.suggestionsRefCount.get(value) === 0) {
|
||||||
|
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.suggestionsRefs.delete(oldPath!);
|
||||||
|
}
|
||||||
|
} else if (type === "delete") {
|
||||||
|
// iterate suggestion refs in deleted file and decrement their ref count
|
||||||
|
// if the ref count reaches 0, remove the suggestion from the list
|
||||||
|
for (const value of this.suggestionsRefs.get(file.path)!) {
|
||||||
|
this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1);
|
||||||
|
if (this.suggestionsRefCount.get(value) === 0) {
|
||||||
|
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.suggestionsRefs.delete(file.path);
|
||||||
|
} else {
|
||||||
|
console.debug("Unknown update type:", type, file, oldPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extractCompositeValuesFromPage(page: DataObject): string[] {
|
||||||
|
const fields = Object.keys(page)
|
||||||
|
.filter((k) => k !== "file")
|
||||||
|
.map((k) => [k, page[k]]);
|
||||||
|
|
||||||
|
const compositeValues: string[] = [];
|
||||||
|
|
||||||
|
for (let [key, val] of fields) {
|
||||||
|
// fields can be a single value or a dict, so we need to handle both
|
||||||
|
let arrayVal;
|
||||||
|
if (!Array.isArray(val)) {
|
||||||
|
arrayVal = [val];
|
||||||
|
} else {
|
||||||
|
arrayVal = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add composite value "key:: value" to suggestions list
|
||||||
|
for (const value of arrayVal) {
|
||||||
|
if (value === null || value === undefined) continue; // skip empty fields
|
||||||
|
|
||||||
|
let compositeValue = this.formatCompositeValue(key, value);
|
||||||
|
compositeValues.push(compositeValue);
|
||||||
|
if (!this.filterCompositeValue(compositeValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return compositeValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters out composite values from suggestions that match any of the ignored fields.
|
||||||
|
* @returns true if the value should be shown in suggestions, false otherwise
|
||||||
|
*/
|
||||||
|
filterCompositeValue(compositeValue: string): boolean {
|
||||||
|
for (const filterPattern of this.plugin.settings.ignoredFields) {
|
||||||
|
const regex = new RegExp(`^(${filterPattern})::.*`);
|
||||||
|
if (regex.test(compositeValue)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a Dataview value to a string
|
||||||
|
* returns the composite value in format "key:: value"
|
||||||
|
*/
|
||||||
|
formatCompositeValue(key: string, value: any): string {
|
||||||
|
const dataviewAPI = getAPI(this.plugin.app);
|
||||||
|
|
||||||
|
let stringValue: string;
|
||||||
|
|
||||||
|
// If the value is a string, number or boolean, we can simply convert it to a string
|
||||||
|
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
||||||
|
stringValue = value.toString();
|
||||||
|
} else if (dataviewAPI.value.typeOf(value) === "link" && value.type === "file") {
|
||||||
|
// parse wiki-style links
|
||||||
|
if (value.display !== undefined) {
|
||||||
|
stringValue = `[[${value.path.split("/").pop().replace(".md", "")}|${value.display}]]`;
|
||||||
|
} else {
|
||||||
|
stringValue = `[[${value.path.split("/").pop().replace(".md", "")}]]`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stringValue = dataviewAPI.value.toString(value);
|
||||||
|
}
|
||||||
|
return `${key}:: ${stringValue}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
filterFile(filepath: string): boolean {
|
||||||
|
for (const filterPattern of this.plugin.settings.ignoredFiles) {
|
||||||
|
const regex = new RegExp(filterPattern);
|
||||||
|
if (regex.test(filepath)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,29 +3,28 @@
|
|||||||
* and captures the enclosed text
|
* and captures the enclosed text
|
||||||
* If the brackets start a markdown link, they are ignored.
|
* If the brackets start a markdown link, they are ignored.
|
||||||
* Wiki links don't need to be ignored since Obsidian overwrites suggestions for them.
|
* Wiki links don't need to be ignored since Obsidian overwrites suggestions for them.
|
||||||
|
* We are not allowd to use lookbehinds, because iOS does not support them in older versions.
|
||||||
*/
|
*/
|
||||||
const filledRegex = new RegExp(
|
const filledRegex = new RegExp(
|
||||||
"(" +
|
[
|
||||||
[
|
// pattern for parantheses
|
||||||
// pattern for parantheses
|
// look for opening parantheses; no closing or opening square bracket before to exlude markdown links, etc.
|
||||||
// pos. lookbehind for opening parantheses; no closing square bracket before to exlude markdown links
|
/(?:^\(|[^\[\]]\()/,
|
||||||
/(?<=^\(|[^\]]\()/,
|
/(.+?)/,
|
||||||
/.+?/,
|
// pos. lookahead for closing paranthesis; not followed by another one for nested ((test))
|
||||||
// pos. lookahead for closing paranthesis; not followed by another one for nested ((test))
|
/(?=\)$|\)[^\)])/,
|
||||||
/(?=\)$|\)[^\)])/,
|
|
||||||
|
|
||||||
/|/,
|
/|/,
|
||||||
|
|
||||||
// pattern for square brackets
|
// pattern for square brackets
|
||||||
// pos. lookbehind for opening square bracket
|
// look for opening square brackets
|
||||||
/(?<=\[)/,
|
/(?:\[)/,
|
||||||
/.+?/,
|
/(.+?)/,
|
||||||
// pos. lookahead for closing bracket; not followed by another one ([[test]]) or an opening paranthese (markdown link!)
|
// pos. lookahead for closing bracket; not followed by another one ([[test]]) or an opening paranthese (markdown link!)
|
||||||
/(?=\]$|\][^\]\(])/,
|
/(?=\]$|\][^\]\(])/,
|
||||||
]
|
]
|
||||||
.map((s) => s.source)
|
.map((s) => s.source)
|
||||||
.join("") +
|
.join(""),
|
||||||
")",
|
|
||||||
"g",
|
"g",
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -57,12 +56,13 @@ function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const matchStart = match!.index;
|
const matchText = match[1] || match[2];
|
||||||
const matchEnd = matchStart + match[1].length;
|
const matchStart = match!.index + match[0].indexOf(matchText);
|
||||||
|
const matchEnd = matchStart + matchText.length;
|
||||||
const cursorInMatch = cursorPos >= matchStart && cursorPos <= matchEnd;
|
const cursorInMatch = cursorPos >= matchStart && cursorPos <= matchEnd;
|
||||||
|
|
||||||
if (cursorInMatch) {
|
if (cursorInMatch) {
|
||||||
return [match[1], matchStart, matchEnd];
|
return [matchText, matchStart, matchEnd];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
29
styles.css
Normal file
29
styles.css
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
div.dataview-suggestion-content {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
margin-inline-end: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.dataview-suggestion-title {
|
||||||
|
overflow-wrap: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.dataview-suggestion-title p {
|
||||||
|
margin-block: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.dataview-suggestion-note{
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
width: 100%;
|
||||||
|
flex-basis: 100%;
|
||||||
|
overflow-wrap: break-word;
|
||||||
|
padding-left: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
span.dataview-suggestion-highlight {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user