/
MudBlazor.Markdown.js
57 lines (46 loc) · 1.43 KB
/
MudBlazor.Markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import hljs from "highlight.js";
const codeStylesDir = "code-styles";
const codeStylesSegment = `/MudBlazor.Markdown/${codeStylesDir}/`;
window.highlightCodeElement = function (element, lang) {
hljs.highlightElement(element, { language: lang });
}
window.setHighlightStylesheet = function (stylesheetPath) {
let isFound = false;
const stylesheets = document.querySelectorAll("link[rel='stylesheet']");
for (let i = 0; i < stylesheets.length; i++) {
const href = stylesheets[i].getAttribute("href");
if (!href) {
continue;
}
const index = href.indexOf(codeStylesSegment);
if (index !== -1) {
if (!isFound) {
isFound = true;
const newHref = href.substring(0, index + codeStylesSegment.length) + stylesheetPath;
stylesheets[i].setAttribute("href", newHref);
} else {
stylesheets[i].remove();
}
}
}
if (!isFound) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = `_content/MudBlazor.Markdown/${codeStylesDir}/${stylesheetPath}`;
document.head.appendChild(link);
}
}
window.scrollToElementId = function (elementId) {
const element = document.getElementById(elementId);
if (element) {
const elementIdHref = `#${elementId}`;
if (!window.location.pathname.endsWith(elementIdHref)) {
history.replaceState(null, "", window.location.pathname + elementIdHref);
}
element.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest"
});
}
}