test(server/share): attachment links

This commit is contained in:
Elian Doran 2025-09-28 15:43:39 +03:00
parent 8b5e53e579
commit 614a8f177c
No known key found for this signature in database
2 changed files with 51 additions and 4 deletions

View File

@ -21,14 +21,37 @@ describe("content_renderer", () => {
Welcome to Trilium Notes!
</strong>
</p>`;
const note = buildShareNote({
title: "Note",
content
});
const note = buildShareNote({ content });
const result = getContent(note);
expect(result.content).toStrictEqual(content);
});
it("handles attachment link", () => {
const content = trimIndentation`\
<h1>Test</h1>
<p>
<a class="reference-link" href="#root/iwTmeWnqBG5Q?viewMode=attachments&amp;attachmentId=q14s2Id7V6pp">
5863845791835102555.mp4
</a>
&nbsp;
</p>
`;
const note = buildShareNote({
content,
attachments: [ { id: "q14s2Id7V6pp" } ]
});
const result = getContent(note);
expect(result.content).toStrictEqual(trimIndentation`\
<h1>Test</h1>
<p>
<a class="reference-link attachment-link role-file" href="api/attachments/q14s2Id7V6pp/download">
5863845791835102555.mp4
</a>
&nbsp;
</p>
`);
});
it("renders included notes", () => {
buildShareNotes([
{ id: "subnote1", content: `<p>Foo</p><div>Bar</div>` },

View File

@ -1,4 +1,5 @@
import utils from "../services/utils.js";
import SAttachment from "../share/shaca/entities/sattachment.js";
import SAttribute from "../share/shaca/entities/sattribute.js";
import SNote from "../share/shaca/entities/snote.js";
import shaca from "../share/shaca/shaca.js";
@ -6,11 +7,19 @@ import shaca from "../share/shaca/shaca.js";
type AttributeDefinitions = { [key in `#${string}`]: string; };
type RelationDefinitions = { [key in `~${string}`]: string; };
interface AttachementDefinition {
id?: string;
role?: string;
mime?: string;
title?: string;
}
interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
id?: string | undefined;
title?: string;
content?: string | Buffer<ArrayBufferLike>;
children?: NoteDefinition[];
attachments?: AttachementDefinition[];
isProtected?: boolean;
}
@ -60,6 +69,21 @@ export function buildShareNote(noteDef: NoteDefinition) {
};
}
// Handle attachments.
if (noteDef.attachments) {
for (const attachmentDef of noteDef.attachments) {
new SAttachment([
attachmentDef.id ?? utils.randomString(12),
note.noteId,
attachmentDef.role ?? "file",
attachmentDef.mime ?? "application/blob",
attachmentDef.title ?? "New attachment",
blobId,
new Date().toUTCString(), // utcDateModified
]);
}
}
// Handle children.
if (noteDef.children) {
for (const childDef of noteDef.children) {