Skip to content

Commit ce73d4f

Browse files
committed
Reset UniqueIdentifierGenerator between each render
Prevents IDs to be misinterpreted as duplicated across the rendering of different pages sharing headings
1 parent 298bf7d commit ce73d4f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/govuk_tech_docs/tech_docs_html_renderer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def initialize(options = {})
1111
super
1212
end
1313

14+
def preprocess(document)
15+
UniqueIdentifierGenerator.instance.reset
16+
17+
document
18+
end
19+
1420
def paragraph(text)
1521
@app.api("<p>#{text.strip}</p>\n")
1622
end

spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def hello_world
109109
expect(output).to include('<h1 id="a-heading">A heading</h1>')
110110
expect(output).to include('<h2 id="a-subheading">A subheading</h2>')
111111
end
112-
112+
113113
it "Ensures IDs are unique among headings in the page" do
114114
output = processor.render <<~MARKDOWN
115115
# A heading
@@ -130,5 +130,19 @@ def hello_world
130130
# Finally the last occurence will get a prefix, but no number as it's in a different section
131131
expect(output).to include('<h3 id="another-subheading-a-shared-heading">A shared heading</h3>')
132132
end
133+
134+
it "Does not consider unique IDs across multiple renders" do
135+
first_output = processor.render <<~MARKDOWN
136+
# A heading
137+
## A subheading
138+
MARKDOWN
139+
140+
second_output = processor.render <<~MARKDOWN
141+
# A heading
142+
## A subheading
143+
MARKDOWN
144+
145+
expect(second_output).to include('<h2 id="a-subheading">A subheading</h2>')
146+
end
133147
end
134148
end

0 commit comments

Comments
 (0)