Skip to content

Build your personal knowledge base with Trilium Notes

Add /table slash command to Markdown editor

Elian Doran1c72fb91309e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f2ab53ef75..366c86929f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -312,13 +312,15 @@
"mermaid_template": "Insert a \"{{name}}\" Mermaid diagram",
"collapsible": "Insert a collapsible block",
"page_break": "Insert a page break (for printing)",
+ "table": "Insert a table",
"admonition": "Insert {{type}} admonition",
"todo": "Insert a \"{{title}}\" task item",
"todo_nonstandard": "Insert a \"{{title}}\" task item (non-standard)",
"placeholders": {
"math": "equation",
"collapsible_summary": "Summary",
- "collapsible_details": "Details"
+ "collapsible_details": "Details",
+ "table_column": "Column {{number}}"
}
},
"slash_commands": {
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 c9e2f1d417..696e00d18c 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
@@ -132,6 +132,23 @@ describe("renderWithSourceLines", () => {
expect(result).toContain("<p>After the break</p>");
});
+ it("renders the /table skeleton as a table without swallowing following content", () => {
+ // Mirrors the GFM table the `/table` slash command inserts (note the trailing blank line),
+ // which must terminate the table block so following text renders as its own paragraph.
+ const src = [
+ "| Column 1 | Column 2 |",
+ "| -------- | -------- |",
+ "| | |",
+ "",
+ "After the table"
+ ].join("\n");
+
+ const result = html(src);
+ expect(result).toContain("<table>");
+ expect(result).toContain("<th>Column 1</th>");
+ expect(result).toContain("<p>After the table</p>");
+ });
+
it("renders [[wikilinks]] with hash-router hrefs so the preview navigates correctly", () => {
const result = html("See [[abc123]] for details.");
expect(result).toContain('class="reference-link"');
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 357bd9a614..4e43adc009 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -668,6 +668,26 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
});
}
},
+ {
+ label: "/table",
+ detail: t("markdown_slash_commands.table"),
+ apply(view, _completion, from, to) {
+ // GFM table skeleton. The trailing blank line terminates the table block
+ // so following text isn't absorbed into it (see page break above).
+ const header = t("markdown_slash_commands.placeholders.table_column", { number: 1 });
+ const table = [
+ `| ${header} | ${t("markdown_slash_commands.placeholders.table_column", { number: 2 })} |`,
+ `| -------- | -------- |`,
+ `| | |`
+ ].join("\n");
+ // Select the first header cell so the user can type the first column name.
+ const anchor = from + 2; // skip the leading "| "
+ view.dispatch({
+ changes: { from, to, insert: `${table}\n\n` },
+ selection: { anchor, head: anchor + header.length }
+ });
+ }
+ },
...["note", "tip", "important", "caution", "warning"].map((admonitionType) => ({
label: `/${admonitionType}`,
detail: t("markdown_slash_commands.admonition", { type: admonitionType }),