mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-05 07:08:14 +08:00
feat(import/single): mermaid with .mermaid extension
This commit is contained in:
parent
16cbd2f793
commit
3dae771e90
@ -26,6 +26,11 @@ describe("#getMime", () => {
|
|||||||
["test.excalidraw"], "application/json"
|
["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",
|
"File extension with inconsistent capitalization that is defined in EXTENSION_TO_MIME",
|
||||||
["test.gRoOvY"], "text/x-groovy"
|
["test.gRoOvY"], "text/x-groovy"
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import mimeTypes from "mime-types";
|
import mimeTypes from "mime-types";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import type { TaskData } from "../task_context_interface.js";
|
import type { TaskData } from "../task_context_interface.js";
|
||||||
|
import type { NoteType } from "../../becca/entities/rows.js";
|
||||||
|
|
||||||
const CODE_MIME_TYPES = new Set([
|
const CODE_MIME_TYPES = new Set([
|
||||||
"application/json",
|
"application/json",
|
||||||
@ -68,7 +69,8 @@ const EXTENSION_TO_MIME = new Map<string, string>([
|
|||||||
[".scala", "text/x-scala"],
|
[".scala", "text/x-scala"],
|
||||||
[".swift", "text/x-swift"],
|
[".swift", "text/x-swift"],
|
||||||
[".ts", "text/x-typescript"],
|
[".ts", "text/x-typescript"],
|
||||||
[".excalidraw", "application/json"]
|
[".excalidraw", "application/json"],
|
||||||
|
[".mermaid", "text/vnd.mermaid"]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/** @returns false if MIME is not detected */
|
/** @returns false if MIME is not detected */
|
||||||
@ -85,7 +87,7 @@ function getMime(fileName: string) {
|
|||||||
return mimeFromExt || mimeTypes.lookup(fileNameLc);
|
return mimeFromExt || mimeTypes.lookup(fileNameLc);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getType(options: TaskData, mime: string) {
|
function getType(options: TaskData, mime: string): NoteType {
|
||||||
const mimeLc = mime?.toLowerCase();
|
const mimeLc = mime?.toLowerCase();
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
@ -98,6 +100,9 @@ function getType(options: TaskData, mime: string) {
|
|||||||
case mime.startsWith("image/"):
|
case mime.startsWith("image/"):
|
||||||
return "image";
|
return "image";
|
||||||
|
|
||||||
|
case mime === "text/vnd.mermaid":
|
||||||
|
return "mermaid";
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return "file";
|
return "file";
|
||||||
}
|
}
|
||||||
|
5
src/services/import/samples/New note.mermaid
Normal file
5
src/services/import/samples/New note.mermaid
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
graph TD;
|
||||||
|
A-->B;
|
||||||
|
A-->C;
|
||||||
|
B-->D;
|
||||||
|
C-->D;
|
@ -96,4 +96,13 @@ describe("processNoteContent", () => {
|
|||||||
expect(importedNote.type).toBe("canvas");
|
expect(importedNote.type).toBe("canvas");
|
||||||
expect(importedNote.title).toBe("New note");
|
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"
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -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") {
|
if (taskContext?.data?.codeImportedAsCode && mimeService.getType(taskContext.data, mime) === "code") {
|
||||||
return importCodeNote(taskContext, file, parentNote);
|
return importCodeNote(taskContext, file, parentNote);
|
||||||
}
|
}
|
||||||
@ -93,6 +97,24 @@ function importCodeNote(taskContext: TaskContext, file: File, parentNote: BNote)
|
|||||||
return note;
|
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) {
|
function importPlainText(taskContext: TaskContext, file: File, parentNote: BNote) {
|
||||||
const title = getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
const title = getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
||||||
const plainTextContent = processStringOrBuffer(file.buffer);
|
const plainTextContent = processStringOrBuffer(file.buffer);
|
||||||
|
@ -181,6 +181,7 @@ export function removeTextFileExtension(filePath: string) {
|
|||||||
case ".html":
|
case ".html":
|
||||||
case ".htm":
|
case ".htm":
|
||||||
case ".excalidraw":
|
case ".excalidraw":
|
||||||
|
case ".mermaid":
|
||||||
return filePath.substring(0, filePath.length - extension.length);
|
return filePath.substring(0, filePath.length - extension.length);
|
||||||
default:
|
default:
|
||||||
return filePath;
|
return filePath;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user