Module: GovukTechDocs::TableOfContents::Helpers

Includes:
PathHelpers
Defined in:
lib/govuk_tech_docs/table_of_contents/helpers.rb

Instance Method Summary collapse

Methods included from PathHelpers

#get_path_to_resource, #path_to_site_root

Instance Method Details

#list_items_from_headings(html, url: "", max_level: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 46

def list_items_from_headings(html, url: "", max_level: nil)
  headings = HeadingsBuilder.new(html, url).headings

  if headings.none? { |heading| heading.size == 1 }
    raise "No H1 tag found. You have to at least add one H1 heading to the page: #{url}"
  end

  tree = HeadingTreeBuilder.new(headings).tree
  HeadingTreeRenderer.new(tree, max_level:).html
end

#multi_page_table_of_contents(resources, current_page, config, current_page_html = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 38

def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
  resources = sort_resources_stably(
    select_top_level_html_files(resources),
  )

  render_page_tree(resources, current_page, config, current_page_html)
end

#render_page_tree(resources, current_page, config, current_page_html) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 57

def render_page_tree(resources, current_page, config, current_page_html)
  resources = sort_resources_stably(resources)

  output = "<ul>\n"
  resources.each do |resource|
    # Skip from page tree if hide_in_navigation:true frontmatter
    next if resource.data.hide_in_navigation

    # Reuse the generated content for the active page
    # If we generate it twice it increments the heading ids
    content = current_page.url == resource.url && current_page_html ? current_page_html : resource.render(layout: false)
    # Avoid redirect pages
    next if content.include? "http-equiv=refresh"

    # If this page has children, just print the title and recursively
    # render the children. If not, print the heading structure.

    # We avoid printing the children of the root index.html as it is the
    # parent of every other top level file.  We need to take any custom
    # prefix in to consideration when checking for the root index.html.
    # The prefix may be set with or without a trailing slash: make sure
    # it has one for this comparison check.
    home_url =
      if config[:http_prefix].end_with?("/")
        config[:http_prefix]
      else
        "#{config[:http_prefix]}/"
      end

    link_value = get_path_to_resource(config, resource, current_page)
    if resource.children.any? && resource.url != home_url
      output += %(<li><a href="#{link_value}"><span>#{resource.data.title}</span></a>\n)
      output += render_page_tree(resource.children, current_page, config, current_page_html)
      output += "</li>\n"
    else
      output +=
        list_items_from_headings(
          content,
          url: link_value,
          max_level: config[:tech_docs][:max_toc_heading_level],
        )
    end
  end
  output += "</ul>\n"

  output
end

#select_top_level_html_files(resources) ⇒ Object



33
34
35
36
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 33

def select_top_level_html_files(resources)
  resources
    .select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") }
end

#single_page_table_of_contents(html, url: "", max_level: nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 13

def single_page_table_of_contents(html, url: "", max_level: nil)
  output = "<ul>\n"
  output += list_items_from_headings(html, url:, max_level:)
  output += "</ul>\n"

  output
end

#sort_resources_stably(resources) ⇒ Object

Items with a weight appear first, in weight order

if items have equal weight, they stay in the order they appeared (stable sort)

Items without a weight appear after, sorted stably in the order that they are present in the resource list



25
26
27
28
29
30
31
# File 'lib/govuk_tech_docs/table_of_contents/helpers.rb', line 25

def sort_resources_stably(resources)
  resources
    .each.with_index
    .sort_by { |r, index| [r.data.weight ? 0 : 1, r.data.weight || 0, index] }
    .map(&:first)
    .to_a
end