Class: Uniword::Validation::Checkers::InternalLinkChecker

Inherits:
LinkChecker
  • Object
show all
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

Examples:

Create and use checker

checker = InternalLinkChecker.new(config: {
  case_sensitive: false
})
result = checker.check(hyperlink, document)

Constant Summary collapse

DEFAULTS =

Default configuration values

{
  case_sensitive: false,
  check_heading_links: true,
}.freeze

Instance Attribute Summary

Attributes inherited from LinkChecker

#config

Instance Method Summary collapse

Methods inherited from LinkChecker

#initialize

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.

Examples:

checker.can_check?(hyperlink) # => true

Parameters:

  • link (Object)

    The link to check

Returns:

  • (Boolean)

    true if link has an anchor but no URL



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.

Examples:

result = checker.check(hyperlink, document)

Parameters:

  • link (Object)

    The link to validate

  • document (Object) (defaults to: nil)

    The document containing bookmarks

Returns:



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)
    message = "Bookmark not found: #{anchor}"
    message += ". Did you mean: #{suggestions.join(', ')}?" if suggestions.any?

    ValidationResult.failure(
      link,
      message,
      metadata: { anchor: anchor, suggestions: suggestions },
    )
  end
end