Skip to content

Commit

Permalink
Reset UniqueIdentifierGenerator between each render
Browse files Browse the repository at this point in the history
Prevents IDs to be misinterpreted as duplicated across the rendering of different pages sharing headings
  • Loading branch information
romaricpascal committed Nov 21, 2022
1 parent 298bf7d commit d9beef0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/govuk_tech_docs/tech_docs_html_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def initialize(options = {})
super
end

def preprocess(document)
UniqueIdentifierGenerator.instance.reset

document
end

def paragraph(text)
@app.api("<p>#{text.strip}</p>\n")
end
Expand Down
16 changes: 15 additions & 1 deletion spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def hello_world
expect(output).to include('<h1 id="a-heading">A heading</h1>')
expect(output).to include('<h2 id="a-subheading">A subheading</h2>')
end

it "Ensures IDs are unique among headings in the page" do
output = processor.render <<~MARKDOWN
# A heading
Expand All @@ -130,5 +130,19 @@ def hello_world
# Finally the last occurence will get a prefix, but no number as it's in a different section
expect(output).to include('<h3 id="another-subheading-a-shared-heading">A shared heading</h3>')
end

it "Does not consider unique IDs across multiple renders" do
processor.render <<~MARKDOWN
# A heading
## A subheading
MARKDOWN

second_output = processor.render <<~MARKDOWN
# A heading
## A subheading
MARKDOWN

expect(second_output).to include('<h2 id="a-subheading">A subheading</h2>')
end
end
end

0 comments on commit d9beef0

Please sign in to comment.