-
Notifications
You must be signed in to change notification settings - Fork 41
Reject duplicate docs IDs #404
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
Open
Alek99
wants to merge
3
commits into
main
Choose a base branch
from
agent/fix-396-unique-docs-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Fast source-level validation for Markdown-rendered documentation IDs. | ||
|
|
||
| The post-build route validator checks complete HTML, including shared layout, | ||
| raw HTML/SVG, and redirects. | ||
| """ | ||
|
|
||
| from collections import Counter | ||
| from collections.abc import Iterator, Sequence | ||
| from typing import Any | ||
|
|
||
| from reflex_site_shared.docs.content import discover_docs | ||
| from xy_docs.config import DOCS_CONFIG | ||
| from xy_docs.markdown import render_xy_markdown_page | ||
|
|
||
|
|
||
| def _static_string(value: Any) -> str | None: | ||
| """Return a Python string from a literal Reflex value.""" | ||
| if isinstance(value, str): | ||
| return value | ||
| literal = getattr(value, "_var_value", None) | ||
| return literal if isinstance(literal, str) else None | ||
|
|
||
|
|
||
| def _walk_component_tree(component: object) -> Iterator[object]: | ||
| """Yield a rendered Reflex component tree in document order.""" | ||
| yield component | ||
| for child in getattr(component, "children", ()): | ||
| yield from _walk_component_tree(child) | ||
|
|
||
|
|
||
| def duplicate_ids(components: Sequence[object]) -> tuple[str, ...]: | ||
| """Return duplicate static IDs from a rendered component sequence.""" | ||
| static_ids = tuple( | ||
| component_id | ||
| for component in components | ||
| if (component_id := _static_string(getattr(component, "id", None))) | ||
| ) | ||
| return tuple(component_id for component_id, count in Counter(static_ids).items() if count > 1) | ||
|
|
||
|
|
||
| def validate_public_page_ids() -> None: | ||
| """Raise when Markdown-rendered page content has duplicate static IDs.""" | ||
| failures: dict[str, tuple[str, ...]] = {} | ||
| for page in discover_docs(DOCS_CONFIG): | ||
|
Alek99 marked this conversation as resolved.
|
||
| components = tuple(_walk_component_tree(render_xy_markdown_page(page))) | ||
|
Alek99 marked this conversation as resolved.
|
||
| duplicates = duplicate_ids(components) | ||
| if duplicates: | ||
| failures[page.route] = duplicates | ||
| if failures: | ||
| raise RuntimeError(f"duplicate static IDs: {failures}") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Validate literal IDs produced from all Markdown-backed pages.""" | ||
| validate_public_page_ids() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """Regression coverage for public documentation element IDs.""" | ||
|
|
||
| import importlib.util | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
|
|
||
| DOCS_APP_ROOT = Path(__file__).resolve().parent.parent | ||
| CHECK_DUPLICATE_IDS_PATH = DOCS_APP_ROOT / "scripts" / "check_duplicate_ids.py" | ||
| SPEC = importlib.util.spec_from_file_location( | ||
| "xy_docs_check_duplicate_ids", | ||
| CHECK_DUPLICATE_IDS_PATH, | ||
| ) | ||
| if SPEC is None or SPEC.loader is None: | ||
| msg = f"Unable to load duplicate-ID validator: {CHECK_DUPLICATE_IDS_PATH}" | ||
| raise RuntimeError(msg) | ||
| check_duplicate_ids = importlib.util.module_from_spec(SPEC) | ||
| SPEC.loader.exec_module(check_duplicate_ids) | ||
|
|
||
|
|
||
| def test_markdown_page_bodies_have_unique_literal_ids() -> None: | ||
| """Keep Markdown-rendered page content free of duplicate literal IDs.""" | ||
| result = subprocess.run( | ||
| [sys.executable, str(CHECK_DUPLICATE_IDS_PATH)], | ||
| cwd=DOCS_APP_ROOT, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=120, | ||
| check=False, | ||
| ) | ||
|
|
||
| assert result.returncode == 0, f"{result.stdout}\n{result.stderr}" | ||
|
|
||
|
|
||
| def test_duplicate_id_validator_rejects_repeated_literal_ids() -> None: | ||
| """Reject two rendered components with the same literal ID.""" | ||
| duplicate = SimpleNamespace(_var_value="duplicate") | ||
| first = SimpleNamespace(id=duplicate) | ||
| second = SimpleNamespace(id=duplicate) | ||
|
|
||
| assert check_duplicate_ids.duplicate_ids((first, second)) == ("duplicate",) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.