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
feat(share): show shared code notes in the app's read-only code view
Elian Doran0c43682ae6bc
diff --git a/packages/share-theme/src/content/app_view.css b/packages/share-theme/src/content/app_view.cssindex 231200af69..15a32046dd 100644--- a/packages/share-theme/src/content/app_view.css+++ b/packages/share-theme/src/content/app_view.css@@ -24,7 +24,7 @@ body:has(#content.app-view) {flex: 1;min-height: 0;- > :is(.note-list-widget, .note-detail-mermaid) {+ > * {flex: 1;min-height: 0;}diff --git a/packages/share-theme/src/content/note_view.tsx b/packages/share-theme/src/content/note_view.tsxindex f169cd8e08..7ffba50da9 100644--- a/packages/share-theme/src/content/note_view.tsx+++ b/packages/share-theme/src/content/note_view.tsx@@ -1,6 +1,8 @@import type FNote from "@triliumnext/client/src/entities/fnote.js";import { t } from "@triliumnext/client/src/services/i18n.js";-import { TYPE_MAPPINGS, type TypeWidget } from "@triliumnext/client/src/widgets/note_types.js";+import {+ type ExtendedNoteType, TYPE_MAPPINGS, type TypeWidget+} from "@triliumnext/client/src/widgets/note_types.js";import { useNoteLabel } from "@triliumnext/client/src/widgets/react/hooks.js";import OverlayControlGroup, {OverlayControlButton@@ -29,7 +31,7 @@ export default function mountNoteView(container: HTMLElement, payload: AppPayloafunction NoteView({ note }: { note: FNote }) {const [ Widget, setWidget ] = useState<TypeWidget>();- const mapping = TYPE_MAPPINGS[note.type as keyof typeof TYPE_MAPPINGS];+ const mapping = TYPE_MAPPINGS[getWidgetType(note)];useEffect(() => {Promise.resolve(mapping.view()).then((view) => {@@ -53,6 +55,18 @@ function NoteView({ note }: { note: FNote }) {);}+/**+ * The widget the app shows the note with when it is read-only, as a shared note always is: the+ * read-only editor for code and text, the type's own widget otherwise.+ */+function getWidgetType(note: FNote): ExtendedNoteType {+ switch (note.type) {+ case "code": return "readOnlyCode";+ case "text": return "readOnlyText";+ default: return note.type as ExtendedNoteType;+ }+}+/** The app's choice of source, split or preview, which a visitor makes for this page only. */function DisplayModeSwitcher({ note }: { note: FNote }) {const [ displayMode ] = useNoteLabel(note, "displayMode");diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex d4967bd213..d3006153d4 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -488,11 +488,10 @@ export function getContent(note: SNote | BNote, options: ShareRenderOptions = {}renderMarkdown(result, note);} else if (note.type === "code") {renderCode(result, note.mime);+ hostNoteView(result, note);} else if (note.type === "mermaid") {renderMermaid(result, note);- if (!(note instanceof BNote) && typeof result.content === "string") {- hostInAppView(result, note, "share-note-view");- }+ hostNoteView(result, note);} else if (["image", "canvas", "mindMap"].includes(note.type)) {renderImage(result, note);} else if (note.type === "file") {@@ -917,6 +916,17 @@ function renderCollectionView(result: Result, note: SNote) {hostInAppView(result, note, "share-collection");}+/**+ * Has the share theme show a shared note with the app's own widget for its type, keeping the content+ * rendered here for a visitor without scripts. The static export and binary content keep the+ * content alone.+ */+function hostNoteView(result: Result, note: SNote | BNote) {+ if (!(note instanceof BNote) && typeof result.content === "string") {+ hostInAppView(result, note, "share-note-view");+ }+}+/*** Wraps the content in a `container` the share theme's script mounts an app view into, beside the* notes and the display options the view starts from. The content stays for a visitor without@@ -939,8 +949,16 @@ function hostInAppView(result: Result, note: SNote, container: string) {/** The view types of the collections the share theme shows with the app's own view. */const HOSTED_VIEW_TYPES = new Set([ "calendar", "geoMap" ]);-/** The options the app's collection views read, which a shared page needs to draw them alike. */-const SHARED_OPTIONS = [ "locale", "formattingLocale", "firstDayOfWeek" ] as const;+/**+ * The options the app's views read to draw a note as the app does, which a shared page receives. Only+ * display options: the rest of the options can hold secrets, such as API keys.+ */+const SHARED_OPTIONS = [+ "locale", "formattingLocale", "firstDayOfWeek",+ "codeNoteTheme", "codeNoteThemeLight", "codeNoteThemeDark", "codeNoteThemeMatchesApp",+ "codeLineWrapEnabled", "codeNoteTabWidth", "codeNoteIndentWithTabs",+ "splitEditorOrientation"+] as const;/** The `data-*` attributes of a Mermaid note that label its viewer, and their translations. */const MERMAID_VIEWER_LABELS = {