feat(import/zip): treat mdx as markdown (closes #1236)

This commit is contained in:
Elian Doran 2025-02-20 20:25:42 +02:00
parent 1c118f2aa9
commit f9e4ae7210
No known key found for this signature in database
5 changed files with 48 additions and 3 deletions

View File

@ -49,7 +49,7 @@
"build:webpack": "tsx node_modules/webpack/bin/webpack.js -c webpack.config.ts",
"build:prepare-dist": "npm run build:webpack && rimraf ./dist && tsc && tsx ./bin/copy-dist.ts",
"test": "cross-env TRILIUM_DATA_DIR=./integration-tests/db vitest",
"test": "cross-env TRILIUM_DATA_DIR=./integration-tests/db TRILIUM_INTEGRATION_TEST=memory vitest",
"test:coverage": "cross-env TRILIUM_DATA_DIR=./integration-tests/db vitest --coverage",
"test:playwright": "playwright test",

View File

@ -88,7 +88,7 @@ function getType(options: TaskData, mime: string) {
const mimeLc = mime?.toLowerCase();
switch (true) {
case options.textImportedAsText && ["text/html", "text/markdown", "text/x-markdown"].includes(mimeLc):
case options.textImportedAsText && ["text/html", "text/markdown", "text/x-markdown", "text/mdx"].includes(mimeLc):
return "text";
case options.codeImportedAsCode && CODE_MIME_TYPES.has(mimeLc):

Binary file not shown.

View File

@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
import zip from "./zip.js";
import becca from "../../becca/becca.js";
import BNote from "../../becca/entities/bnote.js";
import TaskContext from "../task_context.js";
import cls from "../cls.js";
import sql_init from "../sql_init.js";
import { initializeTranslations } from "../i18n.js";
const scriptDir = dirname(fileURLToPath(import.meta.url));
describe("processNoteContent", () => {
it("treats single MDX as Markdown in ZIP as text note", async () => {
const mdxSample = fs.readFileSync(path.join(scriptDir, "samples", "mdx.zip"));
const taskContext = TaskContext.getInstance("import-mdx", "import", {
textImportedAsText: true
});
await new Promise<void>((resolve, reject) => {
cls.init(async () => {
initializeTranslations();
sql_init.initializeDb();
await sql_init.dbReady;
const rootNote = becca.getNote("root");
if (!rootNote) {
expect(rootNote).toBeTruthy();
return;
}
const importedNote = await zip.importZip(taskContext, mdxSample, rootNote as BNote);
try {
expect(importedNote.mime).toBe("text/mdx");
expect(importedNote.type).toBe("text");
} catch (e) {
reject(e);
}
resolve();
});
});
});
})

View File

@ -386,7 +386,7 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo
}
function processNoteContent(noteMeta: NoteMeta | undefined, type: string, mime: string, content: string | Buffer, noteTitle: string, filePath: string) {
if ((noteMeta?.format === "markdown" || (!noteMeta && taskContext.data?.textImportedAsText && ["text/markdown", "text/x-markdown"].includes(mime))) && typeof content === "string") {
if ((noteMeta?.format === "markdown" || (!noteMeta && taskContext.data?.textImportedAsText && ["text/markdown", "text/x-markdown", "text/mdx"].includes(mime))) && typeof content === "string") {
content = markdownService.renderToHtml(content, noteTitle);
}