Skip to content

Commit 3d4babc

Browse files
committed
fix: support custom assets for markmap
1 parent 8679dd5 commit 3d4babc

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,27 @@ plugins:
7272
base_path: docs
7373
encoding: utf-8
7474
file_extension: .mm.md
75-
d3_version: 6.7.0
76-
lib_version: 0.14.1
77-
view_version: 0.14.0
75+
d3_version: 7
76+
lib_version: 0.15.3
77+
view_version: 0.15.3
7878
```
7979
8080
In addition, feel free to define your favourite source urls like this:
8181
8282
```yaml
83+
plugins:
84+
- markmap:
85+
# disable the default assets first
86+
d3_version: ''
87+
lib_version: ''
88+
view_version: ''
89+
8390
extra_javascript:
84-
- https://unpkg.com/d3@6.7.0/dist/d3.min.js
85-
- https://unpkg.com/markmap-lib@0.14.1/dist/browser/index.min.js
86-
- https://unpkg.com/markmap-view@0.14.0/dist/index.min.js
91+
- 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
8794
```
8895
89-
:warning: The urls need to contain one of these keywords to be considered as deviation from default:
90-
91-
* `markmap-d3`
92-
* `markmap-lib`
93-
* `markmap-view`
94-
9596
## Troubleshooting
9697
9798
### Nav tree lists markmaps

mkdocs_markmap/defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class JsModuleConfig(object):
99
uri: str = attr.attrib()
1010

1111
D3_LIB: JsModuleConfig = JsModuleConfig(
12-
version='6.7.0',
13-
uri='https://unpkg.com/d3@{}/dist/d3.min.js',
12+
version='7',
13+
uri='https://unpkg.com/d3@{}',
1414
)
1515

1616
MARKMAP_LIB: JsModuleConfig = JsModuleConfig(

mkdocs_markmap/plugin.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,10 @@ def markmap(self) -> Dict[str, str]:
4444
"""
4545
Provides all markmap libraries defined in mkdocs.yml (if any)
4646
"""
47-
if self._markmap is None:
48-
extra_javascript = self.config.get('extra_javascript', [])
47+
if self._markmap is None:
4948
self._markmap: Dict[str, str] = {}
50-
for uri in extra_javascript:
51-
for name in MARKMAP.keys():
52-
if f'markmap-{name}' in uri.lower():
53-
self._markmap[name] = uri
54-
5549
for name, module in MARKMAP.items():
56-
if name not in self._markmap:
50+
if self.config[f'{name}_version']:
5751
self._markmap[name] = module.uri.format(self.config[f'{name}_version'])
5852

5953
return self._markmap
@@ -62,15 +56,15 @@ def _load_scripts(self, soup: BeautifulSoup, script_base_url: str, js_path: Path
6256
for script_url in self.markmap.values():
6357
if script_url.lower().startswith("http"):
6458
try:
65-
src: str = script_base_url + download(js_path, script_url)
59+
src: str = script_base_url + download(js_path, script_url, extname='.js')
6660
except Exception as e:
6761
log.error(f'unable to download script: {script_url}')
6862
src = script_url
6963

7064
else:
7165
log.info(f"static script detected: {script_url}")
7266
src = script_url
73-
67+
7468
script: Tag = soup.new_tag('script', src=src, type='text/javascript')
7569
soup.head.append(script)
7670

@@ -85,7 +79,7 @@ def _add_statics(soup: BeautifulSoup):
8579
tag: Tag = soup.new_tag(tag_name, type=text_type)
8680
with open(path, 'r') as fp:
8781
tag.string = fp.read()
88-
getattr(soup, attribute).append(tag)
82+
getattr(soup, attribute).append(tag)
8983

9084
def on_config(self, config: Config) -> Config:
9185
config['markdown_extensions'].append('markmap')

mkdocs_markmap/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import logging
22
import os
33
from pathlib import Path
4-
54
from urllib.parse import unquote
6-
from requests.adapters import HTTPAdapter
5+
76
from requests import Response
7+
from requests.adapters import HTTPAdapter
88
from requests.packages.urllib3.util.retry import Retry
99
from requests.packages.urllib3.util.url import Url, parse_url
1010
from requests.sessions import Session
@@ -13,12 +13,15 @@
1313
log = logging.getLogger('mkdocs.markmap')
1414

1515

16-
def download(base_path: Path, url: str, flat: bool = False, force_reload: bool = False) -> str:
16+
def download(base_path: Path, url: str, flat: bool = False, force_reload: bool = False, extname: str = '') -> str:
1717
parsed_url: Url = parse_url(url)
1818
path: str = unquote(parsed_url.request_uri.split('?')[0])
1919
sub_path: str = os.path.basename(path) if flat else f'{parsed_url.hostname}{path}'
20+
if extname != '' and not sub_path.endswith(extname):
21+
sub_path += extname
2022
file_path: Path = base_path / sub_path
2123

24+
2225
file_path.parent.mkdir(parents=True, exist_ok=True)
2326
if force_reload or not file_path.exists():
2427
retries: Retry = Retry(connect=5, read=2, redirect=5)

0 commit comments

Comments
 (0)