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(markdown): render with ReadOnlyText
Elian Doran0c051327cf43
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsxindex 158cd39ddb..86bff72708 100644--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx@@ -7,6 +7,7 @@ import { RefObject } from "preact";import { useEffect, useMemo, useRef, useState } from "preact/hooks";import SplitEditor from "../helpers/SplitEditor";+import { ReadOnlyTextContent } from "../text/ReadOnlyText";import { TypeWidgetProps } from "../type_widget";const marked = new Marked({ breaks: true, gfm: true });@@ -27,11 +28,11 @@ export default function Markdown(props: TypeWidgetProps) {editorRef={editorRef}onContentChanged={setContent}previewContent={(- <div- ref={previewRef}+ <ReadOnlyTextContent+ html={html}+ ntxId={props.ntxId}className="markdown-preview"- // eslint-disable-next-line react/no-danger- dangerouslySetInnerHTML={{ __html: html }}+ contentRef={previewRef}/>)}/>diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsxindex 172abb6990..e10a90cb80 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx@@ -5,7 +5,8 @@ import "./ReadOnlyText.css";import "@triliumnext/ckeditor5";import clsx from "clsx";-import { useEffect, useMemo, useRef } from "preact/hooks";+import { RefObject } from "preact";+import { useEffect, useMemo } from "preact/hooks";import appContext from "../../../components/app_context";import FNote from "../../../entities/fnote";@@ -13,7 +14,7 @@ import { applyInlineMermaid, rewriteMermaidDiagramsInContainer } from "../../../import { getLocaleById } from "../../../services/i18n";import { renderMathInElement } from "../../../services/math";import { formatCodeBlocks } from "../../../services/syntax_highlight";-import { useNoteBlob, useNoteLabel, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";+import { useNoteBlob, useNoteLabel, useSyncedRef, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";import { RawHtmlBlock } from "../../react/RawHtml";import TouchBar, { TouchBarButton, TouchBarSpacer } from "../../react/TouchBar";import { TypeWidgetProps } from "../type_widget";@@ -22,10 +23,54 @@ import { loadIncludedNote, refreshIncludedNote, setupImageOpening } from "./utilexport default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetProps) {const blob = useNoteBlob(note);- const contentRef = useRef<HTMLDivElement>(null);+ const { isRtl } = useNoteLanguage(note);++ return (+ <>+ <ReadOnlyTextContent+ html={blob?.content ?? ""}+ ntxId={ntxId}+ dir={isRtl ? "rtl" : "ltr"}+ />++ <TouchBar>+ <TouchBarSpacer size="flexible" />+ <TouchBarButton+ icon="NSLockUnlockedTemplate"+ click={() => {+ if (noteContext?.viewScope) {+ noteContext.viewScope.readOnlyTemporarilyDisabled = true;+ appContext.triggerEvent("readOnlyTemporarilyDisabled", { noteContext });+ }+ }}+ />+ </TouchBar>+ </>+ );+}++interface ReadOnlyTextContentProps {+ /** CKEditor-compatible HTML to render. */+ html: string;+ /** Note context id — enables `contentElRefreshed` / `executeWithContentElement` integrations when provided. */+ ntxId?: string | null;+ dir?: "ltr" | "rtl";+ /** Extra classes appended to the content div. */+ className?: string;+ /** Optional external ref to the rendered content div (e.g. to drive scroll sync). */+ contentRef?: RefObject<HTMLDivElement>;+}++/**+ * Renders arbitrary CKEditor-style HTML with the same pipeline as {@link ReadOnlyText}:+ * mermaid rewriting, inline mermaid, included-note expansion, KaTeX math, reference-link+ * titles, code-block syntax highlighting, and image click handling. Transforms re-run+ * whenever `html` changes.+ */+export function ReadOnlyTextContent({ html, ntxId, dir, className, contentRef: externalContentRef }: ReadOnlyTextContentProps) {+ const contentRef = useSyncedRef(externalContentRef);const [ codeBlockWordWrap ] = useTriliumOptionBool("codeBlockWordWrap");const [ codeBlockTabWidth ] = useTriliumOption("codeBlockTabWidth");- const { isRtl } = useNoteLanguage(note);useEffect(() => {document.body.style.setProperty("--code-block-tab-width", codeBlockTabWidth || "4");@@ -36,7 +81,9 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetProconst container = contentRef.current;if (!container) return;- appContext.triggerEvent("contentElRefreshed", { ntxId, contentEl: container });+ if (ntxId) {+ appContext.triggerEvent("contentElRefreshed", { ntxId, contentEl: container });+ }rewriteMermaidDiagramsInContainer(container);applyInlineMermaid(container);@@ -45,7 +92,7 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetProapplyReferenceLinks(container);formatCodeBlocks($(container));setupImageOpening(container, true);- }, [ blob ]);+ }, [ html, ntxId, contentRef ]);// React to included note changes.useTriliumEvent("refreshIncludedNote", ({ noteId }) => {@@ -55,33 +102,18 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetPro// Search integration.useTriliumEvent("executeWithContentElement", ({ resolve, ntxId: eventNtxId }) => {- if (eventNtxId !== ntxId || !contentRef.current) return;+ if (!ntxId || eventNtxId !== ntxId || !contentRef.current) return;resolve($(contentRef.current));});return (- <>- <RawHtmlBlock- containerRef={contentRef}- className={clsx("note-detail-readonly-text-content ck-content use-tn-links selectable-text", codeBlockWordWrap && "word-wrap")}- tabindex={100}- dir={isRtl ? "rtl" : "ltr"}- html={blob?.content}- />-- <TouchBar>- <TouchBarSpacer size="flexible" />- <TouchBarButton- icon="NSLockUnlockedTemplate"- click={() => {- if (noteContext?.viewScope) {- noteContext.viewScope.readOnlyTemporarilyDisabled = true;- appContext.triggerEvent("readOnlyTemporarilyDisabled", { noteContext });- }- }}- />- </TouchBar>- </>+ <RawHtmlBlock+ containerRef={contentRef}+ className={clsx("note-detail-readonly-text-content ck-content use-tn-links selectable-text", codeBlockWordWrap && "word-wrap", className)}+ tabindex={100}+ dir={dir}+ html={html}+ />);}