Classes
AnchorValidator
Validates explicit anchors and [[#anchor]] cross-references.
Performs two main validations:
1. Dup…
AnchorValidator
Validates explicit anchors and [[#anchor]] cross-references.
Performs two main validations:
- Duplicate anchors: Scans rendered HTML for duplicate id="..." attributes
- Broken references: Validates [[#anchor]] syntax against known anchors
Creation:
health_check.register(AnchorValidator())
Configuration:
In bengal.yaml:
health_check:
validators:
anchors: true # Enable/disable anchor validation
BaseValidatorAttributes
| Name | Type | Description |
|---|---|---|
strict |
— | If True, treat warnings as errors (for CI builds) |
Methods 1
validate
Validate anchors across all site pages.
validate
def validate(self, site: Site, build_context: Any | None = None) -> list[CheckResult]
Validate anchors across all site pages.
Parameters 2
site |
Site |
Site to validate |
build_context |
Any | None |
Optional BuildContext (not used by this validator) |
Returns
List of CheckResults for anchor issueslist[CheckResult]
—
Internal Methods 5
__init__
Initialize anchor validator.
__init__
def __init__(self, strict: bool = False)
Initialize anchor validator.
Parameters 1
strict |
bool |
If True, report duplicate anchors as errors instead of warnings |
_build_valid_anchor_set
Build set of all valid anchor IDs from xref_index.
Combines both explicit anch…
_build_valid_anchor_set
def _build_valid_anchor_set(self, site: Site) -> set[str]
Build set of all valid anchor IDs from xref_index.
Combines both explicit anchors (from {#id} and {target}) and heading anchors for comprehensive validation.
Parameters 1
site |
Site |
Site with built xref_index |
Returns
Set of lowercase anchor IDsset[str]
—
_validate_duplicate_anchors
Check for duplicate anchor IDs within a single page.
Scans rendered HTML for a…
_validate_duplicate_anchors
def _validate_duplicate_anchors(self, page: Any) -> list[CheckResult]
Check for duplicate anchor IDs within a single page.
Scans rendered HTML for all id="..." attributes and reports any duplicates. Duplicate anchors cause navigation issues and invalid HTML.
Parameters 1
page |
Any |
Page to validate |
Returns
List of CheckResults for duplicate anchorslist[CheckResult]
—
_validate_anchor_references
Check that all [[#anchor]] references resolve to existing anchors.
Validates c…
_validate_anchor_references
def _validate_anchor_references(self, page: Any, valid_anchors: set[str]) -> list[CheckResult]
Check that all [[#anchor]] references resolve to existing anchors.
Validates cross-reference syntax against the set of known anchors from the xref_index.
Parameters 2
page |
Any |
Page to validate |
valid_anchors |
set[str] |
Set of valid anchor IDs (lowercase) |
Returns
List of CheckResults for broken referenceslist[CheckResult]
—
_find_similar_anchors
Find anchors similar to the broken reference (for suggestions).
Uses simple su…
_find_similar_anchors
def _find_similar_anchors(self, anchor: str, valid_anchors: set[str]) -> list[str]
Find anchors similar to the broken reference (for suggestions).
Uses simple substring matching to suggest possible fixes.
Parameters 2
anchor |
str |
The broken anchor reference |
valid_anchors |
set[str] |
Set of valid anchors |
Returns
List of up to 3 similar anchor nameslist[str]
—
Functions
create_anchor_validator
Factory function to create an AnchorValidator.
create_anchor_validator
def create_anchor_validator(strict: bool = False) -> AnchorValidator
Factory function to create an AnchorValidator.
Parameters 1
| Name | Type | Default | Description |
|---|---|---|---|
strict |
bool |
False |
If True, report duplicate anchors as errors |
Returns
Configured AnchorValidatorAnchorValidator
—