Class: Coradoc::Markdown::Heading

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/markdown/model/heading.rb

Overview

Heading model representing a Markdown heading (# to ######).

Examples:

Create a heading

heading = Coradoc::Markdown::Heading.new(level: 1, text: "Title")

Instance Method Summary collapse

Methods inherited from Base

visit, #visit

Instance Method Details

#auto_idString

Generate an auto ID from the heading text

Examples:

Heading.new(text: "Hello World!").auto_id #=> "hello-world"

Returns:

  • (String)

    A slugified version of the text suitable for use as an ID



19
20
21
22
23
24
25
26
27
28
# File 'lib/coradoc/markdown/model/heading.rb', line 19

def auto_id
  return '' if text.nil? || text.empty?

  # Downcase, replace non-alphanumeric with hyphens, collapse multiple hyphens
  slug = text.to_s
             .downcase
             .gsub(/[^a-z0-9]+/, '-')
             .gsub(/^-+|-+$/, '')
  slug.empty? ? 'section' : slug
end

#heading_idString

Get the ID for this heading (uses explicit id if set, otherwise auto_id)

Returns:

  • (String)

    The ID to use for this heading



33
34
35
# File 'lib/coradoc/markdown/model/heading.rb', line 33

def heading_id
  id || auto_id
end