Module: JekyllOpenAPI::Anchors

Defined in:
lib/jekyll_openapi/anchors.rb

Overview

Stable, deterministic fragment ids for operations and tags — the counterpart to SchemaRenderer::HTML.anchor for the parts of a reference page the schema renderer does not own.

operationId is optional in OpenAPI, so a template that anchors on it alone emits id="" (and collisions) the day an operation omits it; here the method+path is the fallback. The slug matches Jekyll's default slugify (downcase, non-alphanumeric runs to single hyphens, trimmed) so these are drop-in replacements for a hand-rolled | slugify and keep existing anchor URLs stable.

Class Method Summary collapse

Class Method Details

.operation(op) ⇒ Object

Parameters:

  • op (Hash, String)

    an operation entry (from Document#operations) or a bare operationId



18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll_openapi/anchors.rb', line 18

def self.operation(op)
  id =
    if op.is_a?(Hash) || op.is_a?(Reference)
      dig(op, "operation", "operationId") || dig(op, "operationId") ||
        "#{op["method"]} #{op["path"]}"
    else
      op
    end
  slug(id)
end

.slug(text) ⇒ Object

Jekyll-slugify-compatible (default mode) slug.



36
37
38
# File 'lib/jekyll_openapi/anchors.rb', line 36

def self.slug(text)
  text.to_s.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
end

.tag(tag) ⇒ Object

Parameters:

  • tag (Hash, String)

    a tag object (from Document#tags) or a bare name



30
31
32
33
# File 'lib/jekyll_openapi/anchors.rb', line 30

def self.tag(tag)
  name = (tag.is_a?(Hash) || tag.is_a?(Reference)) ? tag["name"] : tag
  slug(name)
end