From abf42914f35634687c7c19aeaa3286d57df079c7 Mon Sep 17 00:00:00 2001 From: Gerald Date: Thu, 19 Dec 2024 12:46:09 +0800 Subject: [PATCH] fix: update to markmap v0.18 --- README.md | 8 ++--- mkdocs_markmap/defaults.py | 4 +-- mkdocs_markmap/static_files/mkdocs-markmap.js | 33 ++++++++++++++----- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3a73971..ee04433 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,8 @@ plugins: encoding: utf-8 file_extension: .mm.md d3_version: 7 - lib_version: 0.15.3 - view_version: 0.15.3 + lib_version: 0.18.0 + view_version: 0.18.0 ``` In addition, feel free to define your favourite source urls like this: @@ -89,8 +89,8 @@ plugins: extra_javascript: - https://unpkg.com/d3@7/dist/d3.min.js - - https://unpkg.com/markmap-lib@0.15.3/dist/browser/index.js - - https://unpkg.com/markmap-view@0.15.3/dist/browser/index.js + - https://unpkg.com/markmap-lib@0.18.0/dist/browser/index.js + - https://unpkg.com/markmap-view@0.18.0/dist/browser/index.js ``` ## Troubleshooting diff --git a/mkdocs_markmap/defaults.py b/mkdocs_markmap/defaults.py index af317e2..2757355 100644 --- a/mkdocs_markmap/defaults.py +++ b/mkdocs_markmap/defaults.py @@ -14,12 +14,12 @@ class JsModuleConfig(object): ) MARKMAP_LIB: JsModuleConfig = JsModuleConfig( - version="0.15.4", + version="0.18.0", uri="https://unpkg.com/markmap-lib@{}", ) MARKMAP_VIEW: JsModuleConfig = JsModuleConfig( - version="0.15.4", + version="0.18.0", uri="https://unpkg.com/markmap-view@{}", ) diff --git a/mkdocs_markmap/static_files/mkdocs-markmap.js b/mkdocs_markmap/static_files/mkdocs-markmap.js index ce31c7d..c7d5d44 100644 --- a/mkdocs_markmap/static_files/mkdocs-markmap.js +++ b/mkdocs_markmap/static_files/mkdocs-markmap.js @@ -3,7 +3,25 @@ const assets = transformer.getAssets(); const loading = Promise.all([ assets.styles && markmap.loadCSS(assets.styles), - assets.scripts && markmap.loadJS(assets.scripts), + markmap.loadJS([ + ...transformer.plugins.flatMap( + (plugin) => + plugin.config?.preloadScripts?.map((item) => + item.data.src + ? { + ...item, + data: { + ...item.data, + src: transformer.urlBuilder.getFullUrl( + item.data.src + ), + }, + } + : item + ) || [] + ), + ...(assets.scripts || []), + ]), ]); function parseData(content) { @@ -19,8 +37,9 @@ } function resetMarkmap(m, el) { - const { minX, maxX, minY, maxY } = m.state; - const height = (el.clientWidth * (maxX - minX)) / (maxY - minY); + if (!m.state.rect) return; + const { x1, y1, x2, y2 } = m.state.rect; + const height = (el.offsetWidth / (x2 - x1)) * (y2 - y1); el.style.height = height + "px"; m.fit(); } @@ -44,11 +63,9 @@ el.innerHTML = ""; svg = el.firstChild; const { root, options } = parseData(content); - const m = markmap.Markmap.create(svg, options, root); - resetMarkmap(m, el); - transformer.hooks.retransform.tap(() => { - const { root, options } = parseData(content); - m.setData(root, options); + const m = markmap.Markmap.create(svg, options); + m.setData(root); + requestAnimationFrame(() => { resetMarkmap(m, el); }); }