Skip to content

Build your personal knowledge base with Trilium Notes

feat(image): allow viewing source of SVG images

Elian Doranecdcaa748db2
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.ts
index de57f4a556..21dbb12bc7 100644
--- a/apps/client/src/entities/fnote.ts
+++ b/apps/client/src/entities/fnote.ts
@@ -1078,6 +1078,10 @@ export default class FNote {
return this.mime.startsWith("application/javascript");
}
+ isSvg() {
+ return this.mime === "image/svg+xml";
+ }
+
/**
* Provides note's date metadata.
*/
diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsx
index a841001aa3..ea1b2a0be0 100644
--- a/apps/client/src/widgets/ribbon/NoteActions.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx
@@ -93,7 +93,7 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote
);
const isElectron = getIsElectron();
const isMac = getIsMac();
- const hasSource = ["text", "code", "relationMap", "mermaid", "canvas", "mindMap", "spreadsheet", "llmChat"].includes(noteType);
+ const hasSource = ["text", "code", "relationMap", "mermaid", "canvas", "mindMap", "spreadsheet", "llmChat"].includes(noteType) || note.isSvg();
const isSearchOrBook = ["search", "book"].includes(noteType);
const isHelpPage = note.noteId.startsWith("_help");
const [syncServerHost] = useTriliumOption("syncServerHost");
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index 9071ec47f5..a7485b76ed 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -6,7 +6,7 @@ import { highlightSelectionMatches } from "@codemirror/search";
import { autocompletion, type CompletionSource } from "@codemirror/autocomplete";
import { vim } from "@replit/codemirror-vim";
import { indentationMarkers } from "@replit/codemirror-indentation-markers";
-import byMimeType from "./syntax_highlighting.js";
+import byMimeType, { MIME_ALIASES } from "./syntax_highlighting.js";
import smartIndentWithTab from "./extensions/custom_tab.js";
import type { ThemeDefinition } from "./color_themes.js";
import { createSearchHighlighter, SearchHighlighter, searchMatchHighlightTheme } from "./find_replace.js";
@@ -318,7 +318,7 @@ export default class CodeMirror extends EditorView {
this.currentMime = mime;
let newExtension: Extension[] = [];
- const correspondingSyntax = byMimeType[mime];
+ const correspondingSyntax = byMimeType[MIME_ALIASES[mime] ?? mime];
if (correspondingSyntax) {
const resolvedSyntax = await correspondingSyntax();
diff --git a/packages/codemirror/src/syntax_highlighting.ts b/packages/codemirror/src/syntax_highlighting.ts
index 9c90457b66..94968c2213 100644
--- a/packages/codemirror/src/syntax_highlighting.ts
+++ b/packages/codemirror/src/syntax_highlighting.ts
@@ -199,4 +199,13 @@ const byMimeType: Record<SupportedMimeTypes, (() => Promise<StreamParser<unknown
"text/xml": async () => (await import('@codemirror/lang-xml')).xml()
}
+/**
+ * Maps MIME types that aren't first-class code-note languages onto an equivalent
+ * {@link byMimeType} language for syntax highlighting. For example, SVG image notes
+ * (`image/svg+xml`) are XML under the hood, so their source view highlights as XML.
+ */
+export const MIME_ALIASES: Record<string, SupportedMimeTypes> = {
+ "image/svg+xml": "text/xml"
+};
+
export default byMimeType;