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 1 commit
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
Next Next commit
added scan html tags
  • Loading branch information
tvdboom authored and pawamoy committed Feb 16, 2024
commit 8685763ccfb29788a5471c56b1b1968cc781194a
12 changes: 10 additions & 2 deletions src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
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.plugins import BasePlugin
Expand Down Expand Up @@ -50,7 +51,8 @@ class AutorefsPlugin(BasePlugin):
"""

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

def __init__(self) -> None:
"""Initialize the object."""
Expand Down Expand Up @@ -170,6 +172,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.scan_html_tags:
# Matches any html tag with the name property
for match in re.findall(r"""<(\w+?) .*?name=["']([\w-]*)["'].*?>.*?</\1>""", html):
self.register_anchor(page.url, match[1])

return html

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