Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

feat(markdown): improve mermaid diagrams syntax by allowing standard version (#717)

Sairam Polisettyf5c1e1ed4b2a
diff --git a/frontend/app/components/MarkdownEditor/modules/mermaidHighlight.ts b/frontend/app/components/MarkdownEditor/modules/mermaidHighlight.ts
index fdaf153e..5338fbce 100644
--- a/frontend/app/components/MarkdownEditor/modules/mermaidHighlight.ts
+++ b/frontend/app/components/MarkdownEditor/modules/mermaidHighlight.ts
@@ -1,6 +1,6 @@
import { ViewPlugin, Decoration, type DecorationSet, EditorView } from '@codemirror/view';
import { RangeSetBuilder, type Text } from '@codemirror/state';
-import { MERMAID_OPEN_RE, MERMAID_CLOSE_RE } from '~/helpers/markdown/mermaid';
+import { MERMAID_OPEN_RE } from '~/helpers/markdown/mermaid';
// Main diagram-type declarations plus the handful of structural keywords that show up
// across most diagram types. Intentionally not exhaustive (e.g. gantt/pie-specific
@@ -12,14 +12,26 @@ const MERMAID_ARROW_RE = /-->|--x|--o|-\.->|==>|---|<-->/g;
function findMermaidBodyLineRanges(doc: Text): { startLine: number; endLine: number }[] {
const ranges: { startLine: number; endLine: number }[] = [];
let openLine: number | null = null;
+ let currentMarkerChar: string | null = null;
+ let currentMarkerLen: number = 0;
for (let i = 1; i <= doc.lines; i++) {
const text = doc.line(i).text;
if (openLine === null) {
- if (MERMAID_OPEN_RE.test(text)) openLine = i + 1;
- } else if (MERMAID_CLOSE_RE.test(text)) {
- if (i - 1 >= openLine) ranges.push({ startLine: openLine, endLine: i - 1 });
- openLine = null;
+ const match = text.match(MERMAID_OPEN_RE);
+ if (match) {
+ openLine = i + 1;
+ currentMarkerChar = match[1]!.charAt(0);
+ currentMarkerLen = match[1]!.length;
+ }
+ } else {
+ const trimmed = text.trim();
+ if (trimmed.length >= currentMarkerLen && trimmed.split('').every(c => c === currentMarkerChar)) {
+ if (i - 1 >= openLine) ranges.push({ startLine: openLine, endLine: i - 1 });
+ openLine = null;
+ currentMarkerChar = null;
+ currentMarkerLen = 0;
+ }
}
}
if (openLine !== null && openLine <= doc.lines) ranges.push({ startLine: openLine, endLine: doc.lines });
diff --git a/frontend/app/components/MarkdownSyntax.vue b/frontend/app/components/MarkdownSyntax.vue
index 66076edc..c7176dbe 100644
--- a/frontend/app/components/MarkdownSyntax.vue
+++ b/frontend/app/components/MarkdownSyntax.vue
@@ -217,7 +217,7 @@ Useful for side content or notes.
<!-- DIAGRAMS -->
<h2><Icon name="format/diagrams" fill="var(--primary)" /> {{ t('markdown.syntax.diagrams') }}</h2>
<p>
- Render live diagrams with <a href="https://mermaid.js.org" target="_blank" rel="noopener">Mermaid</a> syntax inside a <code>:::mermaid</code> block.
+ Render live diagrams with <a href="https://mermaid.js.org" target="_blank" rel="noopener">Mermaid</a> syntax inside a <code>:::mermaid</code> or <code>```mermaid</code> block.
</p>
<div class="demo">
<pre>
diff --git a/frontend/app/helpers/markdown/mermaid.ts b/frontend/app/helpers/markdown/mermaid.ts
index e18379bd..5aaf25b0 100644
--- a/frontend/app/helpers/markdown/mermaid.ts
+++ b/frontend/app/helpers/markdown/mermaid.ts
@@ -1,7 +1,6 @@
import type { MarkdownIt } from 'markdown-it';
-export const MERMAID_OPEN_RE = /^:{3,}mermaid\s*$/;
-export const MERMAID_CLOSE_RE = /^:{3,}\s*$/;
+export const MERMAID_OPEN_RE = /^(:{3,}|`{3,})mermaid\s*$/;
// Raw-capture block rule (like `fence`) rather than markdown-it-container, so diagram
// source is never fed through the nested block/inline parser: indentation, [labels],
@@ -13,7 +12,11 @@ export function mermaidPlugin(md: MarkdownIt) {
(state, startLine, endLine, silent) => {
const startPos = state.bMarks[startLine]! + state.tShift[startLine]!;
const startMax = state.eMarks[startLine]!;
- if (!MERMAID_OPEN_RE.test(state.src.slice(startPos, startMax))) return false;
+ const match = state.src.slice(startPos, startMax).match(MERMAID_OPEN_RE);
+ if (!match) return false;
+
+ const markerChar = match[1]![0];
+ const minMarkerLen = match[1]!.length;
let nextLine = startLine;
let haveEndMarker = false;
@@ -28,7 +31,8 @@ export function mermaidPlugin(md: MarkdownIt) {
if (pos < max && state.sCount[nextLine]! < state.blkIndent) break; // de-indented, stop
if (state.sCount[nextLine]! - state.blkIndent >= 4) continue; // closing marker can't be indented
- if (MERMAID_CLOSE_RE.test(state.src.slice(pos, max))) {
+ const lineText = state.src.slice(pos, max).trim();
+ if (lineText.length >= minMarkerLen && lineText.split('').every(c => c === markerChar)) {
haveEndMarker = true;
break;
}