Skip to content

Commit 3adf6a7

Browse files
authored
Merge pull request #54 from markmap/fix/spa
fix: render markmaps in SPA
2 parents 35e4776 + dec0c9e commit 3adf6a7

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

mkdocs_markmap/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def on_page_content(self, html: str, page: Page, **kwargs) -> str:
127127
code = markmap
128128
pre.name = "div"
129129
pre["class"] = pre.get("class", []) + ["mkdocs-markmap"]
130-
code.name = "script"
131-
code["type"] = "text/template"
130+
code.name = "markmap-data"
131+
code.attrs["hidden"] = "true"
132132

133133
return str(soup)

mkdocs_markmap/static_files/mkdocs-markmap.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@
3838
});
3939
}
4040

41-
function updateMarkmaps() {
42-
const markmaps = document.getElementsByClassName('mkdocs-markmap');
43-
for (var i = 0; i < markmaps.length; i++) {
44-
const el = markmaps[i];
41+
function updateMarkmaps(node) {
42+
for (const el of node.querySelectorAll('.mkdocs-markmap')) {
4543
renderMarkmap(el);
4644
}
4745
}
4846

4947
loading.then(() => {
50-
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
51-
var observer = new MutationObserver(function(mutations) {
52-
updateMarkmaps();
48+
const observer = new MutationObserver((mutationList) => {
49+
for (const mutation of mutationList) {
50+
if (mutation.type === 'childList') {
51+
for (const node of mutation.addedNodes) {
52+
updateMarkmaps(node);
53+
}
54+
}
55+
}
5356
});
5457

55-
var target = document.getElementById('mkdocs-decrypted-content');
56-
if (undefined != target) {
57-
observer.observe(target, { childList: true });
58-
}
58+
observer.observe(document.body, { childList: true });
5959

60-
updateMarkmaps();
60+
updateMarkmaps(document);
6161
});
6262
})();

0 commit comments

Comments
 (0)