Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): heading and navigation not supporting CJK (closes #6430)

Elian Doran94d62f810a57
diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.ts
index ca6b809bb3..75d6b564ce 100644
--- a/apps/server/src/services/utils.ts
+++ b/apps/server/src/services/utils.ts
@@ -497,6 +497,14 @@ export function formatSize(size: number | null | undefined) {
}
}
+export function slugify(text: string) {
+ return text
+ .normalize("NFKD") // handles accents like é → e
+ .toLowerCase()
+ .replace(/[^\p{Letter}\p{Number}]+/gu, "-") // keep Unicode letters/numbers
+ .replace(/(^-|-$)+/g, ""); // trim leading/trailing dashes
+}
+
export default {
compareVersions,
crash,
diff --git a/apps/server/src/share/routes.ts b/apps/server/src/share/routes.ts
index 37162ea286..f8bce49b08 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 } from "../services/utils.js";
+import utils, { isDev, safeExtractMessageAndStackFromError, slugify } from "../services/utils.js";
import options from "../services/options.js";
import { t } from "i18next";
import ejs from "ejs";
@@ -175,7 +175,8 @@ function register(router: Router) {
appPath: isDev ? appPath : `../${appPath}`,
showLoginInShareTheme,
t,
- isDev
+ isDev,
+ slugify
};
let useDefaultView = true;
diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejs
index cc96cc4cae..0a227a32c4 100644
--- a/packages/share-theme/src/templates/page.ejs
+++ b/packages/share-theme/src/templates/page.ejs
@@ -90,7 +90,6 @@ const currentTheme = note.getLabel("shareTheme") === "light" ? "light" : "dark";
const themeClass = currentTheme === "light" ? " theme-light" : " theme-dark";
const headingRe = /(<h[1-6]>)(.+?)(<\/h[1-6]>)/g;
const headingMatches = [...content.matchAll(headingRe)];
-const slugify = (text) => text.toLowerCase().replace(/[^\w]/g, "-");
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]}`);
return match[0];
diff --git a/packages/share-theme/src/templates/toc_item.ejs b/packages/share-theme/src/templates/toc_item.ejs
index b18b4a1a60..726ca4cca8 100644
--- a/packages/share-theme/src/templates/toc_item.ejs
+++ b/packages/share-theme/src/templates/toc_item.ejs
@@ -1,5 +1,4 @@
<%
-const slugify = (text) => text.toLowerCase().replace(/[^\w]/g, "-");
const slug = slugify(entry.name);
%>