Skip to content

Commit ecb2f33

Browse files
committed
🐛 fix(MarkdownPreview): When the anchor link is not in English, it cannot be jump
1 parent 1dc7599 commit ecb2f33

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/SwashbucklerDiary.Rcl/wwwroot/js/markdown-preview-helper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ function handleA(dotNetCallbackRef, element) {
3030
event.preventDefault();
3131
const url = location.origin + location.pathname + location.search + href;
3232
dotNetCallbackRef.invokeMethodAsync('NavigateToReplace', url);
33+
34+
const targetElement = document.getElementById(href.substring(1));
35+
if (targetElement) {
36+
setTimeout(() => {
37+
targetElement.scrollIntoView();
38+
}, 100);
39+
}
3340
}
3441
});
3542
}

src/SwashbucklerDiary.Rcl/wwwroot/js/vditor-preview-helper.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
handlePreviewElement(element);
1515
handleAnchorScroll();
1616
fixToc(element);
17+
fixAnchorLink(element);
1718
dotNetCallbackRef.invokeMethodAsync('After');
1819
}
1920
}
@@ -104,3 +105,14 @@ function fixToc(element) {
104105
}
105106
});
106107
}
108+
109+
function fixAnchorLink(element) {
110+
element.querySelectorAll("a").forEach(a => {
111+
const href = a.getAttribute('href');
112+
if (!href || !href.startsWith('#')) {
113+
return;
114+
}
115+
116+
a.href = decodeURIComponent(href);
117+
});
118+
}

0 commit comments

Comments
 (0)