Skip to content

Build your personal knowledge base with Trilium Notes

client/code notes: fix some issues

Adorian Dorane719acde3aef
diff --git a/apps/client/src/services/code_note_preview.spec.ts b/apps/client/src/services/code_note_preview.spec.ts
index 0d34213161..40825d91b5 100644
--- a/apps/client/src/services/code_note_preview.spec.ts
+++ b/apps/client/src/services/code_note_preview.spec.ts
@@ -77,7 +77,7 @@ describe("renderCodeNotePreview", () => {
expect(pre.getAttribute("style")).toBe("--code-preview-digits: 3");
});
- it("renders the same content once, until 256 others push it out", async () => {
+ it("renders the same content once, until others fill the budget of characters", async () => {
const renderCodePreview = vi.fn();
vi.doMock("@triliumnext/codemirror/src/code_preview.js", async (importOriginal) => {
const original = await importOriginal<CodeMirrorPreviewModule>();
@@ -86,13 +86,29 @@ describe("renderCodeNotePreview", () => {
});
try {
const { renderCodeNotePreview } = await freshModule({});
- for (let index = 0; index <= 256; index++) {
- await renderCodeNotePreview(`a = ${index}`, "text/plain");
+ const isRendered = async (content: string) => {
+ const calls = renderCodePreview.mock.calls.length;
+ await renderCodeNotePreview(content, "text/plain");
+ return renderCodePreview.mock.calls.length > calls;
+ };
+ const large = (index: number) => `${index}${"x".repeat(400_000)}`;
+
+ expect(await isRendered("a = 0")).toBe(true);
+ expect(await isRendered("a = 0")).toBe(false);
+
+ // A preview over the limit of one entry is not cached, and pushes out nothing.
+ const huge = "x".repeat(1_000_000);
+ expect(await isRendered(huge)).toBe(true);
+ expect(await isRendered(huge)).toBe(true);
+ expect(await isRendered("a = 0")).toBe(false);
+
+ // Ten entries of about 800,000 characters, key and preview, overrun the budget.
+ for (let index = 0; index < 10; index++) {
+ expect(await isRendered(large(index))).toBe(true);
}
- await renderCodeNotePreview("a = 256", "text/plain");
- expect(renderCodePreview).toHaveBeenCalledTimes(257);
- await renderCodeNotePreview("a = 0", "text/plain");
- expect(renderCodePreview).toHaveBeenCalledTimes(258);
+ expect(await isRendered("a = 0")).toBe(true);
+ expect(await isRendered(large(0))).toBe(true);
+ expect(await isRendered(large(9))).toBe(false);
} finally {
vi.doUnmock("@triliumnext/codemirror/src/code_preview.js");
}
diff --git a/apps/client/src/services/code_note_preview.ts b/apps/client/src/services/code_note_preview.ts
index a35d8132e2..70e10a081a 100644
--- a/apps/client/src/services/code_note_preview.ts
+++ b/apps/client/src/services/code_note_preview.ts
@@ -18,9 +18,13 @@ interface ThemeChoice {
scope?: string;
}
-const CACHE_MAX = 256;
+/** The most characters that the keys and the previews in `cache` hold together. */
+const CACHE_MAX_LENGTH = 8_000_000;
+/** The most characters of one key and preview that `cache` keeps. */
+const CACHE_MAX_ENTRY_LENGTH = 1_000_000;
const cache = new Map<string, string>();
+let cacheLength = 0;
const mountedThemes = new Set<string>();
/**
@@ -87,10 +91,16 @@ function isLineWrapped(note: FNote | null | undefined) {
return label == null ? options.is("codeLineWrapEnabled") : label !== "false";
}
+/** Caches `html` under `key`, and drops the oldest previews that no longer fit the budget. */
function remember(key: string, html: string) {
- if (cache.size >= CACHE_MAX) {
- const oldest = cache.keys().next().value;
- if (oldest !== undefined) cache.delete(oldest);
+ const length = key.length + html.length;
+ if (length > CACHE_MAX_ENTRY_LENGTH) return;
+
+ for (const [ oldKey, oldHtml ] of cache) {
+ if (cacheLength + length <= CACHE_MAX_LENGTH) break;
+ cache.delete(oldKey);
+ cacheLength -= oldKey.length + oldHtml.length;
}
cache.set(key, html);
+ cacheLength += length;
}
diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
index ebdab12ab4..4e65a677eb 100644
--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
@@ -307,6 +307,50 @@ describe("CodeEmbed", () => {
});
});
+ it("previews again as the code note themes or the line wrapping change", async () => {
+ const note = buildNote({
+ title: "Script", type: "code", mime: "text/x-python", content: "a", "#wrapLines": "true"
+ });
+ await mount(note, buildEditor());
+ const reload = async (loadResults: LoadResults) => {
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", { loadResults });
+ });
+ };
+ const names = [
+ "codeNoteTheme", "codeNoteThemeMatchesApp", "codeNoteThemeLight", "codeNoteThemeDark",
+ "codeLineWrapEnabled"
+ ] as const;
+ const previous = names.map((name) => options.get(name));
+
+ try {
+ for (const name of names) {
+ options.set(name, "changed");
+ const optionChanged = new LoadResults([]);
+ optionChanged.addOption(name);
+ await reload(optionChanged);
+ }
+
+ const [ label ] = note.getOwnedLabels("wrapLines");
+ label.value = "false";
+ const labelChanged = new LoadResults([ {
+ entityName: "attributes", entityId: label.attributeId, hash: "", isSynced: true,
+ isErased: false, entity: {
+ attributeId: label.attributeId, noteId: note.noteId, type: "label",
+ name: "wrapLines", value: "false", isDeleted: false
+ }
+ } as never ]);
+ labelChanged.addAttribute(label.attributeId, "other");
+ await reload(labelChanged);
+
+ await vi.waitFor(() => expect(renderCodePreview).toHaveBeenCalledTimes(6));
+ } finally {
+ for (const [ index, name ] of names.entries()) {
+ options.set(name, previous[index]);
+ }
+ }
+ });
+
it("follows each save of its note or attachment, its own included, while it has no changes", async () => {
const note = buildCodeNote("one");
const editor = buildEditor();
diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
index d153d680b6..01850ceb06 100644
--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
@@ -11,7 +11,9 @@ import {
type CodeSettings, type CodeText, useCodeLanguageMenu, useNoteCodeSettings, useTabWidthMenu
} from "../../layout/code_menus";
import { SaveStateBadge } from "../../layout/NoteBadges";
-import { useNoteBlob, useTriliumEvent } from "../../react/hooks";
+import {
+ useNoteBlob, useNoteLabelOptionalBool, useTriliumEvent, useTriliumOption
+} from "../../react/hooks";
import { useContentEmbedMenus } from "../text/content_embed_menus";
import { useContentEmbedAttribute } from "../text/content_embed_tools";
import {
@@ -58,9 +60,10 @@ export default function CodeEmbed({
const [ content, setContent ] = useState(initialContent);
const mime = settings.mime ?? initialMime;
useSavedContent(entity, editor, setContent);
+ const appearance = useCodePreviewAppearance(note);
const shownPreview = useEmbedPreview(
preview,
- `${mime}\n${content}`,
+ `${mime}\n${appearance}\n${content}`,
() => renderCodePreview(content, mime, note),
isEditing
);
@@ -102,6 +105,21 @@ export default function CodeEmbed({
);
}
+/**
+ * The options and the `#wrapLines` label of `note` that `renderCodePreview()` draws with, as a key
+ * that changes with any of them.
+ */
+function useCodePreviewAppearance(note: FNote | null) {
+ const [ theme ] = useTriliumOption("codeNoteTheme");
+ const [ matchesApp ] = useTriliumOption("codeNoteThemeMatchesApp");
+ const [ lightTheme ] = useTriliumOption("codeNoteThemeLight");
+ const [ darkTheme ] = useTriliumOption("codeNoteThemeDark");
+ const [ isWrapped ] = useTriliumOption("codeLineWrapEnabled");
+ const [ isNoteWrapped ] = useNoteLabelOptionalBool(note, "wrapLines");
+
+ return [ theme, matchesApp, lightTheme, darkTheme, isWrapped, isNoteWrapped ].join("\n");
+}
+
/**
* The code settings of a file, which its embed keeps in `data-code-*` attributes: a language that
* replaces `fileMime`, the language of its name, and the indentation. `initialLanguage` is the
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index ddebcc1825..83fa2f10d6 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1258,18 +1258,27 @@ describe("content_renderer", () => {
const pythonBlock = () => buildShareNote({
content: `<pre><code class="language-text-x-python">def x(): pass</code></pre>`
});
+ const pythonNote = () => {
+ const result: Result = { header: "", content: "def x(): pass" };
+ renderCode(result, "text/x-python");
+ return String(result.content);
+ };
+ const highlightedToken = /<span class="l\d+ d\d+">/;
getOption.mockReturnValue(JSON.stringify([ "text/x-python" ]));
const first = ensureShareHighlighting();
expect(ensureShareHighlighting()).toBe(first);
await first;
expect(getContent(pythonBlock()).content).toContain("hljs-keyword");
+ expect(pythonNote()).toMatch(highlightedToken);
getOption.mockReturnValue(JSON.stringify([ "text/x-go" ]));
const second = ensureShareHighlighting();
expect(second).not.toBe(first);
await second;
expect(getContent(pythonBlock()).content).not.toContain("hljs");
+ expect(pythonNote()).toContain("code-preview-line");
+ expect(pythonNote()).not.toMatch(highlightedToken);
getOption.mockRestore();
await ensureShareHighlighting();
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 0354ada411..446fb8df0a 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -873,8 +873,15 @@ type CodePreviewModule = typeof import("@triliumnext/codemirror/src/code_preview
let registeredMimeTypesOption: string | null = null;
let pendingRegistration: Promise<void> | null = null;
-/** The renderer of code notes and the share themes, once `ensureShareHighlighting()` loads them. */
-let codePreviews: { module: CodePreviewModule; themes: PreviewThemeSlot[] } | null = null;
+/**
+ * The renderer of code notes, the share themes and the enabled MIME types, once
+ * `ensureShareHighlighting()` loads them.
+ */
+let codePreviews: {
+ module: CodePreviewModule;
+ themes: PreviewThemeSlot[];
+ enabledMimes: Set<string>;
+} | null = null;
/**
* Registers the highlight.js languages enabled in the `codeNotesMimeTypes` option and unregisters
@@ -912,7 +919,7 @@ async function loadCodePreviews(mimeTypes: MimeType[]) {
}))),
Promise.all(enabledMimes.map((mime) => module.loadPreviewLanguage(mime)))
]);
- codePreviews = { module, themes };
+ codePreviews = { module, themes, enabledMimes: new Set(enabledMimes) };
}
/**
@@ -925,8 +932,8 @@ export function renderCode(result: Result, mime?: string, note?: SNote | BNote)
if (typeof result.content !== "string" || !result.content?.trim()) {
result.isEmpty = true;
} else if (codePreviews) {
- const { module, themes } = codePreviews;
- const language = mime && shouldSyntaxHighlight(result.content)
+ const { module, themes, enabledMimes } = codePreviews;
+ const language = mime && enabledMimes.has(mime) && shouldSyntaxHighlight(result.content)
? module.getLoadedPreviewLanguage(mime) ?? null
: null;
result.content = module.renderCodePreview(result.content, {