Module: YARD::Markdown::HeadingHelper

Includes:
ArefHelper
Defined in:
lib/yard/markdown/heading_helper.rb

Overview

Builds headings and legacy anchors for rendered object sections.

Instance Method Summary collapse

Methods included from ArefHelper

#aref

Methods included from AnchorComponentHelper

#anchor_component

Instance Method Details

#anchor_tag(id) ⇒ String

Builds an HTML anchor tag for a generated id.

Parameters:

  • id (String)

    Anchor id value.

Returns:

  • (String)

    HTML anchor tag.



47
48
49
# File 'lib/yard/markdown/heading_helper.rb', line 47

def anchor_tag(id)
  %(<a id="#{id}"></a>)
end

#anchor_tags_for(object) ⇒ Array<String>

Returns all anchor tags that should be attached to a heading.

Parameters:

  • object (YARD::CodeObjects::Base)

    Object being rendered.

Returns:

  • (Array<String>)

    HTML anchor tags for the object.



29
30
31
32
# File 'lib/yard/markdown/heading_helper.rb', line 29

def anchor_tags_for(object)
  anchors = [aref(object), legacy_aref(object)].compact
  anchors.map { |id| anchor_tag(id) }
end

#heading_with_anchors(heading, object) ⇒ String

Appends the generated anchor tags to a Markdown heading.

Parameters:

  • heading (String)

    Heading text to decorate.

  • object (YARD::CodeObjects::Base)

    Object being rendered.

Returns:

  • (String)

    Heading text with embedded anchor tags.



39
40
41
# File 'lib/yard/markdown/heading_helper.rb', line 39

def heading_with_anchors(heading, object)
  "#{heading} #{anchor_tags_for(object).join(' ')}"
end

#legacy_aref(object) ⇒ String?

Returns the legacy YARD anchor for an object when one exists.

Parameters:

  • object (YARD::CodeObjects::Base)

    Object being rendered.

Returns:

  • (String, nil)

    Legacy anchor id, if supported.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yard/markdown/heading_helper.rb', line 13

def legacy_aref(object)
  type = object.type

  return "#{object.name}-constant" if type == :constant
  return "#{object.name}-classvariable" if type == :classvariable
  return nil unless object.respond_to?(:scope)

  return "#{object.name}-class_method" if object.scope == :class

  "#{object.name}-instance_method"
end