refactor(import/zip): extract method and test it

This commit is contained in:
Elian Doran 2025-03-31 00:27:22 +03:00
parent 2a69a98dd3
commit f32b76d047
No known key found for this signature in database
2 changed files with 33 additions and 10 deletions

View File

@ -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("<div");
});
});
describe("removeTriliumTags", () => {
it("removes <h1> tags from HTML", () => {
const output = removeTriliumTags(trimIndentation`\
<h1 data-trilium-h1>21 - Thursday</h1>
<p>Hello world</p>
`);
const expected = `\n<p>Hello world</p>\n`;
expect(output).toEqual(expected);
});
it("removes <title> tags from HTML", () => {
const output = removeTriliumTags(trimIndentation`\
<title data-trilium-title>21 - Thursday</title>
<p>Hello world</p>
`);
const expected = `\n<p>Hello world</p>\n`;
expect(output).toEqual(expected);
});
});

View File

@ -385,15 +385,6 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo
return content;
}
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;
}
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
};