Skip to content

Commit a5c2f07

Browse files
committed
Fix to always add id to footnote heading
Previusly, since 1dc8ab3, it was possible to customize the properties generated on the `h2` element in the footnote section. That commit made it possible to remove the `id` attribute. However, other steps in the process require that that `id` attribute, with the expected value, exists. Due to that, broken, inaccessible HTML could be generated. This is now fixed. Related-to: GH-64.
1 parent d97bcbd commit a5c2f07

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

lib/footer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ export function footer(h) {
110110
{
111111
type: 'element',
112112
tagName: h.footnoteLabelTagName,
113-
properties: JSON.parse(JSON.stringify(h.footnoteLabelProperties)),
113+
properties: {
114+
...JSON.parse(JSON.stringify(h.footnoteLabelProperties)),
115+
id: 'footnote-label'
116+
},
114117
children: [u('text', h.footnoteLabel)]
115118
},
116119
{type: 'text', value: '\n'},

lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* @property {string} [footnoteLabelTagName='h2']
7979
* HTML tag to use for the footnote label.
8080
* Can be changed to match your document structure and play well with your choice of css.
81-
* @property {Properties} [footnoteLabelProperties={id: 'footnote-label', className: ['sr-only']}]
81+
* @property {Properties} [footnoteLabelProperties={className: ['sr-only']}]
8282
* Properties to use on the footnote label.
8383
* A 'sr-only' class is added by default to hide this from sighted users.
8484
* Change it to make the label visible, or add classes for other purposes.
@@ -130,7 +130,6 @@ function factory(tree, options) {
130130
h.footnoteLabel = settings.footnoteLabel || 'Footnotes'
131131
h.footnoteLabelTagName = settings.footnoteLabelTagName || 'h2'
132132
h.footnoteLabelProperties = settings.footnoteLabelProperties || {
133-
id: 'footnote-label',
134133
className: ['sr-only']
135134
}
136135
h.footnoteBackLabel = settings.footnoteBackLabel || 'Back to content'

readme.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ Change it when the markdown is not in English.
175175
###### `options.footnoteLabelTagName`
176176

177177
HTML tag to use for the footnote label (`string`, default: `h2`).
178-
Can be changed to match your document structure and play well with your
179-
choice of css.
178+
Can be changed to match your document structure and play well with your CSS.
180179

181180
> 👉 **Note**: this option affects footnotes.
182181
> Footnotes are not specified by CommonMark.
@@ -185,12 +184,12 @@ choice of css.
185184
186185
###### `options.footnoteLabelProperties`
187186

188-
Properties to use on the footnote label (`object`, default: `{id:
189-
'footnote-label', className: ['sr-only']}`).
187+
Properties to use on the footnote label (`object`, default:
188+
`{className: ['sr-only']}`).
189+
Importantly, `id: 'footnote-label'` is always added, because footnote calls use
190+
it with `aria-describedby` to provide an accessible label.
190191
A `sr-only` class is added by default to hide this from sighted users.
191192
Change it to make the label visible, or add classes for other purposes.
192-
Provide an object with no `className` or no `id` to have a footnote label with
193-
either no class or no id, or an empty object to have none.
194193

195194
> 👉 **Note**: this option affects footnotes.
196195
> Footnotes are not specified by CommonMark.
@@ -368,7 +367,7 @@ console.log(html)
368367

369368
```html
370369
<p>Bonjour<sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>
371-
<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
370+
<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
372371
<ol>
373372
<li id="user-content-fn-1">
374373
<p>Monde! <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>
@@ -402,8 +401,8 @@ footnotes so that screen reader users can properly pronounce the page:
402401
```diff
403402
@@ -1,8 +1,8 @@
404403
<p>Bonjour<sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>
405-
-<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
406-
+<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Notes de bas de page</h2>
404+
-<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
405+
+<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Notes de bas de page</h2>
407406
<ol>
408407
<li id="user-content-fn-1">
409408
-<p>Monde! <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>

test/footnote.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ test('Footnote', (t) => {
362362
</tr>
363363
</tbody>
364364
</table>
365-
<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
365+
<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
366366
<ol>
367367
<li id="user-content-fn-1">
368368
<p>a <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>
@@ -400,7 +400,7 @@ test('Footnote', (t) => {
400400
</tr>
401401
</thead>
402402
</table>
403-
<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
403+
<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
404404
<ol>
405405
<li id="user-content-fn-1">
406406
<p>a <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>
@@ -424,7 +424,7 @@ test('Footnote', (t) => {
424424
)
425425
),
426426
`<p>Call<sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup><sup><a href="#user-content-fn-1" id="user-content-fnref-1-2" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>
427-
<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
427+
<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
428428
<ol>
429429
<li id="user-content-fn-1">
430430
<p>Recursion<sup><a href="#user-content-fn-1" id="user-content-fnref-1-3" data-footnote-ref aria-describedby="footnote-label">1</a></sup><sup><a href="#user-content-fn-1" id="user-content-fnref-1-4" data-footnote-ref aria-describedby="footnote-label">1</a></sup> <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a> <a href="#user-content-fnref-1-2" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩<sup>2</sup></a> <a href="#user-content-fnref-1-3" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩<sup>3</sup></a> <a href="#user-content-fnref-1-4" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩<sup>4</sup></a></p>
@@ -445,7 +445,7 @@ test('Footnote', (t) => {
445445
{footnoteLabel: 'Voetnoten', footnoteBackLabel: 'Terug naar de inhoud'}
446446
)
447447
),
448-
'<p><sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Voetnoten</h2>\n<ol>\n<li id="user-content-fn-1">\n<p>a <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Terug naar de inhoud">↩</a></p>\n</li>\n</ol>\n</section>',
448+
'<p><sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Voetnoten</h2>\n<ol>\n<li id="user-content-fn-1">\n<p>a <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Terug naar de inhoud">↩</a></p>\n</li>\n</ol>\n</section>',
449449
'should support `footnoteLabel`, `footnoteBackLabel`'
450450
)
451451

@@ -460,7 +460,7 @@ test('Footnote', (t) => {
460460
{clobberPrefix: ''}
461461
)
462462
),
463-
'<p><sup><a href="#fn-1" id="fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>\n<ol>\n<li id="fn-1">\n<p>a <a href="#fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>\n</li>\n</ol>\n</section>',
463+
'<p><sup><a href="#fn-1" id="fnref-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>\n<ol>\n<li id="fn-1">\n<p>a <a href="#fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content">↩</a></p>\n</li>\n</ol>\n</section>',
464464
'should support an empty `clobberPrefix`'
465465
)
466466

@@ -475,7 +475,7 @@ test('Footnote', (t) => {
475475
{footnoteLabelTagName: 'h1'}
476476
)
477477
),
478-
/<h1 id="footnote-label" class="sr-only">Footnotes<\/h1>/,
478+
/<h1 class="sr-only" id="footnote-label">Footnotes<\/h1>/,
479479
'should support a `footnoteLabelTagName`'
480480
)
481481

@@ -490,7 +490,7 @@ test('Footnote', (t) => {
490490
{footnoteLabelProperties: {}}
491491
)
492492
),
493-
/<h2>Footnotes<\/h2>/,
493+
/<h2 id="footnote-label">Footnotes<\/h2>/,
494494
'should support a `footnoteLabelProperties`'
495495
)
496496

0 commit comments

Comments
 (0)