Class: Coradoc::Markdown::Heading
- Defined in:
- lib/coradoc/markdown/model/heading.rb
Overview
Heading model representing a Markdown heading (# to ######).
Instance Method Summary collapse
-
#auto_id ⇒ String
Generate an auto ID from the heading text.
-
#heading_id ⇒ String
Get the ID for this heading (uses explicit id if set, otherwise auto_id).
-
#initialize(level: 1, text: '') ⇒ Heading
constructor
A new instance of Heading.
Methods inherited from Base
#serialize_content, #to_h, #to_md, visit, #visit
Constructor Details
#initialize(level: 1, text: '') ⇒ Heading
Returns a new instance of Heading.
14 15 16 17 18 |
# File 'lib/coradoc/markdown/model/heading.rb', line 14 def initialize(level: 1, text: '') super() @level = level @text = text end |
Instance Method Details
#auto_id ⇒ String
Generate an auto ID from the heading text
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coradoc/markdown/model/heading.rb', line 25 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_id ⇒ String
Get the ID for this heading (uses explicit id if set, otherwise auto_id)
39 40 41 |
# File 'lib/coradoc/markdown/model/heading.rb', line 39 def heading_id id || auto_id end |