MCPcopy Create free account
hub / github.com/Azure/static-web-apps-cli / toc_data

Function toc_data

e2e/samples/app/slate/lib/toc_data.rb:3–31  ·  view source on GitHub ↗
(page_content)

Source from the content-addressed store, hash-verified

1require 'nokogiri'
2
3def toc_data(page_content)
4 html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content)
5
6 # get a flat list of headers
7 headers = []
8 html_doc.css('h1, h2, h3').each do |header|
9 headers.push({
10 id: header.attribute('id').to_s,
11 content: header.children,
12 title: header.children.to_s.gsub(/<[^>]*>/, ''),
13 level: header.name[1].to_i,
14 children: []
15 })
16 end
17
18 [3,2].each do |header_level|
19 header_to_nest = nil
20 headers = headers.reject do |header|
21 if header[:level] == header_level
22 header_to_nest[:children].push header if header_to_nest
23 true
24 else
25 header_to_nest = header if header[:level] < header_level
26 false
27 end
28 end
29 end
30 headers
31end

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…