diff --git a/src/services/import/zip.spec.ts b/src/services/import/zip.spec.ts index a93f8b62a..74cb50256 100644 --- a/src/services/import/zip.spec.ts +++ b/src/services/import/zip.spec.ts @@ -3,13 +3,14 @@ import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; import { dirname } from "path"; -import zip from "./zip.js"; +import zip, { removeTriliumTags } 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"; +import { trimIndentation } from "../../../spec/support/utils.js"; const scriptDir = dirname(fileURLToPath(import.meta.url)); async function testImport(fileName: string) { @@ -62,3 +63,23 @@ describe("processNoteContent", () => { expect(htmlNote?.getContent().toString().substring(0, 4)).toEqual(" { + it("removes

tags from HTML", () => { + const output = removeTriliumTags(trimIndentation`\ +

21 - Thursday

+

Hello world

+ `); + const expected = `\n

Hello world

\n`; + expect(output).toEqual(expected); + }); + + it("removes tags from HTML", () => { + const output = removeTriliumTags(trimIndentation`\ + <title data-trilium-title>21 - Thursday +

Hello world

+ `); + const expected = `\n

Hello world

\n`; + expect(output).toEqual(expected); + }); +}); diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index aa64251fc..9180ee3af 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -385,15 +385,6 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo return content; } - function removeTriliumTags(content: string) { - const tagsToRemove = ["

([^<]*)<\/h1>", "([^<]*)<\/title>"]; - for (const tag of tagsToRemove) { - let re = new RegExp(tag, "gi"); - content = content.replace(re, ""); - } - return content; - } - 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", "text/mdx"].includes(mime))) && typeof content === "string") { content = markdownService.renderToHtml(content, noteTitle); @@ -665,6 +656,17 @@ function resolveNoteType(type: string | undefined): NoteType { } } +export function removeTriliumTags(content: string) { + const tagsToRemove = [ + "<h1 data-trilium-h1>([^<]*)<\/h1>", + "<title data-trilium-title>([^<]*)<\/title>"]; + for (const tag of tagsToRemove) { + let re = new RegExp(tag, "gi"); + content = content.replace(re, ""); + } + return content; +} + export default { importZip };