Skip to content

Build your personal knowledge base with Trilium Notes

feat(mermaid): persist the diagram display mode in the note content

Elian Doranf51bdf6ce406
diff --git a/packages/ckeditor5/src/plugins/mermaid/README.md b/packages/ckeditor5/src/plugins/mermaid/README.md
index 3a9c2354a2..9664176c3a 100644
--- a/packages/ckeditor5/src/plugins/mermaid/README.md
+++ b/packages/ckeditor5/src/plugins/mermaid/README.md
@@ -14,6 +14,14 @@ or both side by side. Load the `Mermaid` glue plugin; it pulls in `MermaidEditin
| `mermaid_{preview,source_view,split_view}_command.ts` | Switch the widget's `displayMode` |
| `utils.ts` | `debounce` for the source textarea, `checkIsOn` for button state |
+## The display mode is part of the content
+
+A diagram round-trips as `<pre spellcheck="false"><code class="language-mermaid">…</code></pre>`, with
+the picked mode persisted on the `<code>` as `data-trilium-display-mode` — the same treatment
+collapsed list items get with `data-trilium-collapsed`. The default (`split`) is left out, so a
+diagram nobody switched keeps producing exactly the content it did before; a missing or unknown
+value upcasts back to `split`.
+
## The mermaid library is not a dependency
The plugin never imports mermaid. The host passes it in through editor config:
diff --git a/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.spec.ts b/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.spec.ts
index 1abec0823d..d860593f4d 100644
--- a/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.spec.ts
+++ b/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.spec.ts
@@ -84,6 +84,36 @@ describe( 'MermaidEditing', () => {
'<mermaid displayMode="split" source=""></mermaid>'
);
} );
+
+ it( 'restores the persisted display mode', () => {
+ for ( const mode of [ 'source', 'preview' ] ) {
+ editor.setData(
+ '<pre spellcheck="false">' +
+ `<code class="language-mermaid" data-trilium-display-mode="${ mode }">flowchart TB</code>` +
+ '</pre>'
+ );
+
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
+ `<mermaid displayMode="${ mode }" source="flowchart TB"></mermaid>`
+ );
+ }
+ } );
+
+ it( 'falls back to the split mode for a missing or unknown display mode', () => {
+ // A diagram saved before the mode was persisted, and one carrying a mode this
+ // version has no widget state for (hand-edited or imported content).
+ for ( const attribute of [ '', ' data-trilium-display-mode="nonsense"' ] ) {
+ editor.setData(
+ '<pre spellcheck="false">' +
+ `<code class="language-mermaid"${ attribute }>flowchart TB</code>` +
+ '</pre>'
+ );
+
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
+ '<mermaid displayMode="split" source="flowchart TB"></mermaid>'
+ );
+ }
+ } );
} );
describe( 'data downcast', () => {
@@ -116,6 +146,48 @@ describe( 'MermaidEditing', () => {
'</pre>'
);
} );
+
+ it( 'persists a non-default display mode, and only that', () => {
+ // The default (split) stays out of the data so untouched diagrams keep
+ // producing the very same content.
+ setModelData( editor.model, '<mermaid displayMode="split" source="flowchart TB"></mermaid>' );
+
+ expect( editor.getData() ).to.equal(
+ '<pre spellcheck="false"><code class="language-mermaid">flowchart TB</code></pre>'
+ );
+
+ for ( const mode of [ 'source', 'preview' ] ) {
+ setModelData( editor.model, `<mermaid displayMode="${ mode }" source="flowchart TB"></mermaid>` );
+
+ expect( editor.getData() ).to.equal(
+ '<pre spellcheck="false">' +
+ `<code class="language-mermaid" data-trilium-display-mode="${ mode }">flowchart TB</code>` +
+ '</pre>'
+ );
+ }
+ } );
+
+ it( 'round-trips the mode a display-mode command switched to', () => {
+ editor.setData(
+ '<pre spellcheck="false">' +
+ '<code class="language-mermaid">flowchart TB</code>' +
+ '</pre>'
+ );
+
+ editor.execute( 'mermaidPreviewCommand' );
+
+ expect( editor.getData() ).to.equal(
+ '<pre spellcheck="false">' +
+ '<code class="language-mermaid" data-trilium-display-mode="preview">flowchart TB</code>' +
+ '</pre>'
+ );
+
+ editor.setData( editor.getData() );
+
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
+ '<mermaid displayMode="preview" source="flowchart TB"></mermaid>'
+ );
+ } );
} );
describe( 'editing downcast', () => {
diff --git a/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.ts b/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.ts
index ceb974fac9..e427c5e90b 100644
--- a/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.ts
+++ b/packages/ckeditor5/src/plugins/mermaid/mermaid_editing.ts
@@ -17,6 +17,22 @@ const DEBOUNCE_TIME = 300;
type DowncastConversionData = DowncastAttributeEvent["args"][0];
+/** The modes the widget can be shown in, i.e. the values of its `displayMode` model attribute. */
+const DISPLAY_MODES = [ 'source', 'split', 'preview' ] as const;
+
+type MermaidDisplayMode = typeof DISPLAY_MODES[ number ];
+
+const DEFAULT_DISPLAY_MODE: MermaidDisplayMode = 'split';
+
+/**
+ * Carries the mode the user picked into the note content, the way collapsed list items persist
+ * `data-trilium-collapsed`. It is written on the `<code>` element the widget already round-trips
+ * through, and only for a non-default mode — so a diagram left in split view keeps producing
+ * byte-identical content, and every consumer that keys off `code.language-mermaid` (the share
+ * view, markdown export, the importers) is unaffected.
+ */
+const DISPLAY_MODE_DATA_ATTRIBUTE = 'data-trilium-display-mode';
+
export default class MermaidEditing extends Plugin {
private _config!: EditorConfig["mermaid"];
@@ -97,11 +113,18 @@ export default class MermaidEditing extends Plugin {
}
const targetViewPosition = mapper.toViewPosition( model.createPositionBefore( data.item as ModelItem ) );
+ const displayMode = readDisplayMode( data.item.getAttribute( 'displayMode' ) );
// For downcast we're using only language-mermaid class. We don't set class to `mermaid language-mermaid` as
// multiple markdown converters that we have seen are using only `language-mermaid` class and not `mermaid` alone.
- const code = writer.createContainerElement( 'code', {
+ const codeAttributes: Record<string, string> = {
class: 'language-mermaid'
- } ) as any;
+ };
+
+ if ( displayMode !== DEFAULT_DISPLAY_MODE ) {
+ codeAttributes[ DISPLAY_MODE_DATA_ATTRIBUTE ] = displayMode;
+ }
+
+ const code = writer.createContainerElement( 'code', codeAttributes ) as any;
const pre = writer.createContainerElement( 'pre', {
spellcheck: 'false'
} ) as any;
@@ -241,7 +264,7 @@ export default class MermaidEditing extends Plugin {
const mermaidElement = writer.createElement( 'mermaid', {
source: mermaidSource,
- displayMode: 'split'
+ displayMode: readDisplayMode( viewCodeElement.getAttribute( DISPLAY_MODE_DATA_ATTRIBUTE ) )
} );
// Let's try to insert mermaid element.
@@ -288,3 +311,12 @@ export default class MermaidEditing extends Plugin {
}
}
}
+
+/**
+ * Narrows a persisted (or model-held) display mode to a known one, falling back to the default.
+ * Content can carry anything: an older note has no attribute at all, and a hand-edited or
+ * imported one can carry a value the widget has no mode for.
+ */
+function readDisplayMode( value: unknown ): MermaidDisplayMode {
+ return DISPLAY_MODES.includes( value as MermaidDisplayMode ) ? value as MermaidDisplayMode : DEFAULT_DISPLAY_MODE;
+}