-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
New IdAttributePlugin
will add id
attributes to your headings
#3363
Comments
<h1>Hello</h1>
<h2 id="already-has-an-id">Subheading</h1> becomes <h1 id="hello">Hello</h1>
<h2 id="already-has-an-id">Subheading</h1> Simple usageimport { IdAttributePlugin } from "@11ty/eleventy";
export default function (eleventyConfig) {
eleventyConfig.addPlugin(IdAttributePlugin);
}; Full options:import { IdAttributePlugin } from "@11ty/eleventy";
export default function (eleventyConfig) {
eleventyConfig.addPlugin(IdAttributePlugin, {
// custom slugify function, otherwise we use Eleventy’s built-in `slugify` filter.
slugify: function(textContent) {
// …
},
selector: "h1,h2,h3,h4,h5,h6", // default
});
}; Duplicate headings are de-duped using |
id
Attribute plugin will add id
attributes to your headings (multi-template-language support)IdAttributePlugin
will add id
attributes to your headings
(Has the added benefit of removing the unnecessary link markup from your RSS feeds too!) |
- use new IdAttributePlugin, makes “use of the existing posthtml infrastructure used by the HTML base plugin and the input path to URL plugins to add” slugified `id` attrs. Has the added benefit of removing the unnecessary link markup from your RSS feeds too! 11ty/eleventy#3363 - rm markdownItAnchor and comment out header-anchor css - eleventeen.png instead of eight-million.jpg in thirdpost.md - upd eleventy + eleventy-plugin-rss 9.2.20-alpha.17
- use new IdAttributePlugin, makes “use of the existing posthtml infrastructure used by the HTML base plugin and the input path to URL plugins to add” slugified `id` attrs. Has the added benefit of removing the unnecessary link markup from your RSS feeds too! 11ty/eleventy#3363 - rm markdownItAnchor and comment out header-anchor css - eleventeen.png instead of eight-million.jpg in thirdpost.md - upd eleventy + eleventy-plugin-rss 9.2.20-alpha.17
- use new IdAttributePlugin, makes “use of the existing posthtml infrastructure used by the HTML base plugin and the input path to URL plugins to add” slugified `id` attrs. Has the added benefit of removing the unnecessary link markup from your RSS feeds too! 11ty/eleventy#3363 - rm markdownItAnchor and comment out header-anchor css - eleventeen.png instead of eight-million.jpg in thirdpost.md - upd eleventy + eleventy-plugin-rss 9.2.20-alpha.17 * trunk: eleventy 3.0.0-alpha.17, IdAttributePlugin, eleventeen.png (#39)
At least until [#40](#40) finds resolution, building on the work in [#39](#39), adding [daviddarnes/heading-anchors](https://github.com/daviddarnes/heading-anchors) per rec in [11ty/eleventy #3363](11ty/eleventy#3363) Used `position="beforeend"` and styled with a slight adjustment to [demo-styling], adding `vertical-align: bottom;` to `a[href^="#"]` and constraining styles to `heading-anchors` element. demo-styling: https://github.com/daviddarnes/heading-anchors/blob/main/demo-styling.html#L11-L25
* add @daviddarnes/heading-anchors At least until [#40](#40) finds resolution, building on the work in [#39](#39), adding [daviddarnes/heading-anchors](https://github.com/daviddarnes/heading-anchors) per rec in [11ty/eleventy #3363](11ty/eleventy#3363) Used `position="beforeend"` and styled with a slight adjustment to [demo-styling], adding `vertical-align: bottom;` to `a[href^="#"]` and constraining styles to `heading-anchors` element. [demo-styling]: https://github.com/daviddarnes/heading-anchors/blob/main/demo-styling.html#L11-L25 * move heading-anchors code to base.njk instead of post.njk - add hover + focus styles for anchor links - delete deprecated css - 9.2.21-alpha.17
* add @daviddarnes/heading-anchors At least until #40 finds resolution, building on the work in #39 adding https://github.com/daviddarnes/heading-anchors per rec in 11ty/eleventy#3363 Used `position="beforeend"` and styled with a slight adjustment to https://github.com/daviddarnes/heading-anchors/blob/main/demo-styling.html#L11-L25 adding `vertical-align: bottom;` to `a[href^="#"]` and constraining styles to `heading-anchors` element. * move heading-anchors code to base.njk instead of post.njk - add hover + focus styles for anchor links - delete deprecated css
Temporary docs preview url deploying: https://11ty-website-git-v3-11ty.vercel.app/docs/plugins/id-attribute/ |
Just to note this enhancement seems to add |
Mainly leaving something here in case others expand on the use of this plugin for generating headings with self-linking anchors. Is there a way to use this plugin (which I'd like as it can add IDs to all HTML not just Markdown) and have some manner of injected HTML in a heading? I currently use markdown-it-anchor not only to add the IDs but also to add some custom markup inside anchors, most notably an anchor link pointing to the heading's ID. I can see using a custom element for this but I don't want to add any JavaScript to my site as this can be done at build time. My own experiment so far: In the following Eleventy config, I stripped out every one of my other settings and left only what I added now to add anchors to existing headings combining the IdAttributePlugin with a cheerio-based Eleventy transform. In my setup, this allows me to throw out markdown-it-anchor so technically, I don't need to customize the Note that I only tried replicating my existing setup for anchors here. It's likely that adding the anchor markup outside of the actual heading is better for overall accessibility. eleventy.config.js: import { IdAttributePlugin } from '@11ty/eleventy'
import * as cheerio from 'cheerio'
export default function (eleventyConfig) {
eleventyConfig.addTransform('inject_heading_anchors', createInjectHeadingAnchors({
selector: '[id]:is(h2,h3,h4,h5,h6):not(:has(> .header-anchor))',
}))
}
/**
* @typedef {object} InjectHeadingAnchorsOptions
* @property {string} [selector]
*/
/** @type {Required<InjectHeadingAnchorsOptions>} */
const injectHeadingAnchorsDefaultOptions = {
selector: '[id]:is(h2,h3,h4,h5,h6)',
}
/**
* @param {InjectHeadingAnchorsOptions} [options]
* @returns {(content: string) => string}
*/
function createInjectHeadingAnchors(options) {
return function (content) {
return injectHeadingAnchors(content, options)
}
}
/**
* @param {string} content
* @param {InjectHeadingAnchorsOptions} [options]
* @returns {string}
*/
function injectHeadingAnchors(content, options) {
/** @type {Required<InjectHeadingAnchorsOptions>} */
const optionsWithDefaults = {
...injectHeadingAnchorsDefaultOptions,
...options,
}
const $ = cheerio.load(content)
$(optionsWithDefaults.selector).map((_i, el) => {
const $heading = $(el)
$heading.attr('tabindex', '-1')
$heading.prepend(`<a class="header-anchor" href="#${$heading.attr('id')}">
<span class="header-anchor__icon">
<b>#</b>
<span class="visually-hidden">Jump to heading</span>
</span>
</a>`)
return $heading
})
return $.html()
} |
Unfortunately, it does not work with |
Rather than rely on template language specific plugins for
id
attributes, a la this one https://www.npmjs.com/package/markdown-it-anchor we can instead make use of the existing posthtml infrastructure used by the HTML base plugin and the input path to URL plugins to add anid
attributes to content.This then pairs nicely with a web component to add anchors to headings for the UX piece, a la @daviddarnes’ https://github.com/daviddarnes/heading-anchors
The text was updated successfully, but these errors were encountered: