Skip to content

Commit 4fd4a60

Browse files
committed
Fix missing <ul> in single page navigation
Since the changes made in 30471a9, there’s now no outer <ul> when `multipage_nav` is false – it jumps straight from the `<nav>` to the `<li>`. Because `multi_page_table_of_contents ` calls `single_page_table_of_contents` via `render_page_tree`, it's not as simple as adding the extra `<ul>` to the existing `single_page_table_of_contents` method, otherwise you end up with duplicate `<ul>` elements in some cases. Instead, rename the existing `single_page_table_of_contents` to create a new function `list_items_from_headings `. The `list_items_from_headings` method can be called by both `render_page_tree` and a new `single_page_table_of_contents` method which maintains the existing API. This new `single_page_table_of_contents` method can then add the wrapper without affecting the output of `multi_page_table_of_contents`.
1 parent 8375d93 commit 4fd4a60

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/govuk_tech_docs/table_of_contents/helpers.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ module GovukTechDocs
88
module TableOfContents
99
module Helpers
1010
def single_page_table_of_contents(html, url: "", max_level: nil)
11-
headings = HeadingsBuilder.new(html, url).headings
12-
13-
if headings.none? { |heading| heading.size == 1 }
14-
raise "No H1 tag found. You have to at least add one H1 heading to the page: " + url
15-
end
11+
output = "<ul>\n"
12+
output += list_items_from_headings(html, url: url, max_level: max_level)
13+
output += "</ul>\n"
1614

17-
tree = HeadingTreeBuilder.new(headings).tree
18-
HeadingTreeRenderer.new(tree, max_level: max_level).html
15+
output
1916
end
2017

2118
def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
@@ -28,6 +25,17 @@ def multi_page_table_of_contents(resources, current_page, config, current_page_h
2825
render_page_tree(resources, current_page, config, current_page_html)
2926
end
3027

28+
def list_items_from_headings(html, url: "", max_level: nil)
29+
headings = HeadingsBuilder.new(html, url).headings
30+
31+
if headings.none? { |heading| heading.size == 1 }
32+
raise "No H1 tag found. You have to at least add one H1 heading to the page: " + url
33+
end
34+
35+
tree = HeadingTreeBuilder.new(headings).tree
36+
HeadingTreeRenderer.new(tree, max_level: max_level).html
37+
end
38+
3139
def render_page_tree(resources, current_page, config, current_page_html)
3240
# Sort by weight frontmatter
3341
resources = resources
@@ -69,7 +77,7 @@ def render_page_tree(resources, current_page, config, current_page_html)
6977
output += "</li>\n"
7078
else
7179
output +=
72-
single_page_table_of_contents(
80+
list_items_from_headings(
7381
content,
7482
url: resource.url,
7583
max_level: config[:tech_docs][:max_toc_heading_level],

spec/table_of_contents/helpers_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Subject
1818
}
1919

2020
expected_single_page_table_of_contents = %{
21+
<ul>
2122
<li>
2223
<a href="#fruit"><span>Fruit</span></a>
2324
</li>
@@ -29,6 +30,7 @@ class Subject
2930
</li>
3031
</ul>
3132
</li>
33+
</ul>
3234
}
3335

3436
expect(subject.single_page_table_of_contents(html).strip).to eq(expected_single_page_table_of_contents.strip)
@@ -45,6 +47,7 @@ class Subject
4547
}
4648

4749
expected_single_page_table_of_contents = %{
50+
<ul>
4851
<li>
4952
<a href="#fruit"><span>Fruit</span></a>
5053
<ul>
@@ -65,6 +68,7 @@ class Subject
6568
<li>
6669
<a href="#bread"><span>Bread</span></a>
6770
</li>
71+
</ul>
6872
}
6973

7074
expect(subject.single_page_table_of_contents(html).strip).to eq(expected_single_page_table_of_contents.strip)

0 commit comments

Comments
 (0)