Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate multipage TOC with http_prefix #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/govuk_tech_docs/table_of_contents/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially extract an helper method:

def determine_home_url(http_prefix)
  http_prefix.end_with?("/") ? http_prefix : "#{http_prefix}/"
end

And then within multi_page_table_of_contents we could use it:

home_url = determine_home_url(config[:http_prefix])

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)
Expand Down
97 changes: 96 additions & 1 deletion spec/table_of_contents/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,102 @@ def is_a?(klass)
path: "/1/2/3/index.html",
metadata: { locals: {} })

current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>'
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';

config = {
http_prefix: "/",
tech_docs: {
max_toc_heading_level: 3
}
}

expected_multi_page_table_of_contents = %{
<ul><li><a href="/index.html">Index</a>
<ul>
<li>
<a href="/a.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/a.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
<ul>
<li>
<a href="/b.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/b.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
</li></ul>
}

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', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Index');
resources[1] = FakeResource.new('/prefix/a.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Sub page A', resources[0]);
resources[2] = FakeResource.new('/prefix/b.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 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 = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';

config = {
http_prefix: "/prefix",
tech_docs: {
max_toc_heading_level: 3
}
}

expected_multi_page_table_of_contents = %{
<ul><li><a href="/prefix/index.html">Index</a>
<ul>
<li>
<a href="/prefix/a.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/prefix/a.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
<ul>
<li>
<a href="/prefix/b.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/prefix/b.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
</li></ul>
}

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', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>');

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

current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';

config = {
http_prefix: "/",
Expand Down