Module: YARD::Markdown::HeadingHelper

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

Overview

Builds headings and legacy anchors for rendered object sections.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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.



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

def self.legacy_aref(object)
  name = object.name

  case object.type
  when :constant
    "#{name}-constant"
  when :classvariable
    "#{name}-classvariable"
  when :method
    "#{name}-#{(object.scope == :class) ? "class" : "instance"}_method"
  end
end

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.



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

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.



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

def anchor_tags_for(object)
  anchors = [ArefHelper.aref(object), HeadingHelper.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.



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

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