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(split-editor): show render errors as an admonition in the preview
Elian Doran91c63b638724
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 749daa282c..422a649625 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1159,6 +1159,11 @@"editable_code": {"placeholder": "Type the content of your code note here..."},+ "split_editor": {+ "render_error": "Unable to render the preview",+ "show_details": "Show details",+ "last_valid_render": "Showing last valid render"+ },"editable_text": {"placeholder": "Type the content of your note here...","editor_crashed_title": "The text editor crashed",diff --git a/apps/client/src/widgets/react/Admonition.css b/apps/client/src/widgets/react/Admonition.cssnew file mode 100644index 0000000000..7bbec47bb3--- /dev/null+++ b/apps/client/src/widgets/react/Admonition.css@@ -0,0 +1,41 @@+/* Extended admonition: an icon + title header, a body and an optional details+ collapsible. Builds on the base `.admonition`, but swaps its accent border for a+ neutral hairline and shows the type accent as a clipped leading bar instead. */+.admonition.extended-admonition {+ /* The header carries its own icon, so the space reserved for the base+ `::before` icon is not needed. A neutral hairline border surrounds the card,+ with the type accent shown only as the leading bar below. */+ padding-inline-start: 1em;+ border-color: var(--main-border-color);+}++/* Thick accent bar on the leading edge. It has no radius of its own; the card's+ rounded corners (with the base `overflow: hidden`) clip it to shape. */+.admonition.extended-admonition::before {+ content: "";+ position: absolute;+ inset-block: 0;+ inset-inline-start: 0;+ width: 4px;+ background-color: var(--accent-color);+}++.extended-admonition .admonition-header {+ display: flex;+ align-items: center;+ gap: 0.4em;+ margin-block-end: 0.35em;+}++.extended-admonition .admonition-header .tn-icon {+ color: var(--accent-color);+ font-size: 1.2em;+}++.extended-admonition .admonition-title {+ font-weight: 600;+}++.extended-admonition .admonition-details {+ margin-block-start: 0.5em;+}diff --git a/apps/client/src/widgets/react/Admonition.tsx b/apps/client/src/widgets/react/Admonition.tsxindex 726bb1b445..99cab39990 100644--- a/apps/client/src/widgets/react/Admonition.tsx+++ b/apps/client/src/widgets/react/Admonition.tsx@@ -1,6 +1,11 @@-import { HTML } from "mermaid/dist/diagram-api/types.js";+import "./Admonition.css";++import clsx from "clsx";import { ComponentChildren, HTMLAttributes } from "preact";+import Collapsible from "./Collapsible";+import Icon from "./Icon";+interface AdmonitionProps extends Pick<HTMLAttributes<HTMLDivElement>, "style"> {type: "warning" | "note" | "caution";children: ComponentChildren;@@ -9,8 +14,43 @@ interface AdmonitionProps extends Pick<HTMLAttributes<HTMLDivElement>, "style">export default function Admonition({ type, children, className, ...props }: AdmonitionProps) {return (- <div className={`admonition ${type} ${className}`} role="alert" {...props}>+ <div className={clsx("admonition", type, className)} role="alert" {...props}>{children}</div>);}++interface ExtendedAdmonitionProps extends AdmonitionProps {+ /** Boxicons class for the header icon, e.g. `bx bx-error-circle`. */+ icon: string;+ /** Bold heading shown next to the icon. */+ title: string;+ /** Optional content revealed by a collapsible below the body. */+ details?: ComponentChildren;+ /** Label for the details collapsible toggle; provide it whenever `details` is set. */+ detailsLabel?: string;+}++/**+ * An {@link Admonition} with a richer layout: an icon + title header, a body+ * (`children`) and an optional collapsible "details" section. Reuse for inline+ * error/notice cards that need more than a single block of text.+ */+export function ExtendedAdmonition({ type, icon, title, details, detailsLabel, className, children, ...props }: ExtendedAdmonitionProps) {+ return (+ <div className={clsx("admonition", "extended-admonition", type, className)} role="alert" {...props}>+ <div className="admonition-header">+ <Icon icon={icon} />+ <span className="admonition-title">{title}</span>+ </div>+ <div className="admonition-body">+ {children}+ </div>+ {details && (+ <Collapsible className="admonition-details" title={detailsLabel ?? ""}>+ {details}+ </Collapsible>+ )}+ </div>+ );+}diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.cssindex 3bf733c52d..2e282b4c60 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.css@@ -36,15 +36,56 @@ body.desktop .note-detail-split .note-detail-code-editor {margin-top: 1px;}-.note-detail-split .note-detail-error-container {+/* Error card, anchored to the top so the dimmed last render stays visible behind it.+ The card's header/details layout comes from `.extended-admonition`; here we only+ position it as an overlay and give it a solid background so the dimmed preview+ underneath doesn't bleed through the text. */+.note-detail-split .note-detail-split-preview-col .note-detail-error-card {+ position: absolute;+ inset-block-start: 0;+ inset-inline: 0;+ margin: 10px;+ max-height: calc(100% - 20px);+ overflow: auto;+ z-index: 10;++ background-color: var(--main-background-color);+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);+}++.note-detail-split .note-detail-error-message {font-family: var(--monospace-font-family);- margin: 5px;- white-space: pre-wrap;font-size: 0.85em;- overflow: auto;+ white-space: pre-wrap;+ overflow-wrap: anywhere;user-select: text;}+.note-detail-error-card pre {+ margin: 0.5em 0 0;+ padding: 0.5em 0.75em;+ border-radius: 4px;+ background-color: var(--accented-background-color);+ font-family: var(--monospace-font-family);+ font-size: 0.8em;+ white-space: pre-wrap;+ overflow-wrap: anywhere;+ user-select: text;+}++/* "Showing last valid render" badge, anchored bottom-left over the dimmed preview. */+.note-detail-split .note-detail-error-stale-badge.ext-badge {+ position: absolute;+ inset-block-end: 0;+ inset-inline-start: 0;+ margin: 10px;+ z-index: 10;++ --badge-radius: 12px;+ --color: var(--accented-background-color);+ color: var(--muted-text-color);+}+.note-detail-split .note-detail-split-preview {transition: opacity 250ms ease-in-out;height: 100%;diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsxindex cc97811380..771528901a 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx@@ -4,16 +4,20 @@ import Split from "@triliumnext/split.js";import { ComponentChildren } from "preact";import { useEffect, useRef } from "preact/hooks";+import { t } from "../../../services/i18n";import { DEFAULT_GUTTER_SIZE } from "../../../services/resizer";import utils, { isMobile } from "../../../services/utils";import ActionButton, { ActionButtonProps } from "../../react/ActionButton";-import Admonition from "../../react/Admonition";+import { ExtendedAdmonition } from "../../react/Admonition";+import { Badge } from "../../react/Badge";import { useEffectiveReadOnly, useNoteBlob, useNoteLabel, useTriliumOption } from "../../react/hooks";import { EditableCode, EditableCodeProps, ReadOnlyCode } from "../code/Code";export interface SplitEditorProps extends EditableCodeProps {className?: string;error?: string | null;+ /** When the preview still shows a previous successful render despite the current {@link error}. */+ previewStale?: boolean;splitOptions?: Split.Options;previewContent: ComponentChildren;previewButtons?: ComponentChildren;@@ -37,7 +41,7 @@ export interface SplitEditorProps extends EditableCodeProps {* - Can display errors to the user via {@link setError}.* - Horizontal or vertical orientation for the editor/preview split, adjustable via the switch split orientation button floating button.*/-export default function SplitEditor({ note, noteContext, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, extraContent, ...editorProps }: SplitEditorProps) {+export default function SplitEditor({ note, noteContext, error, previewStale, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, extraContent, ...editorProps }: SplitEditorProps) {const containerRef = useRef<HTMLDivElement>(null);const splitEditorOrientation = useSplitOrientation(forceOrientation);const [ displayMode ] = useNoteLabel(note, "displayMode");@@ -80,17 +84,13 @@ export default function SplitEditor({ note, noteContext, error, splitOptions, pr{...editorProps}/>}</div>- {error && (- <Admonition type="caution" className="note-detail-error-container">- {error}- </Admonition>- )}{extraContent}</div>);const preview = previewMounted.current && <PreviewContainererror={error}+ previewStale={previewStale}previewContent={previewContent}previewButtons={previewButtons}/>;@@ -123,16 +123,37 @@ export default function SplitEditor({ note, noteContext, error, splitOptions, pr);}-function PreviewContainer({ error, previewContent, previewButtons }: {+function PreviewContainer({ error, previewStale, previewContent, previewButtons }: {error?: string | null;+ previewStale?: boolean;previewContent: ComponentChildren;previewButtons?: ComponentChildren;}) {+ const { summary, details } = error ? splitPreviewError(error) : {};return (<div className="note-detail-split-preview-col"><div className={`note-detail-split-preview ${error ? "on-error" : ""}`}>{previewContent}</div>+ {error && (+ <ExtendedAdmonition+ className="note-detail-error-card"+ type="caution"+ icon="bx bx-error-circle"+ title={t("split_editor.render_error")}+ detailsLabel={t("split_editor.show_details")}+ details={details && <pre>{details}</pre>}+ >+ <div className="note-detail-error-message">{summary}</div>+ </ExtendedAdmonition>+ )}+ {error && previewStale && (+ <Badge+ className="note-detail-error-stale-badge"+ icon="bx bx-history"+ text={t("split_editor.last_valid_render")}+ />+ )}<div className="btn-group btn-group-sm map-type-switcher content-floating-buttons preview-buttons bottom-right" role="group">{previewButtons}</div>@@ -140,6 +161,24 @@ function PreviewContainer({ error, previewContent, previewButtons }: {);}+/**+ * Splits a raw preview/render error into its first line, shown under the title as a+ * headline, and the remaining lines, revealed by the "show details" collapsible. The+ * first line is never repeated in the details; `details` is omitted when the error is+ * a single line.+ */+function splitPreviewError(error: string): { summary: string; details?: string } {+ const trimmed = error.trim();+ const newlineIndex = trimmed.indexOf("\n");+ if (newlineIndex < 0) {+ return { summary: trimmed };+ }+ return {+ summary: trimmed.slice(0, newlineIndex).trim(),+ details: trimmed.slice(newlineIndex + 1).trim() || undefined+ };+}+export function PreviewButton(props: Omit<ActionButtonProps, "titlePosition">) {return <ActionButton{...props}diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsxindex d0043f9784..3195e48b98 100644--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx@@ -115,6 +115,7 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,className="svg-editor"note={note} ntxId={ntxId}error={error}+ previewStale={!!svg}onContentChanged={onContentChanged}dataSaved={onSave}placeholder={t("mermaid.placeholder")}