Skip to content

Commit 3888b81

Browse files
neatc0dergera2ld
andauthored
Release/2.5.0 (#64)
* fix: encode markmap source as base64 to avoid being handled by other plugins * fix: update to markmap v0.18 * chore: add editorconfig * Add changeling for v2.5.0.md * Bump version to 2.5.0 --------- Co-authored-by: Gerald <[email protected]>
1 parent dc844b1 commit 3888b81

File tree

8 files changed

+66
-37
lines changed

8 files changed

+66
-37
lines changed

.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 4
4+
quote_type = double

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ plugins:
7373
encoding: utf-8
7474
file_extension: .mm.md
7575
d3_version: 7
76-
lib_version: 0.15.3
77-
view_version: 0.15.3
76+
lib_version: 0.18
77+
view_version: 0.18
7878
```
7979
8080
In addition, feel free to define your favourite source urls like this:
@@ -89,8 +89,8 @@ plugins:
8989
9090
extra_javascript:
9191
- https://unpkg.com/d3@7/dist/d3.min.js
92-
- https://unpkg.com/markmap-lib@0.15.3/dist/browser/index.js
93-
- https://unpkg.com/markmap-view@0.15.3/dist/browser/index.js
92+
- https://unpkg.com/markmap-lib@0.18/dist/browser/index.iife.js
93+
- https://unpkg.com/markmap-view@0.18/dist/browser/index.js
9494
```
9595
9696
## Troubleshooting

changelog/v2.5.0.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# v2.5.0
2+
3+
* Bump version of `markmap` to v0.18
4+
* Fix of issue that other plugins broke `markmap` (see #63)

mkdocs_markmap/__meta__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PACKAGE_NAME: str = "mkdocs_markmap"
22
PROJECT_NAME: str = PACKAGE_NAME.replace("_", "-")
3-
PROJECT_VERSION: str = "2.4.3"
3+
PROJECT_VERSION: str = "2.5.0"
44

55
OWNER: str = "neatc0der"
66
ORGANISATION: str = "markmap"

mkdocs_markmap/defaults.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class JsModuleConfig(object):
1414
)
1515

1616
MARKMAP_LIB: JsModuleConfig = JsModuleConfig(
17-
version="0.15.4",
17+
version="0.18",
1818
uri="https://unpkg.com/markmap-lib@{}",
1919
)
2020

2121
MARKMAP_VIEW: JsModuleConfig = JsModuleConfig(
22-
version="0.15.4",
22+
version="0.18",
2323
uri="https://unpkg.com/markmap-view@{}",
2424
)
2525

mkdocs_markmap/extension.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import logging
23
import re
34
from functools import partial
@@ -48,30 +49,31 @@ def run(self, lines: List[str]) -> List[str]:
4849

4950
included_paths.append(path)
5051
try:
51-
with open(path, "r", encoding=self.encoding) as r:
52-
markmap: List[str] = r.readlines()
53-
52+
markmap: str = path.read_text(encoding=self.encoding)
53+
5454
except Exception as e:
5555
log.error("unable to include file {}. Ignoring statement. Error: {}".format(path, e))
5656
lines[loc] = INCLUDE_SYNTAX.sub("",line)
5757
break
5858

5959
line_split: List[str] = INCLUDE_SYNTAX.split(line)
60+
output: List[str] = []
6061
if len(markmap) == 0:
61-
markmap.append("")
62+
output.append("")
6263
else:
63-
markmap.insert(0, "```markmap")
64-
markmap.append("```")
65-
64+
output.append('<pre class="language-markmap"><code encoding="base64">')
65+
output.append(base64.b64encode(markmap.encode()).decode())
66+
output.append("</code></pre>")
67+
6668
if line_split[0].strip() != "":
67-
markmap.insert(0, line_split[0])
69+
output.insert(0, line_split[0])
6870

6971
if line_split[2].strip() != "":
70-
markmap.append(line_split[2])
72+
output.append(line_split[2])
7173

72-
lines = lines[:loc] + markmap + lines[loc+1:]
74+
lines = lines[:loc] + output + lines[loc+1:]
7375
break
74-
76+
7577
else:
7678
done = True
7779

mkdocs_markmap/plugin.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import base64
12
import logging
23
from pathlib import Path
34
import re
45
from typing import Dict, Tuple
56

67
from bs4 import BeautifulSoup, ResultSet, Tag
7-
88
from mkdocs.config.base import Config, load_config
99
from mkdocs.config.config_options import Type as PluginType
1010
from mkdocs.plugins import BasePlugin
1111
from mkdocs.structure.pages import Page
12+
1213
from mkdocs_markmap.extension import MarkmapExtension
1314

1415
from .defaults import MARKMAP
@@ -116,7 +117,6 @@ def on_page_content(self, html: str, page: Page, **kwargs) -> str:
116117

117118
for index, markmap in enumerate(markmaps):
118119
markmap: Tag
119-
tag_id: str = f"markmap-{index}"
120120
pre: Tag
121121
code: Tag
122122
if markmap.name == "pre":
@@ -129,5 +129,9 @@ def on_page_content(self, html: str, page: Page, **kwargs) -> str:
129129
pre["class"] = pre.get("class", []) + ["mkdocs-markmap"]
130130
code.name = "markmap-data"
131131
code.attrs["hidden"] = "true"
132+
if not code.attrs.get("encoding"):
133+
# Encode content as base64 to avoid being handled by other plugins like KaTeX
134+
code.attrs["encoding"] = "base64"
135+
code.string = base64.b64encode(code.get_text().strip().encode()).decode()
132136

133137
return str(soup)

mkdocs_markmap/static_files/mkdocs-markmap.js

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
(function initializeMarkmap() {
22
const transformer = new markmap.Transformer();
3+
const preloadAssets = transformer.getPreloadScripts();
34
const assets = transformer.getAssets();
45
const loading = Promise.all([
56
assets.styles && markmap.loadCSS(assets.styles),
6-
assets.scripts && markmap.loadJS(assets.scripts),
7+
markmap.loadJS([...preloadAssets.scripts, ...assets.scripts]),
78
]);
89

910
function parseData(content) {
1011
const { root, frontmatter } = transformer.transform(content);
1112
let options = markmap.deriveOptions(frontmatter?.markmap);
12-
options = Object.assign({
13-
fitRatio: 0.85,
14-
}, options);
13+
options = Object.assign(
14+
{
15+
fitRatio: 0.85,
16+
},
17+
options
18+
);
1519
return { root, options };
1620
}
1721

1822
function resetMarkmap(m, el) {
19-
const { minX, maxX, minY, maxY } = m.state;
20-
const height = el.clientWidth * (maxX - minX) / (maxY - minY);
23+
if (!m.state.rect) return;
24+
const { x1, y1, x2, y2 } = m.state.rect;
25+
const height = (el.offsetWidth / (x2 - x1)) * (y2 - y1);
2126
el.style.height = height + "px";
2227
m.fit();
2328
}
2429

30+
function decodeBase64(encoded) {
31+
const binary = atob(encoded);
32+
const bytes = new Uint8Array(binary.length);
33+
for (let i = 0; i < bytes.length; i++) {
34+
bytes[i] = binary.charCodeAt(i);
35+
}
36+
return new TextDecoder().decode(bytes);
37+
}
38+
2539
function renderMarkmap(el) {
26-
let svg = el.querySelector('svg');
27-
if (svg) return;
28-
const content = el.textContent;
29-
el.innerHTML = '<svg>';
40+
const dataEl = el.querySelector("markmap-data");
41+
if (!dataEl) return;
42+
let content = el.textContent;
43+
if (dataEl.getAttribute("encoding") === "base64") {
44+
content = decodeBase64(content);
45+
}
46+
el.innerHTML = "<svg>";
3047
svg = el.firstChild;
3148
const { root, options } = parseData(content);
32-
const m = markmap.Markmap.create(svg, options, root);
33-
resetMarkmap(m, el);
34-
transformer.hooks.retransform.tap(() => {
35-
const { root, options } = parseData(content);
36-
m.setData(root, options);
49+
const m = markmap.Markmap.create(svg, options);
50+
m.setData(root);
51+
requestAnimationFrame(() => {
3752
resetMarkmap(m, el);
3853
});
3954
}
4055

4156
function updateMarkmaps(node) {
42-
for (const el of node.querySelectorAll('.mkdocs-markmap')) {
57+
for (const el of node.querySelectorAll(".mkdocs-markmap")) {
4358
renderMarkmap(el);
4459
}
4560
}
4661

4762
loading.then(() => {
4863
const observer = new MutationObserver((mutationList) => {
4964
for (const mutation of mutationList) {
50-
if (mutation.type === 'childList') {
65+
if (mutation.type === "childList") {
5166
for (const node of mutation.addedNodes) {
5267
updateMarkmaps(node);
5368
}

0 commit comments

Comments
 (0)