Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): HTML tags displayed escaped in headings

Elian Dorand2b6014b499f
diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.ts
index 75d6b564ce..8a4d7c7aa8 100644
--- a/apps/server/src/services/utils.ts
+++ b/apps/server/src/services/utils.ts
@@ -497,7 +497,7 @@ export function formatSize(size: number | null | undefined) {
}
}
-export function slugify(text: string) {
+function slugify(text: string) {
return text
.normalize("NFKD") // handles accents like é → e
.toLowerCase()
@@ -540,6 +540,7 @@ export default {
safeExtractMessageAndStackFromError,
sanitizeSqlIdentifier,
stripTags,
+ slugify,
timeLimit,
toBase64,
toMap,
diff --git a/apps/server/src/share/routes.ts b/apps/server/src/share/routes.ts
index f8bce49b08..77f542ba2d 100644
--- a/apps/server/src/share/routes.ts
+++ b/apps/server/src/share/routes.ts
@@ -14,7 +14,7 @@ import log from "../services/log.js";
import type SNote from "./shaca/entities/snote.js";
import type SBranch from "./shaca/entities/sbranch.js";
import type SAttachment from "./shaca/entities/sattachment.js";
-import utils, { isDev, safeExtractMessageAndStackFromError, slugify } from "../services/utils.js";
+import utils, { isDev, safeExtractMessageAndStackFromError } from "../services/utils.js";
import options from "../services/options.js";
import { t } from "i18next";
import ejs from "ejs";
@@ -176,7 +176,7 @@ function register(router: Router) {
showLoginInShareTheme,
t,
isDev,
- slugify
+ utils
};
let useDefaultView = true;
diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejs
index 0a227a32c4..2fd07c8a7c 100644
--- a/packages/share-theme/src/templates/page.ejs
+++ b/packages/share-theme/src/templates/page.ejs
@@ -91,7 +91,8 @@ const themeClass = currentTheme === "light" ? " theme-light" : " theme-dark";
const headingRe = /(<h[1-6]>)(.+?)(<\/h[1-6]>)/g;
const headingMatches = [...content.matchAll(headingRe)];
content = content.replaceAll(headingRe, (...match) => {
- match[0] = match[0].replace(match[3], `<a id="${slugify(match[2])}" class="toc-anchor" name="${slugify(match[2])}" href="#${slugify(match[2])}">#</a>${match[3]}`);
+ const slug = utils.slugify(utils.stripTags(match[2]));
+ match[0] = match[0].replace(match[3], `<a id="${slug}" class="toc-anchor" name="${slug}" href="#${slug}">#</a>${match[3]}`);
return match[0];
});
%>
diff --git a/packages/share-theme/src/templates/toc_item.ejs b/packages/share-theme/src/templates/toc_item.ejs
index 726ca4cca8..4346fe55a9 100644
--- a/packages/share-theme/src/templates/toc_item.ejs
+++ b/packages/share-theme/src/templates/toc_item.ejs
@@ -1,11 +1,11 @@
<%
-const slug = slugify(entry.name);
+const strippedName = utils.stripTags(entry.name);
+const slug = utils.slugify(strippedName);
%>
-
<li>
<a href="#<%= slug %>">
- <span><%= entry.name %></span>
+ <span><%= strippedName %></span>
</a>
<% if (entry.children.length) { %>