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
fix(mermaid-linter): address PR review feedback
Elian Doran34391f58429b
diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.cssindex 2e282b4c60..58cde28989 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css@@ -61,7 +61,7 @@ body.desktop .note-detail-split .note-detail-code-editor {user-select: text;}-.note-detail-error-card pre {+.note-detail-split .note-detail-error-card pre {margin: 0.5em 0 0;padding: 0.5em 0.75em;border-radius: 4px;diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsxindex 3195e48b98..aed0cb25a3 100644--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx@@ -41,6 +41,13 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,const [ error, setError ] = useState<string | null | undefined>();const containerRef = useRef<HTMLDivElement>(null);+ // Reset the render state when switching notes so a previous note's render (and the+ // "showing last valid render" badge) can't briefly carry over to a different note.+ useEffect(() => {+ setSvg(undefined);+ setError(undefined);+ }, [ note.noteId ]);+// Render the SVG.async function onContentChanged(content: string) {try {diff --git a/apps/client/src/widgets/type_widgets/linters/mermaid.ts b/apps/client/src/widgets/type_widgets/linters/mermaid.tsindex 1f7855cb2e..fd4c6335b6 100644--- a/apps/client/src/widgets/type_widgets/linters/mermaid.ts+++ b/apps/client/src/widgets/type_widgets/linters/mermaid.ts@@ -46,12 +46,26 @@ export async function validateMermaid(view: EditorView): Promise<Diagnostic[]> {const { doc } = view.state;const diagnostics = await getMermaidDiagnostics(doc.toString());- return diagnostics.map(({ message, fromLine, fromColumn, toLine, toColumn }) => ({- severity: "error",- message,- from: doc.line(fromLine).from + fromColumn,- to: doc.line(toLine).from + toColumn- }));+ return diagnostics.map(({ message, fromLine, fromColumn, toLine, toColumn }) => {+ // Clamp to the document/line bounds: an out-of-range position reported by a future+ // Mermaid version would otherwise throw from doc.line() and (since this is an async+ // lint source) silently drop every diagnostic.+ const fromLineObj = doc.line(clampLine(fromLine, doc.lines));+ const toLineObj = doc.line(clampLine(toLine, doc.lines));+ const from = Math.min(fromLineObj.from + fromColumn, fromLineObj.to);+ const to = Math.min(toLineObj.from + toColumn, toLineObj.to);++ return {+ severity: "error",+ message,+ from: Math.min(from, to),+ to: Math.max(from, to)+ };+ });+}++function clampLine(line: number, lineCount: number) {+ return Math.max(1, Math.min(line, lineCount));}/**@@ -73,6 +87,11 @@ export async function getMermaidDiagnostics(text: string): Promise<MermaidLineDitry {await mermaid.parse(text);} catch (e: unknown) {+ if (typeof e !== "object" || e === null) {+ // mermaid.parse is expected to throw a structured error object; bail out+ // gracefully if it ever rejects with a primitive or null.+ return [];+ }const hash = (e as MermaidParseError).hash;if (!hash) {// Some diagram types (e.g. the newer Langium-based parsers) throw errors