
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@md-plugins/md-plugin-headers
Advanced tools
A markdown-it plugin for handling headers (H1-H6).
A Markdown-It plugin that extracts and processes headers from Markdown content. This plugin is ideal for generating Table of Contents (ToC) or managing headers for documentation and static sites.
NOTE: When using with vue-router
be sure to use history
mode and not hash
mode otherwise your hash links will not work as expected.
h1
, h2
, h3
).Install the plugin via your preferred package manager:
# with pnpm:
pnpm add @md-plugins/md-plugin-headers
# with Yarn:
yarn add @md-plugins/md-plugin-headers
# with npm:
npm install @md-plugins/md-plugin-headers
import MarkdownIt from 'markdown-it';
import { headersPlugin } from '@md-plugins/md-plugin-headers';
import type { MarkdownItEnv } from '@md-plugins/shared';
const md = new MarkdownIt();
md.use(headersPlugin, {
level: [1, 2, 3], // Extract h1, h2, and h3 headers
slugify: (str) => str.toLowerCase().replace(/\s+/g, '-'),
format: (title) => title.toUpperCase(),
});
const markdownContent = `
# Header 1
## Subheader 1.1
### Subheader 1.1.1
`;
const env: MarkdownItEnv = {};
const renderedOutput = md.render(markdownContent, env);
console.log('Rendered Output:', renderedOutput);
console.log('Extracted Headers:', env.toc);
The plugin processes headers and stores them in the toc
property of the Markdown-It environment (env
):
[
{
"level": 1,
"title": "HEADER 1",
"id": "header-1",
"link": "#header-1",
"children": [
{
"level": 2,
"title": "SUBHEADER 1.1",
"id": "subheader-1-1",
"link": "#subheader-1-1",
"children": [
{
"level": 3,
"title": "SUBHEADER 1.1.1",
"id": "subheader-1-1-1",
"link": "#subheader-1-1-1",
"children": []
}
]
}
]
}
]
The md-plugin-headers
plugin supports the following options:
Option | Type | Default | Description |
---|---|---|---|
level | number[] | [2, 3] | Heading levels to extract (e.g., [2, 3] for h2, h3). |
slugify | function | (str) => str | Function to generate slugs for header IDs. |
format | function | (str) => str | Function to format header titles. |
shouldAllowNested | boolean | false | Whether to allow headers inside nested blocks (e.g., lists, quotes). |
With the toc
property in env
, you can build a ToC dynamically:
function generateToC(toc) {
const list = toc
.map((item) => {
const children = item.children ? `<ul>${generateToC(item.children)}</ul>` : ''
return `<li><a href="${item.link}">${item.title}</a>${children}</li>`
})
.join('')
return `<ul>${list}</ul>`
}
const tocHtml = generateToC(env.toc)
console.log('Generated ToC:', tocHtml)
You can use your own slugification logic for header IDs:
md.use(headersPlugin, {
slugify: (str) => encodeURIComponent(str.replace(/\s+/g, '_')),
})
Whatever you use on the backend, should be the same logic on the frontend for slugification so that they match when using hash links.
Enable nested headers to include headers inside blockquotes or lists:
md.use(headersPlugin, {
shouldAllowNested: true,
})
pnpm test
In case this README falls out of date, please refer to the documentation for the latest information.
This project is licensed under the MIT License. See the LICENSE file for details.
FAQs
A markdown-it plugin for handling headers (H1-H6).
The npm package @md-plugins/md-plugin-headers receives a total of 12 weekly downloads. As such, @md-plugins/md-plugin-headers popularity was classified as not popular.
We found that @md-plugins/md-plugin-headers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.