Class: Coradoc::Html::TocSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/toc_serializer.rb

Overview

Serializes a document’s TOC structure to JSON for inline embedding.

Used by the SPA layout to provide client-side navigation data.

Instance Method Summary collapse

Instance Method Details

#build_json(document, options) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/coradoc/html/toc_serializer.rb', line 9

def build_json(document, options)
  return { entries: [], numbered: false } unless document.is_a?(CoreModel::StructuralElement)

  numbered = options[:section_numbers] == true
  builder = TocBuilder.from_options(options)
  toc = builder.build(document)
  { entries: serialize_entries(toc.entries), numbered: numbered }
end

#serialize_entries(entries) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/coradoc/html/toc_serializer.rb', line 18

def serialize_entries(entries)
  entries.map do |entry|
    {
      id: entry.id,
      title: TitleText.resolve(entry.title),
      number: entry.number,
      level: entry.level,
      children: entry.children.any? ? serialize_entries(entry.children) : []
    }
  end
end