diff --git a/lib/govuk_tech_docs/table_of_contents/helpers.rb b/lib/govuk_tech_docs/table_of_contents/helpers.rb index a4ec3ba5..7950027a 100644 --- a/lib/govuk_tech_docs/table_of_contents/helpers.rb +++ b/lib/govuk_tech_docs/table_of_contents/helpers.rb @@ -19,10 +19,18 @@ def single_page_table_of_contents(html, url: "", max_level: nil) end def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil) + # Determine + home_url = + if config[:http_prefix].end_with?("/") + config[:http_prefix] + else + config[:http_prefix] + "/" + end + # Only parse top level html files # Sorted by weight frontmatter resources = resources - .select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") } + .select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == home_url) } .sort_by { |r| [r.data.weight ? 0 : 1, r.data.weight || 0] } render_page_tree(resources, current_page, config, current_page_html) diff --git a/spec/table_of_contents/helpers_spec.rb b/spec/table_of_contents/helpers_spec.rb index 195a95e1..2377814a 100644 --- a/spec/table_of_contents/helpers_spec.rb +++ b/spec/table_of_contents/helpers_spec.rb @@ -153,7 +153,102 @@ def is_a?(klass) path: "/1/2/3/index.html", metadata: { locals: {} }) - current_page_html = '

Heading one

Heading two

' + current_page_html = '

Heading one

Heading two

'; + + config = { + http_prefix: "/", + tech_docs: { + max_toc_heading_level: 3 + } + } + + expected_multi_page_table_of_contents = %{ + + } + + expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip) + end + + it 'builds a table of contents from several page resources with a custom http prefix configured' do + resources = [] + resources[0] = FakeResource.new('/prefix/index.html', '

Heading one

Heading two

', 10, 'Index'); + resources[1] = FakeResource.new('/prefix/a.html', '

Heading one

Heading two

', 10, 'Sub page A', resources[0]); + resources[2] = FakeResource.new('/prefix/b.html', '

Heading one

Heading two

', 20, 'Sub page B', resources[0]); + resources[0].add_children [resources[1], resources[2]] + + current_page = double("current_page", + data: double("page_frontmatter", description: "The description.", title: "The Title"), + url: "/prefix/index.html", + metadata: { locals: {} }) + + current_page_html = '

Heading one

Heading two

'; + + config = { + http_prefix: "/prefix", + tech_docs: { + max_toc_heading_level: 3 + } + } + + expected_multi_page_table_of_contents = %{ + + } + + expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip) + end + + it 'builds a table of contents from a single page resources' do + resources = [] + resources.push FakeResource.new('/index.html', '

Heading one

Heading two

Heading one

Heading two

Heading one

Heading two

'); + + current_page = double("current_page", + data: double("page_frontmatter", description: "The description.", title: "The Title"), + url: "/index.html", + metadata: { locals: {} }) + + current_page_html = '

Heading one

Heading two

Heading one

Heading two

Heading one

Heading two

'; config = { http_prefix: "/",