Skip to content

Commit c275ef1

Browse files
committed
fix
1 parent 58fcbb3 commit c275ef1

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

src/pages/markdownPreview/index.js

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818

1919
let previewController = null;
2020
let mermaidModulePromise = null;
21+
let mermaidThemeSignature = "";
2122

2223
function getThemeColor(name, fallback) {
2324
const value = getComputedStyle(document.documentElement)
@@ -63,14 +64,58 @@ function getTargetElement(container, targetId) {
6364

6465
async function getMermaid() {
6566
if (!mermaidModulePromise) {
66-
mermaidModulePromise = import("mermaid").then(({ default: mermaid }) => {
67-
return mermaid;
68-
});
67+
mermaidModulePromise = import("mermaid")
68+
.then(({ default: mermaid }) => mermaid)
69+
.catch((error) => {
70+
mermaidModulePromise = null;
71+
throw error;
72+
});
6973
}
7074

7175
return mermaidModulePromise;
7276
}
7377

78+
function getMermaidThemeConfig() {
79+
const backgroundColor = getThemeColor("--background-color", "#1e1e1e");
80+
const panelColor = getThemeColor("--popup-background-color", "#2a2f3a");
81+
const borderColor = getThemeColor("--border-color", "#4a4f5a");
82+
const primaryTextColor = getThemeColor("--primary-text-color", "#f5f5f5");
83+
const accentColor = getThemeColor("--link-text-color", "#4ba3ff");
84+
85+
return {
86+
startOnLoad: false,
87+
securityLevel: "strict",
88+
theme: "base",
89+
themeVariables: {
90+
darkMode: isDarkColor(backgroundColor),
91+
background: backgroundColor,
92+
mainBkg: panelColor,
93+
primaryColor: panelColor,
94+
primaryTextColor,
95+
primaryBorderColor: borderColor,
96+
lineColor: primaryTextColor,
97+
secondaryColor: accentColor,
98+
secondaryBorderColor: borderColor,
99+
secondaryTextColor: primaryTextColor,
100+
tertiaryColor: backgroundColor,
101+
tertiaryBorderColor: borderColor,
102+
tertiaryTextColor: primaryTextColor,
103+
clusterBkg: panelColor,
104+
clusterBorder: borderColor,
105+
nodeBorder: borderColor,
106+
edgeLabelBackground: backgroundColor,
107+
},
108+
};
109+
}
110+
111+
function initializeMermaid(mermaid) {
112+
const config = getMermaidThemeConfig();
113+
const signature = JSON.stringify(config);
114+
if (signature === mermaidThemeSignature) return;
115+
mermaid.initialize(config);
116+
mermaidThemeSignature = signature;
117+
}
118+
74119
async function copyText(text) {
75120
if (cordova?.plugins?.clipboard) {
76121
cordova.plugins.clipboard.copy(text);
@@ -302,7 +347,6 @@ function createMarkdownPreview(file) {
302347

303348
codeBlocks.forEach((pre) => {
304349
if (pre.querySelector(".copy-button")) return;
305-
if (pre.querySelector(".mermaid")) return;
306350

307351
pre.style.position = "relative";
308352

@@ -341,35 +385,7 @@ function createMarkdownPreview(file) {
341385

342386
const mermaid = await getMermaid();
343387
if (previewState.disposed || version !== previewState.renderVersion) return;
344-
const backgroundColor = getThemeColor("--background-color", "#1e1e1e");
345-
const panelColor = getThemeColor("--popup-background-color", "#2a2f3a");
346-
const borderColor = getThemeColor("--border-color", "#4a4f5a");
347-
const primaryTextColor = getThemeColor("--primary-text-color", "#f5f5f5");
348-
const accentColor = getThemeColor("--link-text-color", "#4ba3ff");
349-
mermaid.initialize({
350-
startOnLoad: false,
351-
securityLevel: "strict",
352-
theme: "base",
353-
themeVariables: {
354-
darkMode: isDarkColor(backgroundColor),
355-
background: backgroundColor,
356-
mainBkg: panelColor,
357-
primaryColor: panelColor,
358-
primaryTextColor,
359-
primaryBorderColor: borderColor,
360-
lineColor: primaryTextColor,
361-
secondaryColor: accentColor,
362-
secondaryBorderColor: borderColor,
363-
secondaryTextColor: primaryTextColor,
364-
tertiaryColor: backgroundColor,
365-
tertiaryBorderColor: borderColor,
366-
tertiaryTextColor: primaryTextColor,
367-
clusterBkg: panelColor,
368-
clusterBorder: borderColor,
369-
nodeBorder: borderColor,
370-
edgeLabelBackground: backgroundColor,
371-
},
372-
});
388+
initializeMermaid(mermaid);
373389
let index = 0;
374390
await Promise.all(
375391
mermaidBlocks.map(async (block) => {

src/pages/markdownPreview/renderer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function slugify(text) {
1616
return text
1717
.trim()
1818
.toLowerCase()
19-
.replace(/[^a-z0-9]+/g, "-");
19+
.normalize("NFD")
20+
.replace(/[\u0300-\u036f]/g, "")
21+
.replace(/[^\p{L}\p{N}]+/gu, "-")
22+
.replace(/^-+|-+$/g, "");
2023
}
2124

2225
function escapeAttribute(value = "") {

0 commit comments

Comments
 (0)