mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
refactor(import/zip): extract method and test it
This commit is contained in:
parent
2a69a98dd3
commit
f32b76d047
@ -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);
|
||||
});
|
||||
});
|
||||
|
@ -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
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user