Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wrapping output of linkAfterHeader #111

Merged
merged 1 commit into from Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Support wrapping output of `linkAfterHeader`. ([#100], [#110])

## [8.4.1] - 2021-10-11
* Attempt to fix `npm publish` that didn't publish previous version.
Expand Down Expand Up @@ -305,5 +306,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#103]: https://github.com/valeriangalliat/markdown-it-anchor/issues/103
[#106]: https://github.com/valeriangalliat/markdown-it-anchor/pull/106
[#107]: https://github.com/valeriangalliat/markdown-it-anchor/issues/107
[#110]: https://github.com/valeriangalliat/markdown-it-anchor/issues/110

[`6fcc502`]: https://github.com/valeriangalliat/markdown-it-anchor/commit/6fcc50233d593458aa883e5b515cb8311114555c
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -286,6 +286,7 @@ text from the visual experience.
| `visuallyHiddenClass` | The class you use to make an element visually hidden. | `undefined`, required for `visually-hidden` style |
| `space` | Add a space between the assistive text and the permalink symbol. | `true` |
| `placement` | Placement of the permalink symbol relative to the assistive text, can be `before` or `after` the header. | `after` |
| `wrapper` | Opening and closing wrapper string, e.g. `['<div class="wrapper">', '</div>']`. | `null` |
| | See [common options](#common-options). | |

```js
Expand All @@ -296,17 +297,20 @@ md.use(anchor, {
permalink: anchor.permalink.linkAfterHeader({
style: 'visually-hidden',
assistiveText: title => `Permalink to “${title}”`,
visuallyHiddenClass: 'visually-hidden'
visuallyHiddenClass: 'visually-hidden',
wrapper: ['<div class="wrapper">', '</div>']
})
})
```

```html
<h2 id="title">Title</h2>
<a class="header-anchor" href="#title">
<span class="visually-hidden">Permalink to “Title”</span>
<span aria-hidden="true">#</span>
</a>
<div class="wrapper">
<h2 id="title">Title</h2>
<a class="header-anchor" href="#title">
<span class="visually-hidden">Permalink to “Title”</span>
<span aria-hidden="true">#</span>
</a>
</div>
```

By using a visually hidden element for the assistive text, we make sure
Expand Down
13 changes: 12 additions & 1 deletion permalink.js
Expand Up @@ -207,10 +207,21 @@ export const linkAfterHeader = makePermalink((slug, opts, anchorOpts, state, idx
]

state.tokens.splice(idx + 3, 0, ...linkTokens)

if (opts.wrapper) {
state.tokens.splice(idx, 0, Object.assign(new state.Token('html_block', '', 0), {
content: opts.wrapper[0] + '\n'
}))

state.tokens.splice(idx + 3 + linkTokens.length + 1, 0, Object.assign(new state.Token('html_block', '', 0), {
content: opts.wrapper[1] + '\n'
}))
}
})

Object.assign(linkAfterHeader.defaults, {
style: 'visually-hidden',
space: true,
placement: 'after'
placement: 'after',
wrapper: null
})
14 changes: 14 additions & 0 deletions test.js
Expand Up @@ -383,6 +383,20 @@ nest('permalink.linkAfterHeader', test => {
'<div class="wrapper">\n<h1 id="h1" tabindex="-1">H1</h1>\n<a class="header-anchor" href="#h1"><span class="visually-hidden">Permalink to “H1”</span> <span aria-hidden="true">#</span></a></div>\n'
)
})

test('custom native wrapper', t => {
t.is(
md().use(anchor, {
permalink: anchor.permalink.linkAfterHeader({
style: 'visually-hidden',
assistiveText: title => `Permalink to “${title}”`,
visuallyHiddenClass: 'visually-hidden',
wrapper: ['<div class="wrapper">', '</div>']
})
}).render('# H1'),
'<div class="wrapper">\n<h1 id="h1" tabindex="-1">H1</h1>\n<a class="header-anchor" href="#h1"><span class="visually-hidden">Permalink to “H1”</span> <span aria-hidden="true">#</span></a></div>\n'
)
})
})

nest('tokens', test => {
Expand Down