Skip to content

Build your personal knowledge base with Trilium Notes

client: Add syntax highlight for code note previews

Elian Doran91fa1a6cb1e3
diff --git a/src/public/app/services/content_renderer.js b/src/public/app/services/content_renderer.js
index 162ad36305..3593662829 100644
--- a/src/public/app/services/content_renderer.js
+++ b/src/public/app/services/content_renderer.js
@@ -10,7 +10,8 @@ import treeService from "./tree.js";
import FNote from "../entities/fnote.js";
import FAttachment from "../entities/fattachment.js";
import imageContextMenuService from "../menus/image_context_menu.js";
-import { applySyntaxHighlight } from "./syntax_highlight.js";
+import { applySingleBlockSyntaxHighlight, applySyntaxHighlight } from "./syntax_highlight.js";
+import mime_types from "./mime_types.js";
let idCounter = 1;
@@ -113,11 +114,18 @@ async function renderText(note, $renderedContent) {
}
}
-/** @param {FNote} note */
+/**
+ * Renders a code note, by displaying its content and applying syntax highlighting based on the selected MIME type.
+ *
+ * @param {FNote} note
+ */
async function renderCode(note, $renderedContent) {
const blob = await note.getBlob();
- $renderedContent.append($("<pre>").text(blob.content));
+ const $codeBlock = $("<pre>");
+ $codeBlock.text(blob.content);
+ applySingleBlockSyntaxHighlight($codeBlock, mime_types.normalizeMimeTypeForCKEditor(note.mime));
+ $renderedContent.append($codeBlock);
}
function renderImage(entity, $renderedContent, options = {}) {
diff --git a/src/public/app/services/syntax_highlight.js b/src/public/app/services/syntax_highlight.js
index a921fe35c7..45d87cd953 100644
--- a/src/public/app/services/syntax_highlight.js
+++ b/src/public/app/services/syntax_highlight.js
@@ -29,26 +29,39 @@ export async function applySyntaxHighlight($container) {
const codeBlocks = $container.find("pre code");
for (const codeBlock of codeBlocks) {
- $(codeBlock).parent().toggleClass("hljs");
-
- const text = codeBlock.innerText;
-
const normalizedMimeType = extractLanguageFromClassList(codeBlock);
if (!normalizedMimeType) {
continue;
}
+
+ applySingleBlockSyntaxHighlight($(codeBlock, normalizedMimeType));
+ }
+}
+
+/**
+ * Applies syntax highlight to the given code block (assumed to be <pre><code>), using highlight.js.
+ *
+ * @param {*} $codeBlock
+ * @param {*} normalizedMimeType
+ */
+export async function applySingleBlockSyntaxHighlight($codeBlock, normalizedMimeType) {
+ $codeBlock.parent().toggleClass("hljs");
+ const text = $codeBlock.text();
- let highlightedText = null;
- if (normalizedMimeType === mime_types.MIME_TYPE_AUTO) {
- highlightedText = hljs.highlightAuto(text);
- } else if (normalizedMimeType) {
- const language = mime_types.getHighlightJsNameForMime(normalizedMimeType);
+ let highlightedText = null;
+ if (normalizedMimeType === mime_types.MIME_TYPE_AUTO) {
+ highlightedText = hljs.highlightAuto(text);
+ } else if (normalizedMimeType) {
+ const language = mime_types.getHighlightJsNameForMime(normalizedMimeType);
+ if (language) {
highlightedText = hljs.highlight(text, { language });
+ } else {
+ console.warn(`Unknown mime type: ${normalizedMimeType}.`);
}
-
- if (highlightedText) {
- codeBlock.innerHTML = highlightedText.value;
- }
+ }
+
+ if (highlightedText) {
+ $codeBlock.html(highlightedText.value);
}
}