Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(share): load the renderer each exported dashboard tile draws with
Elian Doran5e495e6817ee
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.tsindex 16d5348af5..09edb74777 100644--- a/apps/client/src/entities/fnote.ts+++ b/apps/client/src/entities/fnote.ts@@ -1,4 +1,4 @@-import { getNoteIcon, HighlightedTokenInfo } from "@triliumnext/commons";+import { getNoteIcon, HighlightedTokenInfo, isMarkdownMime } from "@triliumnext/commons";import cssClassManager from "../services/css_class_manager.js";import type { Froca } from "../services/froca-interface.js";@@ -1180,7 +1180,7 @@ export default class FNote {}isMarkdown() {- return this.type === "code" && (this.mime === "text/markdown" || this.mime === "text/x-markdown" || this.mime === "text/x-gfm");+ return this.type === "code" && isMarkdownMime(this.mime);}isIconPack() {diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex a4218f16d9..504bd03275 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -1,4 +1,4 @@-import { attachmentIcon } from "@triliumnext/commons";+import { attachmentIcon, CONTENT_RENDERER_GROUPS } from "@triliumnext/commons";import { h, VNode } from "preact";import { beforeEach, describe, expect, it, vi } from "vitest";@@ -138,6 +138,7 @@ import type { SaveState } from "../components/note_context.js";import FAttachment from "../entities/fattachment.js";import { buildNote } from "../test/easy-froca.js";import {+ CONTENT_RENDERERS,disposeInteractiveContent,getEmbedBoxSize,getRenderedContent as rawGetRenderedContent,@@ -183,6 +184,14 @@ beforeEach(() => {(window as any).electronApi = undefined;});+describe("CONTENT_RENDERERS", () => {+ /** The static export reads the groups from the table in commons, the build from this one. */+ it("has an entry for every group the static export names a renderer by", () => {+ const groups = new Set(Object.values(CONTENT_RENDERER_GROUPS));+ expect([ ...groups ].filter((group) => !Object.hasOwn(CONTENT_RENDERERS, group))).toEqual([]);+ });+});+describe("getEmbedBoxSize", () => {it("sizes an embed by what it previews: none, audio, code or anything else", () => {const protectedCode = buildNote({ title: "Secret", type: "code", mime: "text/javascript" });diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex b98cef9c44..103941857f 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -2,12 +2,12 @@ import "./content_renderer.css";import {attachmentIcon,- CANVAS_ATTACHMENT_MIME,getEditableBlockRun,+ getFileContentType,getMimeTypeFromFileName,+ getNoteContentType,isAcceptedImageMime,isImageAttachmentRole,- isOfficeMimeType,normalizeMimeTypeForCKEditor,type TextRepresentationResponse} from "@triliumnext/commons";@@ -141,7 +141,6 @@ export interface NoteEditor {subscribeSaveState(listener: () => void): () => void;}-const CODE_MIME_TYPES = new Set(["application/json"]);/** The size of an attached text or code file above which it shows as a file: highlighting is slow. */const MAX_ATTACHED_CODE_SIZE = 256 * 1024;@@ -280,7 +279,7 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNo* group of files in the share theme's manifest, `content:<key>`; the editors of an embed are kept* under keys of their own, which a read-only page never loads.*/-const CONTENT_RENDERERS = {+export const CONTENT_RENDERERS = {markdown: () => import("@triliumnext/commons/src/lib/markdown_renderer"),iconPack: () => import("../widgets/type_widgets/icon_pack/IconPackPreview"),image: () => import("../widgets/react/ImageViewer"),@@ -1011,15 +1010,9 @@ function getRenderingType(entity: FNote | FAttachment) {/** The kind of content `entity` holds, from its note type or attachment role and its media type. */function getContentType(entity: FNote | FAttachment) {- if (entity instanceof FNote && entity.isIconPack()) {- // Icon packs (JSON `code`/`file` notes with #iconPack) render as their glyph grid, not as raw JSON.- return "iconPack";- }- if (entity instanceof FNote && entity.isMarkdown()) {- return "markdown";- }- if ("type" in entity) {- return getFileContentType(entity.type, entity.mime);+ if (entity instanceof FNote) {+ // Disabled icon packs (#disabled:iconPack, e.g. from a safe import) still preview.+ return getNoteContentType(entity.type, entity.mime, entity.hasLabelOrDisabled("iconPack"));}if (!("role" in entity)) {return "";@@ -1053,17 +1046,6 @@ function getAttachedFileContentType(mime: string, size = 0) {return size <= MAX_ATTACHED_CODE_SIZE ? "code" : "file";}-/** Narrows a file, or a `viewConfig` attachment, to the kind of file its media type names. */-function getFileContentType(type: string, mime: string) {- if (type === "file" && mime === "application/pdf") return "pdf";- if (type === "file" && mime === CANVAS_ATTACHMENT_MIME) return "canvasDrawing";- if ((type === "file" || type === "viewConfig") && CODE_MIME_TYPES.has(mime)) return "code";- if (type === "file" && mime.startsWith("audio/")) return "audio";- if (type === "file" && mime.startsWith("video/")) return "video";- if (type === "file" && isOfficeMimeType(mime)) return "office";- return type;-}-export default {getRenderedContent,disposeInteractiveContent,diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.tsindex 5b9805517e..9963435d3d 100644--- a/packages/commons/src/lib/notes.spec.ts+++ b/packages/commons/src/lib/notes.spec.ts@@ -1,9 +1,10 @@import { describe, expect, it } from "vitest";import {- CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getImageAttachmentTitle,- getMimeIcon, getNoteIcon, isFullWidthNoteType, MAP_POSITION_ATTRIBUTE, MAP_SHAPE_ATTRIBUTE,- NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink+ CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getContentRendererGroup,+ getImageAttachmentTitle, getMimeIcon, getNoteContentType, getNoteIcon, isFullWidthNoteType,+ MAP_POSITION_ATTRIBUTE, MAP_SHAPE_ATTRIBUTE, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS,+ parseMindMapNoteLink} from "./notes.js";import { NoteType } from "./rows.js";@@ -289,3 +290,41 @@ describe("isFullWidthNoteType", () => {expect(isFullWidthNoteType("search", "")).toBe(false);});});++describe("getNoteContentType", () => {+ it("names the content a note renders as, and the group of its renderer", () => {+ const kinds = [+ [ "code", "text/x-gfm", false ],+ [ "code", "application/json", true ],+ [ "file", "application/json", true ],+ [ "code", "application/json", false ],+ [ "file", "application/json", false ],+ [ "file", "application/pdf", false ],+ [ "file", CANVAS_ATTACHMENT_MIME, false ],+ [ "file", "audio/mpeg", false ],+ [ "file", "video/mp4", false ],+ [ "file", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", false ],+ [ "file", "application/zip", false ],+ [ "mermaid", "text/vnd.mermaid", false ]+ ] as const;++ expect(kinds.map(([ type, mime, hasIconPackLabel ]) => {+ const kind = getNoteContentType(type, mime, hasIconPackLabel);+ return [ kind, getContentRendererGroup(kind) ];+ })).toEqual([+ [ "markdown", "markdown" ],+ [ "iconPack", "iconPack" ],+ [ "iconPack", "iconPack" ],+ [ "code", undefined ],+ [ "code", undefined ],+ [ "pdf", "file" ],+ [ "canvasDrawing", "file" ],+ [ "audio", "file" ],+ [ "video", "file" ],+ [ "office", undefined ],+ [ "file", undefined ],+ [ "mermaid", "mermaid" ]+ ]);+ expect(getContentRendererGroup("constructor")).toBeUndefined();+ });+});diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.tsindex 4a70963151..5a4f700477 100644--- a/packages/commons/src/lib/notes.ts+++ b/packages/commons/src/lib/notes.ts@@ -4,6 +4,7 @@import { isFontMimeType } from "./font_mimes.js";import { MIME_TYPES_DICT, type MimeTypeDefinition } from "./mime_type.js";+import { isOfficeMimeType } from "./office.js";import { NoteType } from "./rows.js";export const NOTE_TYPE_ICONS = {@@ -227,6 +228,65 @@ export function isFullWidthNoteType(type: string, mime: string) {&& (mime === "application/pdf" || mime.startsWith("video/") || mime.startsWith("audio/"));}+/** Whether a `code` note of `mime` holds Markdown, which renders as text rather than as code. */+export function isMarkdownMime(mime: string) {+ return MARKDOWN_MIME_TYPES.includes(mime);+}++/**+ * The kind of content a note renders as, which picks its renderer: `iconPack` for an icon pack's+ * manifest, `markdown` for a Markdown code note, else what {@link getFileContentType} names.+ *+ * @param hasIconPackLabel whether the note carries `#iconPack`, disabled or not.+ */+export function getNoteContentType(type: string, mime: string, hasIconPackLabel: boolean) {+ if ((type === "code" || type === "file") && mime === "application/json" && hasIconPackLabel) {+ return "iconPack";+ }+ if (type === "code" && isMarkdownMime(mime)) {+ return "markdown";+ }+ return getFileContentType(type, mime);+}++/** Narrows a file, or a `viewConfig` attachment, to the kind of file its media type names. */+export function getFileContentType(type: string, mime: string) {+ if (type === "file" && mime === "application/pdf") return "pdf";+ if (type === "file" && mime === CANVAS_ATTACHMENT_MIME) return "canvasDrawing";+ if ((type === "file" || type === "viewConfig") && mime === "application/json") return "code";+ if (type === "file" && mime.startsWith("audio/")) return "audio";+ if (type === "file" && mime.startsWith("video/")) return "video";+ if (type === "file" && isOfficeMimeType(mime)) return "office";+ return type;+}++/**+ * The group of the share theme's manifest, `content:<group>`, that holds the renderer the app+ * loads on demand for each kind of content {@link getNoteContentType} names. A kind it renders+ * with nothing loaded on demand, such as text or code, has none. Each group is a key of the+ * client's `CONTENT_RENDERERS`.+ */+export const CONTENT_RENDERER_GROUPS: Readonly<Record<string, string>> = {+ iconPack: "iconPack",+ markdown: "markdown",+ image: "image",+ canvasDrawing: "file",+ pdf: "file",+ audio: "file",+ video: "file",+ mermaid: "mermaid",+ render: "render",+ webView: "webView",+ llmChat: "llmChat"+};++/** The group {@link CONTENT_RENDERER_GROUPS} names for content of `kind`, or `undefined` for none. */+export function getContentRendererGroup(kind: string) {+ return lookUpMime(CONTENT_RENDERER_GROUPS, kind);+}++const MARKDOWN_MIME_TYPES = [ "text/markdown", "text/x-markdown", "text/x-gfm" ];+const FULL_WIDTH_NOTE_TYPES = ["code", "image", "mermaid", "book", "render", "canvas", "webView", "noteMap", "relationMap","mindMap", "spreadsheet"diff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.tsindex 6f74f4f327..4114aa1962 100644--- a/packages/trilium-core/src/becca/entities/bnote.ts+++ b/packages/trilium-core/src/becca/entities/bnote.ts@@ -1,5 +1,5 @@import type { AttachmentRow, AttributeType, CloneResponse, EraseExcessRevisionsOptions, NoteRow, NoteType, RevisionRow, RevisionSource } from "@triliumnext/commons";-import { dayjs, getNoteIcon } from "@triliumnext/commons";+import { dayjs, getNoteIcon, isMarkdownMime } from "@triliumnext/commons";import cloningService from "../../services/cloning.js";import dateUtils from "../../services/utils/date.js";@@ -299,7 +299,7 @@ class BNote extends AbstractBeccaEntity<BNote> {/** @returns true if this note is a Markdown code note */isMarkdown() {- return this.type === "code" && (this.mime === "text/markdown" || this.mime === "text/x-markdown" || this.mime === "text/x-gfm");+ return this.type === "code" && isMarkdownMime(this.mime);}/** @returns true if this note is an image */diff --git a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts b/packages/trilium-core/src/services/export/zip/share_theme.spec.tsindex 82742ae9a7..46353f48e0 100644--- a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts+++ b/packages/trilium-core/src/services/export/zip/share_theme.spec.ts@@ -425,7 +425,10 @@ describe("getAppViewGroups", () => {{ type: "book", viewType: "calendar" },{ type: "book", viewType: "geoMap", isProtected: true },{ type: "book", viewType: "dashboard", children: [- { type: "code" },+ { type: "code", mime: "text/x-markdown" },+ { type: "file", mime: "application/pdf" },+ { type: "code", mime: "application/json", labels: [ "disabled:iconPack" ] },+ { type: "code", mime: "application/javascript" },{ type: "book", viewType: "table" }] },{ type: "book", viewType: "presentation", children: [@@ -435,10 +438,12 @@ describe("getAppViewGroups", () => {{ type: "book" }] });+ // A drawn note names the group of the renderer the app loads for its content, and a note+ // drawn with nothing loaded on demand, such as text or code, names none.expect([ ...getAppViewGroups(site) ].sort()).toEqual([- "app", "content:book", "content:code", "content:render", "content:text", "scripting",- "type:mermaid", "type:render", "view:calendar", "view:dashboard", "view:presentation",- "view:table"+ "app", "content:file", "content:iconPack", "content:markdown", "content:render",+ "scripting", "type:mermaid", "type:render", "view:calendar", "view:dashboard",+ "view:presentation", "view:table"]);const withoutViews = viewTree({ type: "text", children: [ { type: "book" } ] });expect(getAppViewGroups(withoutViews).size).toBe(0);@@ -497,6 +502,8 @@ describe("getShareThemeExportFiles", () => {interface FakeViewNote {type: string;+ mime?: string;+ labels?: string[];viewType?: string;isProtected?: boolean;children?: FakeViewNote[];@@ -507,8 +514,9 @@ function viewTree(definition: FakeViewNote): any {const children = (definition.children ?? []).map(viewTree);const note = {type: definition.type,- mime: "text/html",+ mime: definition.mime ?? "text/html",isProtected: !!definition.isProtected,+ hasLabel: (name: string) => !!definition.labels?.includes(name),isContentAvailable: () => true,getContent: () => "",getLabelValue: (name: string) => (name === "viewType" ? definition.viewType ?? null : null),diff --git a/packages/trilium-core/src/services/export/zip/share_theme.ts b/packages/trilium-core/src/services/export/zip/share_theme.tsindex b3d9aa9c68..7c5962d789 100644--- a/packages/trilium-core/src/services/export/zip/share_theme.ts+++ b/packages/trilium-core/src/services/export/zip/share_theme.ts@@ -1,4 +1,6 @@-import { getShareThemeGroupFiles, type ShareThemeManifest } from "@triliumnext/commons";+import {+ getContentRendererGroup, getNoteContentType, getShareThemeGroupFiles, type ShareThemeManifest+} from "@triliumnext/commons";import ejs from "ejs";import { convert as convertToText } from "html-to-text";import { t } from "i18next";@@ -246,8 +248,9 @@ export function getShareThemeTranslationFiles(note: BNote) {/*** Returns the groups of the share theme's manifest the pages of the export of `note` load to host* app views: `view:<viewType>` for each collection and `type:<noteType>` for each note type with- * one, `content:<noteType>` for the notes a dashboard or a presentation draws and `view:` for a- * collection among them, `app` beside any of them and `scripting` for a render note.+ * one, `content:<group>` for the renderer of each note a dashboard or a presentation draws, as+ * `CONTENT_RENDERER_GROUPS` names it, and `view:` for a collection among them, `app` beside any+ * of them and `scripting` for a render note.*/export function getAppViewGroups(note: BNote) {const groups = new Set<string>();@@ -265,7 +268,11 @@ export function getAppViewGroups(note: BNote) {groups.add(`view:${viewType}`);if (CONTENT_VIEW_TYPES.includes(viewType)) {for (const drawn of getDrawnNotes(subtreeNote)) {- groups.add(`content:${drawn.type}`);+ const contentGroup = getContentRendererGroup(getNoteContentType(drawn.type,+ drawn.mime, drawn.hasLabel("iconPack") || drawn.hasLabel("disabled:iconPack")));+ if (contentGroup) {+ groups.add(`content:${contentGroup}`);+ }const drawnViewType = getViewTypeOf(drawn);if (drawnViewType) {groups.add(`view:${drawnViewType}`);