Skip to content

Commit 298bf7d

Browse files
committed
Add test for generation of heading IDs while rendering markdown
1 parent 6c6abde commit 298bf7d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/govuk_tech_docs/tech_docs_html_renderer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "middleman-core/renderers/redcarpet"
2+
require "govuk_tech_docs/unique_identifier_generator"
23

34
module GovukTechDocs
45
class TechDocsHTMLRenderer < Middleman::Renderers::MiddlemanRedcarpetHTML

spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,40 @@ def hello_world
9595
end
9696
end
9797
end
98+
99+
describe "#render unique heading IDs" do
100+
# Reset the UniqueIdentifierGenerator between each tests to ensure independence
101+
before(:each) { GovukTechDocs::UniqueIdentifierGenerator.instance.reset }
102+
103+
it "Automatically assigns an ID to the heading" do
104+
output = processor.render <<~MARKDOWN
105+
# A heading
106+
## A subheading
107+
MARKDOWN
108+
109+
expect(output).to include('<h1 id="a-heading">A heading</h1>')
110+
expect(output).to include('<h2 id="a-subheading">A subheading</h2>')
111+
end
112+
113+
it "Ensures IDs are unique among headings in the page" do
114+
output = processor.render <<~MARKDOWN
115+
# A heading
116+
## A subheading
117+
### A shared heading
118+
### A shared heading
119+
### A shared heading
120+
## Another subheading
121+
### A shared heading
122+
MARKDOWN
123+
124+
# Fist heading will get its ID as a normal heading
125+
expect(output).to include('<h3 id="a-shared-heading">A shared heading</h3>')
126+
# Second occurence will be prefixed by the previous heading to ensure uniqueness
127+
expect(output).to include('<h3 id="a-subheading-a-shared-heading">A shared heading</h3>')
128+
# Third occurence will be get the prefix, as well as a numeric suffix, to keep ensuring uniqueness
129+
expect(output).to include('<h3 id="a-subheading-a-shared-heading-2">A shared heading</h3>')
130+
# Finally the last occurence will get a prefix, but no number as it's in a different section
131+
expect(output).to include('<h3 id="another-subheading-a-shared-heading">A shared heading</h3>')
132+
end
133+
end
98134
end

0 commit comments

Comments
 (0)