Skip to content

Commit b93feef

Browse files
committed
Add labelAttributes option
Ported from `markdown-rs`, `mdast-util-to-hast`.
1 parent c4835d8 commit b93feef

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

dev/lib/html.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@
3434
* Change it when the markdown is not in English.
3535
*
3636
* This label is typically hidden visually (assuming a `sr-only` CSS class
37-
* is defined that does that), and thus affects screen readers only.
37+
* is defined that does that) and so affects screen readers only.
38+
* If you do have such a class, but want to show this section to everyone,
39+
* pass different attributes with the `labelAttributes` option.
40+
* @property {string} [labelAttributes='class="sr-only"']
41+
* Attributes to use on the footnote label.
42+
*
43+
* Change it to show the label and add other attributes.
44+
*
45+
* This label is typically hidden visually (assuming an `sr-only` CSS class
46+
* is defined that does that) and so affects screen readers only.
47+
* If you do have such a class, but want to show this section to everyone,
48+
* pass an empty string.
49+
* You can also add different attributes.
50+
*
51+
* > 👉 **Note**: `id="footnote-label"` is always added, because footnote
52+
* > calls use it with `aria-describedby` to provide an accessible label.
3853
* @property {string} [labelTagName='h2']
3954
* HTML tag name to use for the footnote label element.
4055
*
@@ -43,8 +58,7 @@
4358
* This label is typically hidden visually (assuming a `sr-only` CSS class
4459
* is defined that does that) and so affects screen readers only.
4560
* If you do have such a class, but want to show this section to everyone,
46-
* pass different attributes with the `gfm_footnote_label_attributes`
47-
* option.
61+
* pass different attributes with the `labelAttributes` option.
4862
* @property {string} [backLabel='Back to content']
4963
* Textual label to describe the backreference back to footnote calls.
5064
*
@@ -79,9 +93,13 @@ export function gfmFootnoteHtml(options) {
7993
const config = options || emptyOptions
8094
const label = config.label || 'Footnotes'
8195
const labelTagName = config.labelTagName || 'h2'
96+
const labelAttributes =
97+
config.labelAttributes === null || config.labelAttributes === undefined
98+
? 'class="sr-only"'
99+
: config.labelAttributes
82100
const backLabel = config.backLabel || 'Back to content'
83101
const clobberPrefix =
84-
config.clobberPrefix === undefined || config.clobberPrefix === null
102+
config.clobberPrefix === null || config.clobberPrefix === undefined
85103
? 'user-content-'
86104
: config.clobberPrefix
87105
return {
@@ -200,7 +218,9 @@ export function gfmFootnoteHtml(options) {
200218
this.tag(
201219
'<section data-footnotes="" class="footnotes"><' +
202220
labelTagName +
203-
' id="footnote-label" class="sr-only">'
221+
' id="footnote-label"' +
222+
(labelAttributes ? ' ' + labelAttributes : '') +
223+
'>'
204224
)
205225
this.raw(this.encode(label))
206226
this.tag('</' + labelTagName + '>')

readme.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,23 @@ Textual label to use for the footnotes section (`string`, default:
215215
Change it when the markdown is not in English.
216216

217217
This label is typically hidden visually (assuming a `sr-only` CSS class
218-
is defined that does that) and thus affects screen readers only.
218+
is defined that does that) and so affects screen readers only.
219+
220+
###### `labelAttributes`
221+
222+
Attributes to use on the footnote label (`string`, default:
223+
`'class="sr-only"'`).
224+
225+
Change it to show the label and add other attributes.
226+
227+
This label is typically hidden visually (assuming an `sr-only` CSS class
228+
is defined that does that) and so affects screen readers only.
229+
If you do have such a class, but want to show this section to everyone,
230+
pass an empty string.
231+
You can also add different attributes.
232+
233+
> 👉 **Note**: `id="footnote-label"` is always added, because footnote
234+
> calls use it with `aria-describedby` to provide an accessible label.
219235
220236
###### `labelTagName`
221237

@@ -433,7 +449,9 @@ These extensions work with `micromark` version 3+.
433449
## Security
434450

435451
This package is safe.
436-
Setting `clobberPrefix = ''` is dangerous.
452+
Setting `clobberPrefix = ''` is dangerous, it opens you up to DOM clobbering.
453+
The `labelTagName` and `labelAttributes` options are unsafe when used with user
454+
content, they allow defining arbitrary HTML.
437455

438456
## Related
439457

test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ test('markdown -> html (micromark)', () => {
5858
htmlExtensions: [gfmFootnoteHtml({labelTagName: 'h1'})]
5959
}),
6060
'<p>a<sup><a href="#user-content-fn-b" id="user-content-fnref-b" data-footnote-ref="" aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes="" class="footnotes"><h1 id="footnote-label" class="sr-only">Footnotes</h1>\n<ol>\n<li id="user-content-fn-b">\n<p>c <a href="#user-content-fnref-b" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩</a></p>\n</li>\n</ol>\n</section>',
61-
'should support `options.label`, `options.backLabel`'
61+
'should support `options.labelTagName`'
62+
)
63+
64+
assert.deepEqual(
65+
micromark('a[^b]\n\n[^b]: c', {
66+
extensions: [gfmFootnote()],
67+
htmlExtensions: [gfmFootnoteHtml({labelAttributes: ''})]
68+
}),
69+
'<p>a<sup><a href="#user-content-fn-b" id="user-content-fnref-b" data-footnote-ref="" aria-describedby="footnote-label">1</a></sup></p>\n<section data-footnotes="" class="footnotes"><h2 id="footnote-label">Footnotes</h2>\n<ol>\n<li id="user-content-fn-b">\n<p>c <a href="#user-content-fnref-b" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩</a></p>\n</li>\n</ol>\n</section>',
70+
'should support `options.labelAttributes`'
6271
)
6372

6473
assert.deepEqual(

0 commit comments

Comments
 (0)