mirror of
https://github.com/zadam/trilium.git
synced 2025-11-05 04:45:47 +01:00
test(server/note_map): clipper processing notes & images
This commit is contained in:
50
apps/server/src/routes/api/clipper.spec.ts
Normal file
50
apps/server/src/routes/api/clipper.spec.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { BNote } from "../../services/backend_script_entrypoint";
|
||||||
|
import { buildNote } from "../../test/becca_easy_mocking";
|
||||||
|
import { processContent } from "./clipper";
|
||||||
|
|
||||||
|
let note!: BNote;
|
||||||
|
|
||||||
|
describe("processContent", () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
note = buildNote({});
|
||||||
|
note.saveAttachment = () => {};
|
||||||
|
vi.mock("../../services/image.js", () => ({
|
||||||
|
default: {
|
||||||
|
saveImageToAttachment() {
|
||||||
|
return {
|
||||||
|
attachmentId: "foo",
|
||||||
|
title: "encodedTitle",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("processes basic note", () => {
|
||||||
|
const processed = processContent([], note, "<p>Hello world.</p>");
|
||||||
|
expect(processed).toStrictEqual("<p>Hello world.</p>")
|
||||||
|
});
|
||||||
|
|
||||||
|
it("processes plain text", () => {
|
||||||
|
const processed = processContent([], note, "Hello world.");
|
||||||
|
expect(processed).toStrictEqual("<p>Hello world.</p>")
|
||||||
|
});
|
||||||
|
|
||||||
|
it("replaces images", () => {
|
||||||
|
const processed = processContent(
|
||||||
|
[{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAF0lEQVQoU2P8DwQMBADjqKLRIGAgKggAzHs/0SoYCGwAAAAASUVORK5CYII="}],
|
||||||
|
note, `<img src="OKZxZA3MonZJkwFcEhId">`
|
||||||
|
);
|
||||||
|
expect(processed).toStrictEqual(`<img src="api/attachments/foo/image/encodedTitle">`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips over non-data images", () => {
|
||||||
|
for (const url of [ "foo", "" ]) {
|
||||||
|
const processed = processContent(
|
||||||
|
[{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl": url}],
|
||||||
|
note, `<img src="OKZxZA3MonZJkwFcEhId">`
|
||||||
|
);
|
||||||
|
expect(processed).toStrictEqual(`<img src="OKZxZA3MonZJkwFcEhId">`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -147,7 +147,7 @@ async function createNote(req: Request) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function processContent(images: Image[], note: BNote, content: string) {
|
export function processContent(images: Image[], note: BNote, content: string) {
|
||||||
let rewrittenContent = htmlSanitizer.sanitize(content);
|
let rewrittenContent = htmlSanitizer.sanitize(content);
|
||||||
|
|
||||||
if (images) {
|
if (images) {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ type RelationDefinitions = { [key in `~${string}`]: string; };
|
|||||||
|
|
||||||
interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
|
interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
|
||||||
id?: string | undefined;
|
id?: string | undefined;
|
||||||
title: string;
|
title?: string;
|
||||||
content?: string;
|
content?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ export function buildNotes(notes: NoteDefinition[]) {
|
|||||||
export function buildNote(noteDef: NoteDefinition) {
|
export function buildNote(noteDef: NoteDefinition) {
|
||||||
const note = new BNote({
|
const note = new BNote({
|
||||||
noteId: noteDef.id ?? utils.randomString(12),
|
noteId: noteDef.id ?? utils.randomString(12),
|
||||||
title: noteDef.title,
|
title: noteDef.title ?? "New note",
|
||||||
type: "text",
|
type: "text",
|
||||||
mime: "text/html",
|
mime: "text/html",
|
||||||
isProtected: false,
|
isProtected: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user