Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to scan and register HTML anchors #20

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import contextlib
import functools
import logging
from typing import TYPE_CHECKING, Any, Callable, Sequence
import re
from typing import Callable, Dict, Optional, Sequence
from urllib.parse import urlsplit

from mkdocs.config import Config
from mkdocs.config.config_options import Type
from mkdocs.plugins import BasePlugin

from mkdocs_autorefs.references import AutorefsExtension, fix_refs, relative_url
Expand Down Expand Up @@ -49,8 +52,12 @@ class AutorefsPlugin(BasePlugin):
for more information about its plugin system.
"""

config = (
("scan_html_tags", Type(bool, default=False))
)

scan_toc: bool = True
current_page: str | None = None
current_page: Optional[str] = None

def __init__(self) -> None:
"""Initialize the object."""
Expand Down Expand Up @@ -170,6 +177,12 @@ def on_page_content(self, html: str, page: Page, **kwargs: Any) -> str: # noqa:
log.debug(f"Mapping identifiers to URLs for page {page.file.src_path}")
for item in page.toc.items:
self.map_urls(page.url, item)

if self.config["scan_html_tags"]:
# Matches any html anchors with the id property (e.g. <a id="xx">)
for match in re.findall(r"""<a .*?id=["']([_\w-]*)["'].*?>""", html):
self.register_anchor(page.url, match)

return html

def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
Expand Down
Loading