Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): basic rendering in share

Elian Doran1571267496d4
diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts
index 41e3fd7177..cfc9c3a397 100644
--- a/apps/server/src/share/content_renderer.ts
+++ b/apps/server/src/share/content_renderer.ts
@@ -1,5 +1,5 @@
import { sanitizeUrl } from "@braintree/sanitize-url";
-import { renderSpreadsheetToHtml } from "@triliumnext/commons";
+import { renderSpreadsheetToHtml, renderToHtml as renderMarkdownToHtml } from "@triliumnext/commons";
import { highlightAuto } from "@triliumnext/highlightjs";
import ejs from "ejs";
import escapeHtml from "escape-html";
@@ -14,6 +14,7 @@ import type BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
import assetPath, { assetUrlFragment } from "../services/asset_path.js";
import { generateCss, getIconPacks, MIME_TO_EXTENSION_MAPPINGS, ProcessedIconPack } from "../services/icon_packs.js";
+import htmlSanitizer from "../services/html_sanitizer.js";
import log from "../services/log.js";
import options from "../services/options.js";
import utils, { getResourceDir, isDev, safeExtractMessageAndStackFromError } from "../services/utils.js";
@@ -275,6 +276,8 @@ export function getContent(note: SNote | BNote) {
if (note.type === "text") {
renderText(result, note);
+ } else if (note.type === "code" && note.mime === "text/x-markdown") {
+ renderMarkdown(result, note);
} else if (note.type === "code") {
renderCode(result);
} else if (note.type === "mermaid") {
@@ -451,6 +454,37 @@ function cleanUpReferenceLinks(linkEl: HTMLElement, getNote: GetNoteFunction) {
}
}
+/**
+ * Renders a markdown code note by converting the markdown source to HTML
+ * using the shared {@link renderMarkdownToHtml} pipeline.
+ */
+function renderMarkdown(result: Result, note: SNote | BNote) {
+ if (typeof result.content !== "string" || !result.content?.trim()) {
+ result.isEmpty = true;
+ return;
+ }
+
+ let html = renderMarkdownToHtml(result.content, note.title, {
+ sanitize: htmlSanitizer.sanitize,
+ wikiLink: { formatHref: (id) => `./${id}` }
+ });
+
+ // Apply syntax highlighting to code blocks, same as renderText.
+ const parseOpts: Partial<Options> = { blockTextElements: {} };
+ const document = parse(html, parseOpts);
+ for (const codeEl of document.querySelectorAll("pre code")) {
+ if (codeEl.classList.contains("language-mermaid")) {
+ continue;
+ }
+
+ const highlightResult = highlightAuto(codeEl.text);
+ codeEl.innerHTML = highlightResult.value;
+ codeEl.classList.add("hljs");
+ }
+
+ result.content = document.innerHTML;
+}
+
/**
* Renders a code note.
*/