Skip to content

Commit 044fa75

Browse files
committed
fix: generate multipage TOC with http_prefix
1 parent 207bcb8 commit 044fa75

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

lib/govuk_tech_docs/table_of_contents/helpers.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ def single_page_table_of_contents(html, url: "", max_level: nil)
1919
end
2020

2121
def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
22+
# Determine
23+
home_url =
24+
if config[:http_prefix].end_with?("/")
25+
config[:http_prefix]
26+
else
27+
config[:http_prefix] + "/"
28+
end
29+
2230
# Only parse top level html files
2331
# Sorted by weight frontmatter
2432
resources = resources
25-
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") }
33+
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == home_url) }
2634
.sort_by { |r| [r.data.weight ? 0 : 1, r.data.weight || 0] }
2735

2836
render_page_tree(resources, current_page, config, current_page_html)

spec/table_of_contents/helpers_spec.rb

+96-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,102 @@ def is_a?(klass)
153153
path: "/1/2/3/index.html",
154154
metadata: { locals: {} })
155155

156-
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>'
156+
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';
157+
158+
config = {
159+
http_prefix: "/",
160+
tech_docs: {
161+
max_toc_heading_level: 3
162+
}
163+
}
164+
165+
expected_multi_page_table_of_contents = %{
166+
<ul><li><a href="/index.html">Index</a>
167+
<ul>
168+
<li>
169+
<a href="/a.html#heading-one">Heading one</a>
170+
<ul>
171+
<li>
172+
<a href="/a.html#heading-two">Heading two</a>
173+
</li>
174+
</ul>
175+
</li>
176+
</ul>
177+
<ul>
178+
<li>
179+
<a href="/b.html#heading-one">Heading one</a>
180+
<ul>
181+
<li>
182+
<a href="/b.html#heading-two">Heading two</a>
183+
</li>
184+
</ul>
185+
</li>
186+
</ul>
187+
</li></ul>
188+
}
189+
190+
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
191+
end
192+
193+
it 'builds a table of contents from several page resources with a custom http prefix configured' do
194+
resources = []
195+
resources[0] = FakeResource.new('/prefix/index.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Index');
196+
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]);
197+
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]);
198+
resources[0].add_children [resources[1], resources[2]]
199+
200+
current_page = double("current_page",
201+
data: double("page_frontmatter", description: "The description.", title: "The Title"),
202+
url: "/prefix/index.html",
203+
metadata: { locals: {} })
204+
205+
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';
206+
207+
config = {
208+
http_prefix: "/prefix",
209+
tech_docs: {
210+
max_toc_heading_level: 3
211+
}
212+
}
213+
214+
expected_multi_page_table_of_contents = %{
215+
<ul><li><a href="/prefix/index.html">Index</a>
216+
<ul>
217+
<li>
218+
<a href="/prefix/a.html#heading-one">Heading one</a>
219+
<ul>
220+
<li>
221+
<a href="/prefix/a.html#heading-two">Heading two</a>
222+
</li>
223+
</ul>
224+
</li>
225+
</ul>
226+
<ul>
227+
<li>
228+
<a href="/prefix/b.html#heading-one">Heading one</a>
229+
<ul>
230+
<li>
231+
<a href="/prefix/b.html#heading-two">Heading two</a>
232+
</li>
233+
</ul>
234+
</li>
235+
</ul>
236+
</li></ul>
237+
}
238+
239+
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
240+
end
241+
242+
it 'builds a table of contents from a single page resources' do
243+
resources = []
244+
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>');
245+
246+
current_page = double("current_page",
247+
data: double("page_frontmatter", description: "The description.", title: "The Title"),
248+
url: "/index.html",
249+
metadata: { locals: {} })
250+
251+
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>';
157252

158253
config = {
159254
http_prefix: "/",

0 commit comments

Comments
 (0)