Skip to content

Build your personal knowledge base with Trilium Notes

fix(import/mime): importing a Markdown file with conversion to text disabled is treated as file instead of code

Elian Doran75b4877c874b
diff --git a/apps/server/src/services/import/mime.spec.ts b/apps/server/src/services/import/mime.spec.ts
index 223517d320..9c9fe825b9 100644
--- a/apps/server/src/services/import/mime.spec.ts
+++ b/apps/server/src/services/import/mime.spec.ts
@@ -123,6 +123,16 @@ describe("#getType", () => {
[{textImportedAsText: false}, "text/x-markdown"], "file"
],
+ [
+ "w/ codeImportedAsCode: true and 'text/markdown' mime type (override) – it should return 'code'",
+ [{codeImportedAsCode: true}, "text/markdown"], "code"
+ ],
+
+ [
+ "w/ codeImportedAsCode: true and 'application/javascript' mime type (override) – it should return 'code'",
+ [{codeImportedAsCode: true}, "application/javascript"], "code"
+ ],
+
[
"w/ textImportedAsText: false and 'text/html' mime type – it should return 'file'",
[{textImportedAsText: false}, "text/html"], "file"
diff --git a/apps/server/src/services/import/mime.ts b/apps/server/src/services/import/mime.ts
index b25e98926d..7a54a1f5a1 100644
--- a/apps/server/src/services/import/mime.ts
+++ b/apps/server/src/services/import/mime.ts
@@ -97,7 +97,7 @@ function getType(options: TaskData<"importNotes">, mime: string): NoteType {
case options?.textImportedAsText && ["text/html", "text/markdown", "text/x-markdown", "text/mdx"].includes(mimeLc):
return "text";
- case options?.codeImportedAsCode && CODE_MIME_TYPES.has(mimeLc):
+ case options?.codeImportedAsCode && (CODE_MIME_TYPES.has(mimeLc) || CODE_MIME_TYPES_OVERRIDE.has(mimeLc)):
return "code";
case mime.startsWith("image/"):
diff --git a/apps/server/src/services/import/zip.ts b/apps/server/src/services/import/zip.ts
index 5d9e00d8ab..e4a0dfbd98 100644
--- a/apps/server/src/services/import/zip.ts
+++ b/apps/server/src/services/import/zip.ts
@@ -175,8 +175,12 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Bu
}
function detectFileTypeAndMime(taskContext: TaskContext<"importNotes">, filePath: string) {
- const mime = mimeService.getMime(filePath) || "application/octet-stream";
- const type = mimeService.getType(taskContext.data || {}, mime);
+ const rawMime = mimeService.getMime(filePath) || "application/octet-stream";
+ const type = mimeService.getType(taskContext.data || {}, rawMime);
+ // Normalize aliased code MIMEs (e.g. `text/markdown` → `text/x-markdown`,
+ // `application/javascript` → `application/javascript;env=frontend`) so the
+ // stored MIME matches what the rest of the app expects.
+ const mime = (type === "code" && mimeService.normalizeMimeType(rawMime)) || rawMime;
return { mime, type };
}