![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@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 npm:
npm install @md-plugins/md-plugin-headers
# Or with Yarn:
yarn add @md-plugins/md-plugin-headers
# Or with pnpm:
pnpm add @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).
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.