mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-11 14:45:30 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3f44af074 | ||
|
|
57be38b005 | ||
|
|
e744b48536 | ||
|
|
b79d363c9f | ||
|
|
2d9194265a | ||
|
|
27f2e64884 | ||
|
|
779bd0bd68 | ||
|
|
910d343948 | ||
|
|
5802a6cd5d | ||
|
|
d85c07126f | ||
|
|
020054857b | ||
|
|
f58f736df4 |
16
README.md
16
README.md
@@ -3,10 +3,24 @@
|
||||
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)
|
||||
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.
|
||||
|
||||

|
||||
|
||||
## 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
|
||||
|
||||
This project is licensed under the **MIT** License. See [LICENSE](./LICENSE)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "dataview-autocompletion",
|
||||
"name": "Dataview Autocompletion",
|
||||
"version": "0.8.2",
|
||||
"version": "0.9.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Adds autocompletion to Dataview metadata fields",
|
||||
"author": "Daniel Bauer",
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
{
|
||||
"name": "obsidian-dataview-autocompletion",
|
||||
"version": "0.8.2",
|
||||
"version": "0.9.0",
|
||||
"description": "This is a plugin for Obsidian that provides autocompletion for Dataview metadata fields",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "npx rollup --config rollup.config.js -w",
|
||||
"build": "npx rollup --config rollup.config.js --environment BUILD:production",
|
||||
"check-format": "npx prettier --check src",
|
||||
"fix-format": "npx prettier --write src",
|
||||
"test": "npx jest",
|
||||
"bdd": "npx jest -i --watch --no-cache",
|
||||
"version": "node ersion-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": [
|
||||
|
||||
@@ -14,6 +14,10 @@ const createBaseConfig = (outdir) => ({
|
||||
src: "manifest.json",
|
||||
dest: outdir,
|
||||
},
|
||||
{
|
||||
src: "styles.css",
|
||||
dest: outdir,
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -5,12 +5,16 @@ import {
|
||||
EditorSuggest,
|
||||
EditorSuggestContext,
|
||||
EditorSuggestTriggerInfo,
|
||||
htmlToMarkdown,
|
||||
MarkdownRenderer,
|
||||
MarkdownView,
|
||||
TFile,
|
||||
} from "obsidian";
|
||||
import { getTriggerText } from "./trigger";
|
||||
import uFuzzy from "@leeoniya/ufuzzy";
|
||||
import { getAPI, DataviewApi } from "obsidian-dataview";
|
||||
import DataviewAutocompletePlugin from "./main";
|
||||
import { parentPort } from "worker_threads";
|
||||
|
||||
export class DataviewSuggester extends EditorSuggest<String> {
|
||||
plugin: DataviewAutocompletePlugin;
|
||||
@@ -68,9 +72,40 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
||||
}
|
||||
|
||||
renderSuggestion(value: string, el: HTMLElement): void {
|
||||
// replace marks with bold
|
||||
const formattedHtml = value.replace(/<mark>(.*?)<\/mark>/g, '<span style="font-weight: bold;">$1</span>');
|
||||
el.innerHTML = formattedHtml;
|
||||
// Split the value into parts based on the <mark> tags and their content
|
||||
const parts = value.split(/(<mark>.*?<\/mark>)/g);
|
||||
|
||||
const container = el.createDiv("dataview-suggestion-content");
|
||||
const titleDiv = container.createDiv("dataview-suggestion-title");
|
||||
|
||||
parts.forEach((textPart) => {
|
||||
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
||||
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
||||
titleDiv.createEl("span", { text, cls: "dataview-suggestion-highlight" });
|
||||
} else {
|
||||
titleDiv.appendText(textPart);
|
||||
}
|
||||
});
|
||||
|
||||
if (value.indexOf("[[") !== -1 || value.indexOf("]]") !== -1 || value.indexOf("](") !== -1) {
|
||||
const noteDiv = container.createDiv("dataview-suggestion-note");
|
||||
let suggestionText = "";
|
||||
parts.forEach((textPart) => {
|
||||
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
||||
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
||||
suggestionText += text;
|
||||
} else {
|
||||
suggestionText += textPart;
|
||||
}
|
||||
});
|
||||
MarkdownRenderer.render(
|
||||
this.app,
|
||||
htmlToMarkdown(suggestionText),
|
||||
noteDiv,
|
||||
this.context!.file.path,
|
||||
this.app.workspace.getActiveViewOfType(MarkdownView)!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
||||
@@ -112,14 +147,16 @@ export class DataviewSuggester extends EditorSuggest<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 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}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,29 +3,28 @@
|
||||
* and captures the enclosed text
|
||||
* If the brackets start a markdown link, they are ignored.
|
||||
* 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(
|
||||
"(" +
|
||||
[
|
||||
// pattern for parantheses
|
||||
// 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))
|
||||
/(?=\)$|\)[^\)])/,
|
||||
[
|
||||
// pattern for parantheses
|
||||
// look for opening parantheses; no closing or opening square bracket before to exlude markdown links, etc.
|
||||
/(?:^\(|[^\[\]]\()/,
|
||||
/(.+?)/,
|
||||
// pos. lookahead for closing paranthesis; not followed by another one for nested ((test))
|
||||
/(?=\)$|\)[^\)])/,
|
||||
|
||||
/|/,
|
||||
/|/,
|
||||
|
||||
// pattern for square brackets
|
||||
// pos. lookbehind for opening square bracket
|
||||
/(?<=\[)/,
|
||||
/.+?/,
|
||||
// pos. lookahead for closing bracket; not followed by another one ([[test]]) or an opening paranthese (markdown link!)
|
||||
/(?=\]$|\][^\]\(])/,
|
||||
]
|
||||
.map((s) => s.source)
|
||||
.join("") +
|
||||
")",
|
||||
// pattern for square brackets
|
||||
// look for opening square brackets
|
||||
/(?:\[)/,
|
||||
/(.+?)/,
|
||||
// pos. lookahead for closing bracket; not followed by another one ([[test]]) or an opening paranthese (markdown link!)
|
||||
/(?=\]$|\][^\]\(])/,
|
||||
]
|
||||
.map((s) => s.source)
|
||||
.join(""),
|
||||
"g",
|
||||
);
|
||||
|
||||
@@ -57,12 +56,13 @@ function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp)
|
||||
continue;
|
||||
}
|
||||
|
||||
const matchStart = match!.index;
|
||||
const matchEnd = matchStart + match[1].length;
|
||||
const matchText = match[1] || match[2];
|
||||
const matchStart = match!.index + match[0].indexOf(matchText);
|
||||
const matchEnd = matchStart + matchText.length;
|
||||
const cursorInMatch = cursorPos >= matchStart && cursorPos <= matchEnd;
|
||||
|
||||
if (cursorInMatch) {
|
||||
return [match[1], matchStart, matchEnd];
|
||||
return [matchText, matchStart, matchEnd];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
28
styles.css
Normal file
28
styles.css
Normal file
@@ -0,0 +1,28 @@
|
||||
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-note 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;
|
||||
}
|
||||
|
||||
span.dataview-suggestion-highlight {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user