Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): withhold a credential-protected subpage's description from its card

Elian Doran8b56ba8d1772
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.ts
index d71b281c5c..cb4d4816bb 100644
--- a/packages/share-theme/src/model/page.spec.ts
+++ b/packages/share-theme/src/model/page.spec.ts
@@ -526,6 +526,15 @@ describe("getChildLinks", () => {
]);
expect(getChildLinks(parent, options).map((link) => link.excerpt))
.toStrictEqual([ "About it", null, null, null, null, null, null ]);
+
+ const denied = getChildLinks(parent, {
+ ...options,
+ getText: (note) => texts[note.noteId],
+ canAccess: (note) => note.noteId !== "described" && note.noteId !== "short"
+ });
+ expect(denied.map((link) => link.excerpt).slice(0, 3))
+ .toStrictEqual([ null, null, "First sentence of the note, which says what it is about. "
+ + "A second one goes on about it at length, well past where a summary should stop." ]);
});
});
diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.ts
index b7bf958ef3..da75a86980 100644
--- a/packages/share-theme/src/model/page.ts
+++ b/packages/share-theme/src/model/page.ts
@@ -411,6 +411,11 @@ export interface ChildLinksOptions {
* visitor is not allowed to read. Without it, only `#shareDescription` describes a child.
*/
getText?: (note: ShareNote) => string | null;
+ /**
+ * Whether the visitor is allowed to read a child. A child they are not allowed to read has no
+ * excerpt, not even its `#shareDescription`.
+ */
+ canAccess?: (note: ShareNote) => boolean;
}
/**
@@ -424,8 +429,8 @@ export function getChildLinks(note: ShareNote, options: ChildLinksOptions): Chil
}
return note.getVisibleChildNotes().map((child) => {
- const excerpt = readLabel(child, "shareDescription")
- ?? toExcerpt(options.getText?.(child) ?? "");
+ const excerpt = options.canAccess?.(child) === false ? null
+ : readLabel(child, "shareDescription") ?? toExcerpt(options.getText?.(child) ?? "");
const children = excerpt ? [] : child.getVisibleChildNotes().slice(0, CHILD_PREVIEW_LENGTH);
return {
...getShareLink(child, options.sanitizeUrl),
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 96d33f7dcc..ce0e368ced 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -503,7 +503,12 @@ describe("content_renderer", () => {
]
},
{ id: "locked", content: "<p>Secret</p>", isProtected: true },
- { "id": "guarded", "content": "<p>Guarded</p>", "#shareCredentials": "u:p" },
+ {
+ "id": "guarded",
+ "content": "<p>Guarded</p>",
+ "#shareCredentials": "u:p",
+ "#shareDescription": "Guarded description"
+ },
{ id: "drawing", type: "canvas", content: "{}" },
{ id: "bytes", content: Buffer.from("<p>Bytes</p>") }
]
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index c7e5637bc9..aa968f5e26 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -255,7 +255,9 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)
const childLinks = getChildLinks(note, {
sanitizeUrl: sanitize.sanitizeUrl,
iconPackPrefixes: renderArgs.iconPackSupportedPrefixes,
- getText: (child) => getExcerptSource(child as SNote | BNote, renderArgs.canAccessEmbed)
+ getText: (child) => getExcerptSource(child as SNote | BNote),
+ // `canAccessEmbed` is only given with a shaca note, whose children are shaca notes too.
+ canAccess: (child) => renderArgs.canAccessEmbed?.(child as SNote) !== false
});
const opts = {
note,
@@ -401,12 +403,11 @@ function getShareAssetPath() {
/**
* Returns the text of the paragraphs of a text note, which the excerpt of its entry in its parent's
- * list of subpages starts from, or `null` for a note of another type, a protected one, or one the
- * caller is not allowed to read. Only the start of the note is parsed, enough for any excerpt.
+ * list of subpages starts from, or `null` for a note of another type or a protected one. Only the
+ * start of the note is parsed, enough for any excerpt.
*/
-function getExcerptSource(note: SNote | BNote, canAccessEmbed?: CanAccessEmbed) {
- // `canAccessEmbed` is only given with a shaca note, whose children are shaca notes too.
- if (note.type !== "text" || note.isProtected || canAccessEmbed?.(note as SNote) === false) {
+function getExcerptSource(note: SNote | BNote) {
+ if (note.type !== "text" || note.isProtected) {
return null;
}