Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
text editor/slash commands: add Mermaid diagram templates
Adorian Doran5f470735193e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 8033a727b3..b38528918f 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2527,6 +2527,8 @@"mermaid": {"placeholder": "Type the content of your Mermaid diagram or use one of the sample diagrams below.","sample_diagrams": "Sample diagrams:",+ "slash_command_title": "Mermaid Diagram: {{name}}",+ "slash_command_description": "Insert a \"{{name}}\" Mermaid diagram template","sample_flowchart": "Flowchart","sample_class": "Class","sample_sequence": "Sequence",diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.tsindex 69cb9ba102..93e0aa0e97 100644--- a/apps/client/src/widgets/type_widgets/text/config.ts+++ b/apps/client/src/widgets/type_widgets/text/config.ts@@ -4,12 +4,13 @@ import { ALLOWED_PROTOCOLS, DISPLAYABLE_LOCALE_IDS, MIME_TYPE_AUTO, normalizeMimimport { copyTextWithToast } from "../../../services/clipboard_ext.js";import { t } from "../../../services/i18n.js";-import { getTaskStateDefinitions, openCustomTaskStateConfig } from "../../../services/task_states.js";import { getMermaidConfig } from "../../../services/mermaid.js";import { default as mimeTypesService, getHighlightJsNameForMime } from "../../../services/mime_types.js";import noteAutocompleteService, { type Suggestion } from "../../../services/note_autocomplete.js";import options from "../../../services/options.js";import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";+import { getTaskStateDefinitions, openCustomTaskStateConfig } from "../../../services/task_states.js";+import SAMPLE_DIAGRAMS from "../mermaid/sample_diagrams.js";import { buildToolbarConfig } from "./toolbar.js";export const OPEN_SOURCE_LICENSE_KEY = "GPL";@@ -163,7 +164,10 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfislashCommand: {removeCommands: [],dropdownLimit: Number.MAX_SAFE_INTEGER,- extraCommands: buildExtraCommands()+ extraCommands: buildExtraCommands(SAMPLE_DIAGRAMS, {+ title: (name) => t("mermaid.slash_command_title", { name }),+ description: (name) => t("mermaid.slash_command_description", { name })+ })},template: {definitions: opts.templatesdiff --git a/packages/ckeditor5-mermaid/.gitignore b/packages/ckeditor5-mermaid/.gitignoreindex f5540c913e..e7b2bce4e5 100644--- a/packages/ckeditor5-mermaid/.gitignore+++ b/packages/ckeditor5-mermaid/.gitignore@@ -10,4 +10,5 @@ src/**/*.jssrc/**/*.d.ts.vitest-attachments-tests/__screenshots__+# Vitest browser-mode writes screenshots under each test dir (e.g. tests/commands/__screenshots__).+**/__screenshots__/diff --git a/packages/ckeditor5-mermaid/src/commands/insertMermaidCommand.ts b/packages/ckeditor5-mermaid/src/commands/insertMermaidCommand.tsindex 256f0a0b91..7e5c7ea176 100644--- a/packages/ckeditor5-mermaid/src/commands/insertMermaidCommand.ts+++ b/packages/ckeditor5-mermaid/src/commands/insertMermaidCommand.ts@@ -1,5 +1,8 @@import { Command } from "ckeditor5";+/** The name under which {@link InsertMermaidCommand} is registered in the editor. */+export const INSERT_MERMAID_COMMAND = "insertMermaidCommand";+const MOCK_MERMAID_MARKUP = `flowchart TBA --> BB --> C`;@@ -22,15 +25,15 @@ export default class InsertMermaidCommand extends Command {}}- override execute() {+ override execute( options: { source?: string; displayMode?: string } = {} ) {const editor = this.editor;const model = editor.model;let mermaidItem;model.change( writer => {mermaidItem = writer.createElement( 'mermaid', {- displayMode: 'split',- source: MOCK_MERMAID_MARKUP+ displayMode: options.displayMode ?? 'split',+ source: options.source ?? MOCK_MERMAID_MARKUP} );model.insertContent( mermaidItem );diff --git a/packages/ckeditor5-mermaid/src/index.ts b/packages/ckeditor5-mermaid/src/index.tsindex d4f750bc8c..6d52b31ae7 100644--- a/packages/ckeditor5-mermaid/src/index.ts+++ b/packages/ckeditor5-mermaid/src/index.ts@@ -1,6 +1,7 @@import './augmentation.js';export { default as Mermaid } from './mermaid.js';+export { INSERT_MERMAID_COMMAND } from './commands/insertMermaidCommand.js';import infoIcon from './../theme/icons/info.svg?raw';import insertMermaidIcon from './../theme/icons/insert.svg?raw';import previewModeIcon from './../theme/icons/preview-mode.svg?raw';diff --git a/packages/ckeditor5-mermaid/src/mermaidediting.ts b/packages/ckeditor5-mermaid/src/mermaidediting.tsindex d142934099..31a97a2a4e 100644--- a/packages/ckeditor5-mermaid/src/mermaidediting.ts+++ b/packages/ckeditor5-mermaid/src/mermaidediting.ts@@ -5,7 +5,7 @@import MermaidPreviewCommand from './commands/mermaidPreviewCommand.js';import MermaidSourceViewCommand from './commands/mermaidSourceViewCommand.js';import MermaidSplitViewCommand from './commands/mermaidSplitViewCommand.js';-import InsertMermaidCommand from './commands/insertMermaidCommand.js';+import InsertMermaidCommand, { INSERT_MERMAID_COMMAND } from './commands/insertMermaidCommand.js';import { DowncastAttributeEvent, DowncastConversionApi, EditorConfig, ModelElement, EventInfo, ModelItem, ModelNode, Plugin, toWidget, uid, UpcastConversionApi, UpcastConversionData, ViewElement, ViewText, ViewUIElement } from 'ckeditor5';import { debounce } from './utils.js';@@ -59,7 +59,7 @@ export default class MermaidEditing extends Plugin {editor.commands.add( 'mermaidPreviewCommand', new MermaidPreviewCommand( editor ) );editor.commands.add( 'mermaidSplitViewCommand', new MermaidSplitViewCommand( editor ) );editor.commands.add( 'mermaidSourceViewCommand', new MermaidSourceViewCommand( editor ) );- editor.commands.add( 'insertMermaidCommand', new InsertMermaidCommand( editor ) );+ editor.commands.add( INSERT_MERMAID_COMMAND, new InsertMermaidCommand( editor ) );}/**diff --git a/packages/ckeditor5-mermaid/tests/commands/insertMermaidCommand.ts b/packages/ckeditor5-mermaid/tests/commands/insertMermaidCommand.tsindex 31e6b17c6e..f6fc1bd24d 100644--- a/packages/ckeditor5-mermaid/tests/commands/insertMermaidCommand.ts+++ b/packages/ckeditor5-mermaid/tests/commands/insertMermaidCommand.ts@@ -75,6 +75,18 @@ describe( 'InsertMermaidCommand', () => {} );describe( 'execute()', () => {+ it( 'should add mermaid with the provided source', () => {+ setModelData( model,+ '<paragraph>[foo]</paragraph>'+ );++ command.execute( { source: 'graph LR; X --> Y' } );++ expect( getModelData( model, { withoutSelection: true } ) ).to.equal(+ '<mermaid displayMode="split" source="graph LR; X --> Y"></mermaid>'+ );+ } );+it( 'should add sample mermaid', () => {setModelData( model,'<paragraph>[foo]</paragraph>'diff --git a/packages/ckeditor5/src/extra_slash_commands.ts b/packages/ckeditor5/src/extra_slash_commands.tsindex ec34574648..f787152c8a 100644--- a/packages/ckeditor5/src/extra_slash_commands.ts+++ b/packages/ckeditor5/src/extra_slash_commands.ts@@ -18,15 +18,45 @@ import internalLinkIcon from './icons/trilium.svg?raw';import noteIcon from './icons/note.svg?raw';import importMarkdownIcon from './icons/markdown-mark.svg?raw';import { icons as mathIcons, MathUI } from '@triliumnext/ckeditor5-math';+import { INSERT_MERMAID_COMMAND } from '@triliumnext/ckeditor5-mermaid';import { BookmarkUI } from "ckeditor5";import bxBookmark from "boxicons/svg/regular/bx-bookmark.svg?raw";+import bxNetworkChart from "boxicons/svg/regular/bx-network-chart.svg?raw";type SlashCommandDefinition = SlashCommandEditorConfig["extraCommands"][number];-export default function buildExtraCommands(): SlashCommandDefinition[] {+/**+ * A Mermaid sample diagram (name + source). Mirrors the client's+ * `NoteContentTemplate`; redeclared here because this package cannot import+ * from the client app.+ */+export interface MermaidSampleTemplate {+ name: string;+ content: string;+}++/**+ * Localizable labels for the `/Mermaid Diagram: …` sample commands. Supplied by+ * the client (which owns i18n); the English defaults keep this package standalone.+ */+export interface MermaidSampleLabels {+ title: (name: string) => string;+ description: (name: string) => string;+}++const DEFAULT_MERMAID_LABELS: MermaidSampleLabels = {+ title: (name) => `Mermaid Diagram: ${name}`,+ description: (name) => `Insert a "${name}" Mermaid diagram template`+};++export default function buildExtraCommands(+ mermaidSamples: MermaidSampleTemplate[] = [],+ mermaidLabels: MermaidSampleLabels = DEFAULT_MERMAID_LABELS+): SlashCommandDefinition[] {return [...buildAlignmentExtraCommands(),...buildAdmonitionExtraCommands(),+ ...buildMermaidSampleCommands(mermaidSamples, mermaidLabels),{id: "collapsible",title: "Collapsible block",@@ -102,6 +132,18 @@ export default function buildExtraCommands(): SlashCommandDefinition[] {];}+function buildMermaidSampleCommands(samples: MermaidSampleTemplate[], labels: MermaidSampleLabels): SlashCommandDefinition[] {+ return samples.map((sample, index) => ({+ id: `mermaid-sample-${index}`,+ title: labels.title(sample.name),+ description: labels.description(sample.name),+ aliases: [ "mermaid", "diagram", sample.name ],+ icon: bxNetworkChart,+ // Inserts a mermaid block pre-filled with the sample source (see insertMermaidCommand).+ execute: (editor: Editor) => editor.execute(INSERT_MERMAID_COMMAND, { source: sample.content })+ }));+}+function buildAlignmentExtraCommands(): SlashCommandDefinition[] {return [{