valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
291 stars 72 forks source link

Feature request: container option for linkAfterHeader #110

Closed JanDW closed 2 years ago

JanDW commented 2 years ago

Hi Val,

I noticed that to add a container around the outputted HTML when usinglinkAfterHeader I'd need to implement my own render function. Would it make sense to provide an option for passing in a container?

Best,

Jan

valeriangalliat commented 2 years ago

Hey! Seems like a common demand for linkAfterHeader to include a wrapper in the output, @nhoizey also wanted to do this in #100. I came up with a proposal solution in #111, it looks like this:

const anchor = require('markdown-it-anchor')
const md = require('markdown-it')()

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>']
  })
})
<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>

Does this looks like a reasonable API?

Cheers!

JanDW commented 2 years ago

There are more of us. :)

Does this looks like a reasonable API?

Yes, that was exactly what I was hoping for—thanks!

valeriangalliat commented 2 years ago

Awesome, published with v8.5.0! Thank you for the Ko-fi :heart:

JanDW commented 2 years ago

Thank you! 🏅

nhoizey commented 2 years ago

I'm late using this, but it rocks! Thanks @valeriangalliat