Socket
Book a DemoInstallSign in
Socket

remark-custom-header-id

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

remark-custom-header-id

Remark plugin for adding a custom ID attribute to Markdown headers

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
6.7K
20.69%
Maintainers
1
Weekly downloads
 
Created
Source

remark-custom-header-id

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

npm install remark-custom-header-id

Usage

Remark

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));
//=> '<h1 id="custom-id">Foo</h1>'

Astro

// 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 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.

Keywords

remark

FAQs

Package last updated on 28 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts