In the world of modern web development, managing long documents effectively is paramount. This is where the rehype-slug plugin comes into play, making it easy to generate unique IDs for your document headings. Let’s embark on a step-by-step journey to learn how to easily integrate this plugin into your project!
What is rehype-slug?
rehype-slug is a rehype plugin designed to add ID attributes to HTML headings (from h1 to h6). It looks for headings that don’t have IDs and creates them based on the text content of each heading. The resulting IDs follow GitHub’s slugification rules, ensuring they are unique and consistent.
When Should I Use rehype-slug?
This plugin is particularly handy when working with lengthy documents where linking to specific sections is necessary. Moreover, if paired with another plugin like rehype-autolink-headings, users can easily navigate to these sections via clickable links, enhancing the overall user experience.
Installation Instructions
rehype-slug is ESM (ECMAScript Module) only. Below are the installation instructions for different environments:
- In Node.js (version 16+):
npm install rehype-slug
import rehypeSlug from https://esm.sh/rehype-slug@6
<script type="module"> import rehypeSlug from "https://esm.sh/rehype-slug@6"</script>
Using rehype-slug
Let’s illustrate how to use rehype-slug with an example. Assume you have an HTML file named example.html:
<html>
<h1 id="some-id">Lorem ipsum</h1>
<h2>Dolor sit amet</h2>
<h3>consectetur & adipisicing</h3>
<h4>elit</h4>
<h5>elit</h5>
</html>
With a corresponding JavaScript file named example.js:
import read from "to-vfile";
import rehype from "rehype";
import rehypeSlug from "rehype-slug";
const file = await rehype()
.data("settings", { fragment: true })
.use(rehypeSlug)
.process(await read("example.html"));
console.log(String(file));
Running node example.js would yield the following output:
<html>
<h1 id="some-id">Lorem ipsum</h1>
<h2 id="dolor-sit-amet">Dolor sit amet</h2>
<h3 id="consectetur--adipisicing">consectetur & adipisicing</h3>
<h4 id="elit">elit</h4>
<h5 id="elit-1">elit</h5>
</html>
This output demonstrates how each heading is provided with a corresponding ID based on its text.
API Reference
The main function provided by this package is unified().use(rehypeSlug[, options]). It is a transformer function that adds IDs to headings based on specified options. The options parameter allows for custom prefixes to be added to the IDs.
Types and Compatibility
This plugin is fully typed with TypeScript and compatible with array maintainers of Node.js, ensuring it works well with rehype and unified libraries.
Security Considerations
Using rehype-slug can pose a risk of cross-site scripting (XSS) attacks due to the potential for ID collisions. To mitigate this risk, it is recommended to use rehype-sanitize alongside rehype-slug for proper sanitization of your HTML documents. You can refer to their examples for implementation guidance.
Troubleshooting
If you encounter any issues while implementing rehype-slug, here are a few steps you can take to resolve them:
- Ensure you are using Node.js version 16 or higher.
- Verify that all required packages are correctly installed.
- Check for any conflicts with other rehype plugins.
- Review the error messages closely for hints about potential configuration problems.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

