feat(import/single): mermaid with .mermaid extension

This commit is contained in:
Elian Doran 2025-03-22 15:41:56 +02:00
parent 16cbd2f793
commit 3dae771e90
No known key found for this signature in database
6 changed files with 49 additions and 2 deletions

View File

@ -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"

View File

@ -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";
}

View File

@ -0,0 +1,5 @@
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;

View File

@ -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"
});
});
});

View File

@ -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);

View File

@ -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;