From dec0c9ecef1322ae3c8828ca37c63f1dac758431 Mon Sep 17 00:00:00 2001 From: Gerald Date: Wed, 4 Oct 2023 14:46:32 +0800 Subject: [PATCH] fix: render markmaps in SPA and fix compatibility issue with mkdocs-material close #42 --- mkdocs_markmap/plugin.py | 4 ++-- mkdocs_markmap/static_files/mkdocs-markmap.js | 24 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/mkdocs_markmap/plugin.py b/mkdocs_markmap/plugin.py index b7f00e5..b11239e 100644 --- a/mkdocs_markmap/plugin.py +++ b/mkdocs_markmap/plugin.py @@ -127,7 +127,7 @@ def on_page_content(self, html: str, page: Page, **kwargs) -> str: code = markmap pre.name = "div" pre["class"] = pre.get("class", []) + ["mkdocs-markmap"] - code.name = "script" - code["type"] = "text/template" + code.name = "markmap-data" + code.attrs["hidden"] = "true" return str(soup) diff --git a/mkdocs_markmap/static_files/mkdocs-markmap.js b/mkdocs_markmap/static_files/mkdocs-markmap.js index 048bcae..3715fc7 100644 --- a/mkdocs_markmap/static_files/mkdocs-markmap.js +++ b/mkdocs_markmap/static_files/mkdocs-markmap.js @@ -38,25 +38,25 @@ }); } - function updateMarkmaps() { - const markmaps = document.getElementsByClassName('mkdocs-markmap'); - for (var i = 0; i < markmaps.length; i++) { - const el = markmaps[i]; + function updateMarkmaps(node) { + for (const el of node.querySelectorAll('.mkdocs-markmap')) { renderMarkmap(el); } } loading.then(() => { - var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; - var observer = new MutationObserver(function(mutations) { - updateMarkmaps(); + const observer = new MutationObserver((mutationList) => { + for (const mutation of mutationList) { + if (mutation.type === 'childList') { + for (const node of mutation.addedNodes) { + updateMarkmaps(node); + } + } + } }); - var target = document.getElementById('mkdocs-decrypted-content'); - if (undefined != target) { - observer.observe(target, { childList: true }); - } + observer.observe(document.body, { childList: true }); - updateMarkmaps(); + updateMarkmaps(document); }); })();