refactor: migrate autorefs MkDocs plugin replacement

Signed-off-by: squidfunk <martin.donath@squidfunk.com>
This commit is contained in:
squidfunk
2026-09-01 18:35:54 +02:00
parent 3a390c7647
commit 83b0fd7541
19 changed files with 1558 additions and 522 deletions
+26
View File
@@ -174,6 +174,32 @@ Not indexed.
assert _read_index(disabled)["items"] == []
def test_search_exclusion_attribute_is_removed_from_page(
tmp_path: Path,
) -> None:
"""Search pragmas affect the index but do not leak into final HTML."""
config = _write_project(tmp_path, plugins=" - search")
(tmp_path / "docs" / "index.md").write_text(
"""\
# Landing
Visible body.
<div data-search-exclude><p>Hidden body.</p></div>
""",
encoding="utf-8",
)
zensical.build(str(config), _BUILD_OPTIONS)
index = _read_index(tmp_path)
assert "Visible body." in index["items"][0]["text"]
assert "Hidden body." not in index["items"][0]["text"]
page = (tmp_path / "site" / "index.html").read_text()
assert "Hidden body." in page
assert "data-search-exclude" not in page
def test_search_rebuild_replaces_changed_and_removed_pages(
tmp_path: Path,
) -> None:
+40
View File
@@ -396,3 +396,43 @@ custom_dir = "overrides"
captured = capfd.readouterr()
assert "No issues found" in captured.err
def test_cached_template_refreshes_when_page_autoref_changes(
tmp_path: Path,
) -> None:
"""Page-local autoref facts participate in the template cache key."""
docs = tmp_path / "docs"
docs.mkdir()
source = docs / "index.md"
source.write_text(
"# Home\n\n[First title][first-target]\n", encoding="utf-8"
)
(docs / "other.md").write_text(
"# Other\n\n## first-target\n\n## second-target\n",
encoding="utf-8",
)
config = tmp_path / "zensical.toml"
config.write_text(
"""
[project]
site_name = "Test"
[project.plugins.autorefs]
""".lstrip(),
encoding="utf-8",
)
zensical.build(str(config), {"clean": True, "strict": False})
output = (tmp_path / "site" / "index.html").read_text(encoding="utf-8")
assert 'href="other/#first-target">First title</a>' in output
# Both references occupy page-local slot zero. Only their cached facts
# distinguish the template inputs after the Markdown pass.
source.write_text(
"# Home\n\n[Second title][second-target]\n", encoding="utf-8"
)
zensical.build(str(config), {"clean": False, "strict": False})
output = (tmp_path / "site" / "index.html").read_text(encoding="utf-8")
assert 'href="other/#second-target">Second title</a>' in output
assert 'href="other/#first-target">First title</a>' not in output
+39 -1
View File
@@ -29,7 +29,13 @@ from typing import TYPE_CHECKING, Any
import pytest
from tests.unit.extensions.conftest import soup
from zensical.extensions.autorefs import get_autorefs_store, reset
from zensical.extensions.autorefs import (
get_autorefs_inventory_data,
get_autorefs_page_data,
get_autorefs_store,
reset,
)
from zensical.extensions.context import Page
if TYPE_CHECKING:
from collections.abc import Generator
@@ -80,6 +86,38 @@ def _reset_autorefs_store() -> Generator[None, None, None]:
reset()
# ---------------------------------------------------------------------------
# Store
# ---------------------------------------------------------------------------
class TestStore:
"""Tests for page-local fact extraction from the transient store."""
def test_page_data_is_taken_without_consuming_inventory(self) -> None:
"""Page registrations leave the global inventory available."""
store = get_autorefs_store()
page = Page(url="guide/", path="guide.md", meta={})
store.set_page(page)
store.register_anchor(page, "target", title="Target")
store.register_anchor(page, "alias", anchor="target", primary=False)
store.register_url("external", "https://example.com/external")
assert get_autorefs_page_data("guide/") == {
"primary": {"target": ["guide/#target"]},
"secondary": {"alias": ["guide/#target"]},
"titles": {"guide/#target": "Target"},
}
assert get_autorefs_page_data("guide/") == {
"primary": {},
"secondary": {},
"titles": {},
}
assert get_autorefs_inventory_data() == {
"external": "https://example.com/external"
}
# ---------------------------------------------------------------------------
# Inline processor
# ---------------------------------------------------------------------------