Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): add a /tabs slash command

Elian Doran01d2f46c0230
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index c73e39ee18..831dccf36f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -371,6 +371,7 @@
"mermaid": "Insert a Mermaid diagram",
"mermaid_template": "Insert a \"{{name}}\" Mermaid diagram",
"collapsible": "Insert a collapsible block",
+ "tabs": "Insert titled tabs that show one panel at a time",
"page_break": "Insert a page break (for printing)",
"table": "Insert a table",
"admonition": "Insert {{type}} admonition",
@@ -386,6 +387,7 @@
"mermaid": "Mermaid diagram",
"mermaid_template": "Mermaid diagram: {{name}}",
"collapsible": "Collapsible block",
+ "tabs": "Tabs",
"page_break": "Page break",
"table": "Table",
"note": "Note",
@@ -399,6 +401,8 @@
"math": "equation",
"collapsible_summary": "Summary",
"collapsible_details": "Details",
+ "tab_title": "Tab {{number}}",
+ "tab_content": "Content",
"table_column": "Column {{number}}"
}
},
diff --git a/apps/client/src/widgets/type_widgets/markdown/completions.spec.ts b/apps/client/src/widgets/type_widgets/markdown/completions.spec.ts
index 317d80d3a1..fa91947060 100644
--- a/apps/client/src/widgets/type_widgets/markdown/completions.spec.ts
+++ b/apps/client/src/widgets/type_widgets/markdown/completions.spec.ts
@@ -286,6 +286,28 @@ describe("buildSlashCommands", () => {
expect(editor.state.sliceDoc(from, to)).toBe("markdown_slash_commands.placeholders.table_column");
});
+ it("inserts two tabs on a line of their own and selects the first title", () => {
+ const tabs = [
+ "=== \"markdown_slash_commands.placeholders.tab_title\"",
+ "",
+ " markdown_slash_commands.placeholders.tab_content",
+ "",
+ "=== \"markdown_slash_commands.placeholders.tab_title\"",
+ "",
+ " markdown_slash_commands.placeholders.tab_content"
+ ].join("\n");
+ expect(commands().find((entry) => entry.id === "tabs")).toMatchObject({
+ title: "markdown_slash_commands.titles.tabs",
+ aliases: expect.arrayContaining([ "tabs", "tabbed", "variants" ])
+ });
+
+ expect(run("tabs", "/tabs")).toBe(tabs);
+ const { from, to } = editor.state.selection.main;
+ expect([ from, to ]).toEqual([ 5, 5 + "markdown_slash_commands.placeholders.tab_title".length ]);
+
+ expect(run("tabs", "Intro /tabs")).toBe(`Intro\n\n${tabs}`);
+ });
+
it("uploads an image picked to the note shown once the file arrives", () => {
let picker: HTMLInputElement | undefined;
const click = vi.spyOn(HTMLInputElement.prototype, "click").mockImplementation(function (this: HTMLInputElement) { picker = this; });
diff --git a/apps/client/src/widgets/type_widgets/markdown/completions.tsx b/apps/client/src/widgets/type_widgets/markdown/completions.tsx
index 40ae453ae5..d4ab2efcec 100644
--- a/apps/client/src/widgets/type_widgets/markdown/completions.tsx
+++ b/apps/client/src/widgets/type_widgets/markdown/completions.tsx
@@ -8,6 +8,7 @@ import dateTimeIcon from "@triliumnext/ckeditor5/src/icons/date-time.svg?raw";
import insertFootnoteIcon from "@triliumnext/ckeditor5/src/icons/insert-footnote.svg?raw";
import mathIcon from "@triliumnext/ckeditor5/src/icons/math.svg?raw";
import noteIcon from "@triliumnext/ckeditor5/src/icons/note.svg?raw";
+import tabsIcon from "@triliumnext/ckeditor5/src/icons/tabs.svg?raw";
import internalLinkIcon from "@triliumnext/ckeditor5/src/icons/trilium.svg?raw";
import type VanillaCodeMirror from "@triliumnext/codemirror";
import { hostedCompletion, type HostedCompletionApply, type HostedCompletionState } from "@triliumnext/codemirror/src/extensions/hosted_completion";
@@ -271,6 +272,27 @@ export function buildSlashCommands({ parentComponent, getNote, editorView, taskS
selection: { anchor, head: anchor + placeholder.length }
});
}),
+ command("tabs", {
+ title: t("markdown_slash_commands.titles.tabs"),
+ description: t("markdown_slash_commands.tabs"),
+ aliases: [ "tab", "tabbed", "tab group", "panels", "variants" ],
+ iconSvg: tabsIcon
+ }, (view, from, to) => {
+ // The `=== "Title"` syntax of Material for MkDocs, which the Markdown export writes for
+ // a tabs block (see marked_extensions.ts). A header only counts at the start of a line.
+ const lineStart = view.state.doc.lineAt(from).from;
+ const before = view.state.sliceDoc(lineStart, from);
+ const start = from - (before.length - before.trimEnd().length);
+ const prefix = before.trim() ? "\n\n" : "";
+ const content = t("markdown_slash_commands.placeholders.tab_content");
+ const tab = (number: number) =>
+ `=== "${t("markdown_slash_commands.placeholders.tab_title", { number })}"\n\n ${content}`;
+ const anchor = start + prefix.length + 5;
+ view.dispatch({
+ changes: { from: start, to, insert: `${prefix}${tab(1)}\n\n${tab(2)}` },
+ selection: { anchor, head: anchor + t("markdown_slash_commands.placeholders.tab_title", { number: 1 }).length }
+ });
+ }),
command("page-break", {
title: t("markdown_slash_commands.titles.page_break"),
description: t("markdown_slash_commands.page_break"),
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
index e48e9b0fe1..3f0871d65d 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Markdown.html
@@ -199,6 +199,7 @@
<li>Inserting a <a href="#root/_help_s1aBHPd79XYj">Mermaid</a> diagram (<code>/mermaid</code>),
with one variant per sample template (e.g. <code>/mermaid:flowchart</code>).</li>
<li>Inserting a collapsible block (<code>/collapsible</code>).</li>
+ <li>Inserting <a href="#root/_help_rJ9grSgoExl9">tabs</a> (<code>/tabs</code>).</li>
<li>Inserting a page break for printing (<code>/page-break</code>).</li>
<li>Inserting a table (<code>/table</code>).</li>
<li>Creating admonitions (e.g. <code>/tip</code>, <code>/note</code>, <code>/important</code>, <code>/caution</code>, <code>/warning</code>).</li>
diff --git a/docs/User Guide/User Guide/Note Types/Markdown.md b/docs/User Guide/User Guide/Note Types/Markdown.md
index f22742c367..8ee715b80b 100644
--- a/docs/User Guide/User Guide/Note Types/Markdown.md
+++ b/docs/User Guide/User Guide/Note Types/Markdown.md
@@ -165,6 +165,7 @@ Just like <a class="reference-link" href="Text.md">Text</a> notes, Markdown no
* Inserting a [footnote](Text/Footnotes.md) (`/footnote`).
* Inserting a [Mermaid](Mermaid%20Diagrams.md) diagram (`/mermaid`), with one variant per sample template (e.g. `/mermaid:flowchart`).
* Inserting a collapsible block (`/collapsible`).
+* Inserting [tabs](../Basic%20Concepts%20and%20Features/Import%20%26%20Export/Markdown/Supported%20syntax.md) (`/tabs`).
* Inserting a page break for printing (`/page-break`).
* Inserting a table (`/table`).
* Creating admonitions (e.g. `/tip`, `/note`, `/important`, `/caution`, `/warning`).