Skip to content

Build your personal knowledge base with Trilium Notes

custom task states: add a template in the markdown slash commands

Adorian Doran0a271b2545ab
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 7cad042c3d..8033a727b3 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -266,7 +266,9 @@
"footnote": "Insert a footnote",
"mermaid": "Insert a Mermaid diagram",
"collapsible": "Insert a collapsible block",
- "admonition": "Insert {{type}} admonition"
+ "admonition": "Insert {{type}} admonition",
+ "todo": "Insert a \"{{title}}\" task item",
+ "todo_nonstandard": "Insert a \"{{title}}\" task item (non-standard)"
},
"move_to": {
"dialog_title": "Move notes to ...",
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
index 1c6ca302ea..987604ee5e 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { renderWithSourceLines } from "./Markdown.js";
+import { buildTaskItemInsert, renderWithSourceLines } from "./Markdown.js";
describe("renderWithSourceLines", () => {
function extractLines(src: string): number[] {
@@ -84,6 +84,21 @@ describe("renderWithSourceLines", () => {
expect(html("Energy: $e=mc^2$.")).toContain('<span class="math-tex">');
});
+ it("renders the default /todo:<state> templates to their task states", () => {
+ // The `/todo:*` commands insert `- [<symbol>] `; verify each default state's marker
+ // round-trips to the right rendered task item. The ` ` (unchecked) and `x` (checked)
+ // anchors map to native checkboxes; custom states carry data-trilium-task-state.
+ const unchecked = html("- [ ] groceries");
+ expect(unchecked).toContain('type="checkbox"');
+ expect(unchecked).not.toContain("checked");
+ expect(unchecked).not.toContain("data-trilium-task-state");
+
+ expect(html("- [x] groceries")).toContain("checked");
+ expect(html("- [/] groceries")).toContain('data-trilium-task-state="doing"');
+ expect(html("- [?] groceries")).toContain('data-trilium-task-state="maybe"');
+ expect(html("- [-] groceries")).toContain('data-trilium-task-state="cancelled"');
+ });
+
it("renders the /collapsible template as a details block", () => {
// Mirrors the markdown the `/collapsible` slash command inserts.
const src = [
@@ -176,3 +191,17 @@ describe("renderWithSourceLines", () => {
}
});
});
+
+describe("buildTaskItemInsert", () => {
+ it("prepends a bullet when not already in a list item", () => {
+ expect(buildTaskItemInsert(" ", false)).toBe("- [ ] ");
+ expect(buildTaskItemInsert("x", false)).toBe("- [x] ");
+ expect(buildTaskItemInsert("/", false)).toBe("- [/] ");
+ });
+
+ it("reuses an existing '- ' bullet to avoid a doubled marker", () => {
+ expect(buildTaskItemInsert(" ", true)).toBe("[ ] ");
+ expect(buildTaskItemInsert("x", true)).toBe("[x] ");
+ expect(buildTaskItemInsert("/", true)).toBe("[/] ");
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 3a4184386f..b4fe21e9f8 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -5,7 +5,7 @@ import { autocompletion } from "@codemirror/autocomplete";
import { syntaxTree } from "@codemirror/language";
import type { SyntaxNode } from "@lezer/common";
import VanillaCodeMirror from "@triliumnext/codemirror";
-import { CustomMarkdownRenderer, renderToHtml } from "@triliumnext/commons";
+import { CustomMarkdownRenderer, isAnchorState, renderToHtml, type TaskStateDef } from "@triliumnext/commons";
import DOMPurify from "dompurify";
import { Marked, type Tokens } from "marked";
import { createContext } from "preact";
@@ -21,6 +21,7 @@ import note_create from "../../../services/note_create";
import options from "../../../services/options";
import server from "../../../services/server";
import { removeIndividualBinding } from "../../../services/shortcuts";
+import { getTaskStateDefinitions } from "../../../services/task_states";
import toast from "../../../services/toast";
import tree from "../../../services/tree";
import utils, { isDesktop } from "../../../services/utils";
@@ -484,6 +485,15 @@ function useMarkdownKeymap(editorView: VanillaCodeMirror | null) {
//#endregion
//#region Slash commands
+/**
+ * Builds the markdown a `/todo:<state>` command inserts. Omits the leading `- `
+ * bullet when the slash was typed right after an existing one (e.g. `- /todo:doing`),
+ * so the existing bullet is reused instead of producing a doubled `- - [ ] ` marker.
+ */
+export function buildTaskItemInsert(symbol: string, precededByBullet: boolean): string {
+ return `${precededByBullet ? "" : "- "}[${symbol}] `;
+}
+
/**
* Adds `/`-triggered autocomplete to the CodeMirror editor.
* Typing `/` at the start of a line (or after whitespace) shows a menu of commands.
@@ -494,15 +504,20 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
// `appendConfig` would otherwise stack a duplicate extension on each switch.
const noteRef = useRef(note);
const parentRef = useRef(parentComponent);
+ // The user-configured todo task states (from the `_taskStates` subtree), loaded once.
+ // Read inside the autocomplete closure, so `/todo:*` commands reflect the current config.
+ const taskStatesRef = useRef<TaskStateDef[]>([]);
useEffect(() => { noteRef.current = note; }, [note]);
useEffect(() => { parentRef.current = parentComponent; }, [parentComponent]);
+ useEffect(() => { void getTaskStateDefinitions().then((states) => { taskStatesRef.current = states; }); }, []);
useEffect(() => {
if (!editorView) return;
const ext = autocompletion({
override: [(ctx) => {
- const match = ctx.matchBefore(/(?:^|(?<=\s))\/\w*/);
+ // `:` and `-` are allowed so `/todo:<state>` (e.g. `/todo:in-progress`) matches as one token.
+ const match = ctx.matchBefore(/(?:^|(?<=\s))\/[\w:-]*/);
if (!match) return null;
// Suppress slash menu inside fenced/indented code blocks and inline code spans —
@@ -626,7 +641,27 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
selection: { anchor: from + template.length }
});
}
- }))
+ })),
+ // One `/todo:<state>` per configured task state that has a markdown marker —
+ // the ` ` (unchecked) and `x` (checked) anchors are markers too, so both are covered.
+ ...taskStatesRef.current
+ .filter((state) => state.markdownSymbol)
+ .map((state) => {
+ // Anchors (`none`/`done`) are the standard `[ ]`/`[x]`; custom states
+ // use non-standard markers (e.g. `[/]`), so flag those in the description.
+ const detailKey = isAnchorState(state.name)
+ ? "markdown_slash_commands.todo"
+ : "markdown_slash_commands.todo_nonstandard";
+ return {
+ label: `/todo:${state.name}`,
+ detail: t(detailKey, { title: state.title }),
+ apply(view: import("@codemirror/view").EditorView, _c: unknown, from: number, to: number) {
+ const precededByBullet = from >= 2 && view.state.doc.sliceString(from - 2, from) === "- ";
+ const insert = buildTaskItemInsert(state.markdownSymbol, precededByBullet);
+ view.dispatch({ changes: { from, to, insert } });
+ }
+ };
+ })
]
};
}],