![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
rehype-prism
Advanced tools
The unified plugin used to highlight code block in html with Prism.
This package is ESM only. unified >= 10 is needed to use it. If running on the server side, NodeJS needs to be greater than 12
import { unified } from "unified";
import rehype from "rehype";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypePrism from "rehype-prism";
import rehypeStringify from "rehype-stringify";
// you have to load css manual
import "prismjs/themes/prism-coy.css";
import "prismjs/plugins/line-numbers/prism-line-numbers.css";
// load languages manual
// import 'prismjs/components/prism-{language}'
import "prismjs/components/prism-go";
import "prismjs/components/prism-java";
import "prismjs/components/prism-python";
// parse markdown to html
unified()
.use(remarkParse)
.use(remarkRehype)
// it should be after rehype
.use(rehypePrism, { plugins: ["line-numbers"] })
.use(rehypeStringify)
.parse(/* markdown string */);
// .processSync(/* markdown string */)
// parse code block in html string
rehype().use(rehypePrism).use(rehypeStringify).parse(/* html string */);
// .processSync(/* html string */)
PrismJS
will auto highlight all code at pre code
after browser document
loaded.
Disabled prism autoHighlight before import 'rehype-prism'
. there are two way to do this:
set the window.Prism = { manual: true }
use the attribute data-manual
on the <script>
element you used for prism.
<script src="prism.js" data-manual></script>
Some plugins will not work. Because it's only work in browser.
The names to use can be found here.
The table list plugins that cannot running on the server side. Therefor it has been re-implemented by rehype-prism.
Plugin Name |
---|
line-numbers |
toolbar |
copy-to-clipboard |
I haven't tested all prism plugins. If there are another plugins not work, submit issue on github.
loadLanguages()
provided by prismjs
.(don't use loadLanguages()
with Webpack or another bundler)import 'prismjs/components/prism-yaml.js'
import 'prismjs'
will auto load the theme setted in babel-plugin-prismjs config.import 'prismjs/themes/prism-coy.css'
shiki is another code highlight tools that support unified. I recommend you try it. It's new, but better suited for modern front-end projects than Prismjs.
This does not mean that I will give up maintaining this library. If you need this library and have any issue, let me know it.
If there is any doubt, it is very welcome to discuss the issue together.
FAQs
The unified plugin used to highlight code block in html with Prism
We found that rehype-prism 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
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.