Plugins Anchors

help readers find their way @mdsvex/plugin-anchors@0.1.1

Anchors

Heading anchors, a link on every heading that has an id

Install

pnpm add -D @mdsvex/plugin-anchors

Turn it on

import { anchors } from '@mdsvex/plugin-anchors';

mdsvex({ heading_ids: true, plugins: [anchors()] });

What it reads

## Heading

acts on headings

An example

You write
## Getting started

Every heading with an id links to itself.

You get

Getting started

Every heading with an id links to itself.

Build-time preview.

Puts a link to itself on every heading that has an id:

<h2 id="getting-started"><a href="#getting-started">Getting started</a></h2>

It needs heading_ids. The plugin makes no ids of its own. It links the id each heading has once heading_ids has run, so with the option off no heading has one and nothing is linked. A heading left without an id, such as ## {title}, gets no link either. An id a parse plugin set is linked like any other.

anchors({
	position: 'wrap' | 'prepend' | 'append' = 'wrap',
	attrs: Record<string, string>
});
  • 'wrap' puts the content of the heading inside the link. A heading that already holds a link is left as it is, since a link can't hold another.
  • 'prepend' and 'append' add an empty link before or after the content, with aria-hidden="true" and tabindex="-1", for a stylesheet to draw:
anchors({ position: 'prepend', attrs: { class: 'anchor' } });
<h2 id="getting-started">
	<a href="#getting-started" aria-hidden="true" tabindex="-1" class="anchor"></a>Getting started
</h2>
.anchor::before {
	content: '#';
}
  • attrs are more attributes for the link. One named as a default replaces it.

Options, from its types 2

position?: 'wrap' | 'prepend' | 'append'
Where the link goes, `'wrap'` puts the content of the heading inside it `'prepend'` and `'append'` add an empty link before or after the content, with `aria-hidden="true"` and `tabindex="-1"`, for a stylesheet to draw
attrs?: Record<string, string>
More attributes for the link, such as a class, they replace a default of the same name

mdsvex, markdown with Svelte in it. MIT licence.