Skip to content

Commit

Permalink
#26: Moves html adjustment to earlier extension hook
Browse files Browse the repository at this point in the history
  • Loading branch information
neatc0der committed Oct 16, 2021
1 parent 05c8fe5 commit 89d71c1
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mkdocs_markmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MarkmapPlugin(BasePlugin):

def __init__(self):
self._markmap: Dict[str, str] = None
self._found_markmap: bool = False

@property
def markmap(self) -> Dict[str, str]:
Expand Down Expand Up @@ -91,20 +92,24 @@ def on_config(self, config: Config) -> Config:

return config

def on_post_page(self, output_content: str, config: Config, **kwargs) -> str:
soup: BeautifulSoup = BeautifulSoup(output_content, 'html.parser')
page: Page = kwargs.get('page')

markmaps: ResultSet = soup.find_all(class_='language-markmap')
if not any(markmaps):
def on_post_page(self, html: str, page: Page, config: Config, **kwargs) -> str:
if not self._found_markmap:
log.info(f"no markmap found: {page.file.name}")
return output_content
return html

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

return str(soup)

def on_page_content(self, html: str, **kwargs) -> str:
soup: BeautifulSoup = BeautifulSoup(html, 'html.parser')
markmaps: ResultSet = soup.find_all(class_='language-markmap')
self._found_markmap: bool = any(markmaps)

for index, markmap in enumerate(markmaps):
markmap: Tag
tag_id: str = f'markmap-{index}'
Expand Down

0 comments on commit 89d71c1

Please sign in to comment.