Skip to content

Build your personal knowledge base with Trilium Notes

fix(highlightjs): register only the enabled languages

Elian Dorandbecc4d9f011
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html
index e8d8ccfabf..2641c027bd 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.html
@@ -46,8 +46,10 @@
<ul>
<li>When the language is set to <em>Auto-detected</em> (by default), Trilium
will try to identify the programming language (or similar) that corresponds
- to the given snippet of text and highlight it. If this is problematic,
- consider changing the language of the code block manually.</li>
+ to the given snippet of text and highlight it. Only the languages enabled
+ in <a class="reference-link" href="#root/_help_4TIF1oA4VQRO">Options</a> → <em>Code Notes</em> → <em>Available MIME types in the dropdown</em> are
+ considered. If this is problematic, consider changing the language of the
+ code block manually.</li>
<li>When the language is set to <em>Plain text</em>, there will be no syntax
highlighting.</li>
</ul>
diff --git a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md
index d5c0741649..e15459fa88 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md
@@ -27,7 +27,7 @@ Since TriliumNext v0.90.12, Trilium will try to offer syntax highlighting to the
Interaction:
-* When the language is set to _Auto-detected_ (by default), Trilium will try to identify the programming language (or similar) that corresponds to the given snippet of text and highlight it. If this is problematic, consider changing the language of the code block manually.
+* When the language is set to _Auto-detected_ (by default), Trilium will try to identify the programming language (or similar) that corresponds to the given snippet of text and highlight it. Only the languages enabled in <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/UI%20Elements/Options.md">Options</a> → _Code Notes_ → _Available MIME types in the dropdown_ are considered. If this is problematic, consider changing the language of the code block manually.
* When the language is set to _Plain text_, there will be no syntax highlighting.
Note that when editing a text note, syntax highlighting is automatically disabled if the code block is too big (somewhere around 500 lines). This value is currently not configurable. For <a class="reference-link" href="../../../Basic%20Concepts%20and%20Features/Notes/Read-Only%20Notes.md">Read-Only Notes</a>, this limitation is not applied.
diff --git a/packages/highlightjs/src/index.spec.ts b/packages/highlightjs/src/index.spec.ts
index 44ead2987b..c4a3617fc7 100644
--- a/packages/highlightjs/src/index.spec.ts
+++ b/packages/highlightjs/src/index.spec.ts
@@ -46,6 +46,16 @@ describe("ensureMimeTypes", () => {
expect(highlight("{}", { language: "application-json" })).not.toBeNull();
});
+ it("limits highlightAuto to the registered mime types", async () => {
+ const { ensureMimeTypes, highlightAuto } = await freshModule();
+ const python = "def greet(name):\n return f\"Hello, {name}\"\n";
+
+ expect(highlightAuto(python).language).toBeUndefined();
+
+ await ensureMimeTypes([mime("text/x-python")]);
+ expect(highlightAuto(python).language).toBe("text-x-python");
+ });
+
it("remembers a mime type with no highlight.js definition as unsupported", async () => {
const { ensureMimeTypes, highlight } = await freshModule();
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
diff --git a/packages/highlightjs/src/index.ts b/packages/highlightjs/src/index.ts
index 8984acc4b0..689b0df808 100644
--- a/packages/highlightjs/src/index.ts
+++ b/packages/highlightjs/src/index.ts
@@ -1,4 +1,4 @@
-import hljs from "highlight.js";
+import hljs from "highlight.js/lib/core";
import { normalizeMimeTypeForCKEditor, type MimeType } from "@triliumnext/commons";
import syntaxDefinitions from "./syntax_highlighting.js";
import { type Theme } from "./themes.js";