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 missing <ul> in single page navigation #251

Merged
merged 1 commit into from
Jul 29, 2021
Merged
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
24 changes: 16 additions & 8 deletions lib/govuk_tech_docs/table_of_contents/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ module GovukTechDocs
module TableOfContents
module Helpers
def single_page_table_of_contents(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
output = "<ul>\n"
output += list_items_from_headings(html, url: url, max_level: max_level)
output += "</ul>\n"

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

def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
Expand All @@ -28,6 +25,17 @@ def multi_page_table_of_contents(resources, current_page, config, current_page_h
render_page_tree(resources, current_page, config, current_page_html)
end

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: max_level).html
end

def render_page_tree(resources, current_page, config, current_page_html)
# Sort by weight frontmatter
resources = resources
Expand Down Expand Up @@ -69,7 +77,7 @@ def render_page_tree(resources, current_page, config, current_page_html)
output += "</li>\n"
else
output +=
single_page_table_of_contents(
list_items_from_headings(
content,
url: resource.url,
max_level: config[:tech_docs][:max_toc_heading_level],
Expand Down
4 changes: 4 additions & 0 deletions spec/table_of_contents/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Subject
}

expected_single_page_table_of_contents = %{
<ul>
<li>
<a href="#fruit"><span>Fruit</span></a>
</li>
Expand All @@ -29,6 +30,7 @@ class Subject
</li>
</ul>
</li>
</ul>
}

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

expected_single_page_table_of_contents = %{
<ul>
<li>
<a href="#fruit"><span>Fruit</span></a>
<ul>
Expand All @@ -65,6 +68,7 @@ class Subject
<li>
<a href="#bread"><span>Bread</span></a>
</li>
</ul>
}

expect(subject.single_page_table_of_contents(html).strip).to eq(expected_single_page_table_of_contents.strip)
Expand Down