From f9e4ae72105f857caa1d03a4eb172989d889ac01 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 20 Feb 2025 20:25:42 +0200 Subject: [PATCH] feat(import/zip): treat mdx as markdown (closes #1236) --- package.json | 2 +- src/services/import/mime.ts | 2 +- src/services/import/samples/mdx.zip | Bin 0 -> 336 bytes src/services/import/zip.spec.ts | 45 ++++++++++++++++++++++++++++ src/services/import/zip.ts | 2 +- 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/services/import/samples/mdx.zip create mode 100644 src/services/import/zip.spec.ts diff --git a/package.json b/package.json index 2503ee043..a7e69eea6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/services/import/mime.ts b/src/services/import/mime.ts index 339f3b468..b329f12ac 100644 --- a/src/services/import/mime.ts +++ b/src/services/import/mime.ts @@ -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): diff --git a/src/services/import/samples/mdx.zip b/src/services/import/samples/mdx.zip new file mode 100644 index 0000000000000000000000000000000000000000..cb01c501fda30bfaead5853fd3a328405a32752f GIT binary patch literal 336 zcmWIWW@Zs#-~d9GDIrk|NPw3?fgvQdqC~+jza&*JH>IMqf}4Sn=@U)gm)@2%s($}bvSyHCh1Jerp47!}dX<97e<$L{071@?`x|%)>(+MeDlDOnai_PCnJo9J0w!JWY z{VnqVZ+4C~e6wcu03CV4&pEX0MPkt6aWAK literal 0 HcmV?d00001 diff --git a/src/services/import/zip.spec.ts b/src/services/import/zip.spec.ts new file mode 100644 index 000000000..cd1ea9f5c --- /dev/null +++ b/src/services/import/zip.spec.ts @@ -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((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(); + }); + }); + }); +}) diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index 61534fed3..504f28382 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -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); }