Class: Uniword::Validation::Checkers::InternalLinkChecker
- Inherits:
-
LinkChecker
- Object
- LinkChecker
- Uniword::Validation::Checkers::InternalLinkChecker
- Defined in:
- lib/uniword/validation/checkers/internal_link_checker.rb
Overview
Validates internal bookmark links and anchors.
Responsibility: Validate internal document bookmarks exist. Single Responsibility: Internal link validation only.
Verifies:
-
Bookmark targets exist in document
-
Anchor names are valid
-
Case sensitivity (configurable)
Configuration options:
-
case_sensitive: Whether bookmark names are case-sensitive
-
check_heading_links: Whether to check heading bookmark references
Constant Summary collapse
- DEFAULTS =
Default configuration values
{ case_sensitive: false, check_heading_links: true, }.freeze
Instance Attribute Summary
Attributes inherited from LinkChecker
Instance Method Summary collapse
-
#can_check?(link) ⇒ Boolean
Check if this checker can validate the given link.
-
#check(link, document = nil) ⇒ ValidationResult
Validate the internal link.
Methods inherited from LinkChecker
Constructor Details
This class inherits a constructor from Uniword::Validation::LinkChecker
Instance Method Details
#can_check?(link) ⇒ Boolean
Check if this checker can validate the given link.
41 42 43 44 45 46 47 48 49 |
# File 'lib/uniword/validation/checkers/internal_link_checker.rb', line 41 def can_check?(link) return false unless enabled? return false unless link.respond_to?(:anchor) # Internal links have anchor but no URL link.anchor && !link.respond_to?(:url).then do |has_url| has_url ? !link.url : true end end |
#check(link, document = nil) ⇒ ValidationResult
Validate the internal link.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/uniword/validation/checkers/internal_link_checker.rb', line 59 def check(link, document = nil) unless enabled? return ValidationResult.unknown(link, "Checker disabled") end unless document return ValidationResult.warning( link, "Cannot validate without document context", ) end anchor = link.anchor unless anchor return ValidationResult.failure(link, "No anchor specified") end # Get bookmarks from document bookmarks = extract_bookmarks(document) # Check if bookmark exists if bookmark_exists?(anchor, bookmarks) ValidationResult.success( link, metadata: { anchor: anchor, bookmark_count: bookmarks.size }, ) else # Try to find similar bookmarks for suggestions suggestions = find_similar_bookmarks(anchor, bookmarks) = "Bookmark not found: #{anchor}" += ". Did you mean: #{suggestions.join(', ')}?" if suggestions.any? ValidationResult.failure( link, , metadata: { anchor: anchor, suggestions: suggestions }, ) end end |