Skip to content

Commit 89d71c1

Browse files
author
neatc0der
committed
#26: Moves html adjustment to earlier extension hook
1 parent 05c8fe5 commit 89d71c1

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

mkdocs_markmap/plugin.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class MarkmapPlugin(BasePlugin):
3737

3838
def __init__(self):
3939
self._markmap: Dict[str, str] = None
40+
self._found_markmap: bool = False
4041

4142
@property
4243
def markmap(self) -> Dict[str, str]:
@@ -91,20 +92,24 @@ def on_config(self, config: Config) -> Config:
9192

9293
return config
9394

94-
def on_post_page(self, output_content: str, config: Config, **kwargs) -> str:
95-
soup: BeautifulSoup = BeautifulSoup(output_content, 'html.parser')
96-
page: Page = kwargs.get('page')
97-
98-
markmaps: ResultSet = soup.find_all(class_='language-markmap')
99-
if not any(markmaps):
95+
def on_post_page(self, html: str, page: Page, config: Config, **kwargs) -> str:
96+
if not self._found_markmap:
10097
log.info(f"no markmap found: {page.file.name}")
101-
return output_content
98+
return html
10299

100+
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
103101
script_base_url: str = re.sub(r'[^/]+?/', '../', re.sub(r'/+?', '/', page.url)) + 'js/'
104102
js_path: Path = Path(config['site_dir']) / 'js'
105103
self._load_scripts(soup, script_base_url, js_path)
106104
self._add_statics(soup)
107105

106+
return str(soup)
107+
108+
def on_page_content(self, html: str, **kwargs) -> str:
109+
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
110+
markmaps: ResultSet = soup.find_all(class_='language-markmap')
111+
self._found_markmap: bool = any(markmaps)
112+
108113
for index, markmap in enumerate(markmaps):
109114
markmap: Tag
110115
tag_id: str = f'markmap-{index}'

0 commit comments

Comments
 (0)