Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sindresorhus/remark-custom-header-id

Remark plugin for adding a custom ID attribute to Markdown headers
https://github.com/sindresorhus/remark-custom-header-id

markdown nodejs npm remark remark-plugin

Last synced: about 13 hours ago
JSON representation

Remark plugin for adding a custom ID attribute to Markdown headers

Awesome Lists containing this project

README

        

# remark-custom-header-id

> [Remark](https://github.com/remarkjs/remark) plugin for adding a custom ID attribute to Markdown headers

This allows for stable and more meaningful anchor links in your generated HTML.

## Install

```sh
npm install remark-custom-header-id
```

## Usage

### Remark

```js
import {remark} from 'remark';
import remarkRehype from 'remark-rehype';
import rehypeStringify from 'rehype-stringify';
import remarkCustomHeaderId from 'remark-custom-header-id';

const file = await remark()
.use(remarkCustomHeaderId)
.use(remarkRehype) // Markdown to HTML
.use(rehypeStringify) // Stringify the HTML
.process('# Foo {#custom-id}');

console.log(String(file));
//=> '

Foo

'
```

### Astro

```js
// astro.config.js
import remarkCustomHeaderId from 'remark-custom-header-id';

export default defineConfig({
// …
markdown: {
remarkPlugins: [
remarkCustomHeaderId,
],
},
// …
});
```

### Alternative syntax for MDX files

The `{#custom-id}` notation does not work in [MDX](https://mdxjs.com) files because MDX treats `{}` as JSX syntax, causing a parsing error.

You can use one of these alternatives:

- Escape curly braces: `\{#custom-id\}`
- Use this syntax: `||custom-id||`

Examples:

- `## Some header \{#custom-id\}`
- `## Some header ||custom-id||`

## API

### remarkCustomHeaderId()

Returns a transformer function to be used with Remark.