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
feat(import/single): mermaid with .mermaid extension
Elian Doran3dae771e906d
diff --git a/src/services/import/mime.spec.ts b/src/services/import/mime.spec.tsindex 1e32ba3ebe..7281d31c2a 100644--- a/src/services/import/mime.spec.ts+++ b/src/services/import/mime.spec.ts@@ -26,6 +26,11 @@ describe("#getMime", () => {["test.excalidraw"], "application/json"],+ [+ "File extension ('.mermaid') that is defined in EXTENSION_TO_MIME",+ ["test.mermaid"], "text/vnd.mermaid"+ ],+["File extension with inconsistent capitalization that is defined in EXTENSION_TO_MIME",["test.gRoOvY"], "text/x-groovy"diff --git a/src/services/import/mime.ts b/src/services/import/mime.tsindex bab661c632..cb400b8d82 100644--- a/src/services/import/mime.ts+++ b/src/services/import/mime.ts@@ -3,6 +3,7 @@import mimeTypes from "mime-types";import path from "path";import type { TaskData } from "../task_context_interface.js";+import type { NoteType } from "../../becca/entities/rows.js";const CODE_MIME_TYPES = new Set(["application/json",@@ -68,7 +69,8 @@ const EXTENSION_TO_MIME = new Map<string, string>([[".scala", "text/x-scala"],[".swift", "text/x-swift"],[".ts", "text/x-typescript"],- [".excalidraw", "application/json"]+ [".excalidraw", "application/json"],+ [".mermaid", "text/vnd.mermaid"]]);/** @returns false if MIME is not detected */@@ -85,7 +87,7 @@ function getMime(fileName: string) {return mimeFromExt || mimeTypes.lookup(fileNameLc);}-function getType(options: TaskData, mime: string) {+function getType(options: TaskData, mime: string): NoteType {const mimeLc = mime?.toLowerCase();switch (true) {@@ -98,6 +100,9 @@ function getType(options: TaskData, mime: string) {case mime.startsWith("image/"):return "image";+ case mime === "text/vnd.mermaid":+ return "mermaid";+default:return "file";}diff --git a/src/services/import/samples/New note.mermaid b/src/services/import/samples/New note.mermaidnew file mode 100644index 0000000000..577e633599--- /dev/null+++ b/src/services/import/samples/New note.mermaid@@ -0,0 +1,5 @@+graph TD;+ A-->B;+ A-->C;+ B-->D;+ C-->D;\ No newline at end of filediff --git a/src/services/import/single.spec.ts b/src/services/import/single.spec.tsindex 1eacd261bd..060801eb99 100644--- a/src/services/import/single.spec.ts+++ b/src/services/import/single.spec.ts@@ -96,4 +96,13 @@ describe("processNoteContent", () => {expect(importedNote.type).toBe("canvas");expect(importedNote.title).toBe("New note");});++ it("supports mermaid note", async () => {+ const { importedNote } = await testImport("New note.mermaid", "application/json");+ expect(importedNote).toMatchObject({+ mime: "text/vnd.mermaid",+ type: "mermaid",+ title: "New note"+ });+ });});diff --git a/src/services/import/single.ts b/src/services/import/single.tsindex 4105e55e3d..c1597a5620 100644--- a/src/services/import/single.ts+++ b/src/services/import/single.ts@@ -27,6 +27,10 @@ function importSingleFile(taskContext: TaskContext, file: File, parentNote: BNot}}+ if (mime === "text/vnd.mermaid") {+ return importCustomType(taskContext, file, parentNote, "mermaid", mime);+ }+if (taskContext?.data?.codeImportedAsCode && mimeService.getType(taskContext.data, mime) === "code") {return importCodeNote(taskContext, file, parentNote);}@@ -93,6 +97,24 @@ function importCodeNote(taskContext: TaskContext, file: File, parentNote: BNote)return note;}+function importCustomType(taskContext: TaskContext, file: File, parentNote: BNote, type: NoteType, mime: string) {+ const title = getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);+ const content = processStringOrBuffer(file.buffer);++ const { note } = noteService.createNewNote({+ parentNoteId: parentNote.noteId,+ title,+ content,+ type,+ mime: mime,+ isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()+ });++ taskContext.increaseProgressCount();++ return note;+}+function importPlainText(taskContext: TaskContext, file: File, parentNote: BNote) {const title = getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);const plainTextContent = processStringOrBuffer(file.buffer);diff --git a/src/services/utils.ts b/src/services/utils.tsindex 3cb84d3a19..87dcf2d67f 100644--- a/src/services/utils.ts+++ b/src/services/utils.ts@@ -181,6 +181,7 @@ export function removeTextFileExtension(filePath: string) {case ".html":case ".htm":case ".excalidraw":+ case ".mermaid":return filePath.substring(0, filePath.length - extension.length);default:return filePath;