Skip to content

Getting Started

Prism code editor has a rehype plugin, markdown-it plugin, and marked extension you can use to create editors (or highlighted code blocks) from fenced code blocks in markdown. This is much more convenient than using the SSR API directly. All code blocks on this page are rendered by the rehype plugin.

These markdown plugins have prism-code-editor version 4.0.0 or greater as a peer dependency, so be sure to also install that.

npm i prism-code-editor
npm i rehype-prism-code-editor

To write markdown and view the resulting editors and code blocks, check the demo running marked.

import rehype from "rehype"
import prismCodeEditor from "rehype-prism-code-editor"
import "prism-code-editor/prism/languages/common"
rehype().use(prismCodeEditor, {
// More on configuration later
}).process(/* some html */)

With Next.js:

next.config.mjs
import createMDX from "@next/mdx"
import prismCodeEditor from "rehype-prism-code-editor"
import "prism-code-editor/prism/languages/common"
/** @type {import("rehype-prism-code-editor").PcePluginOptions} */
const rehypePrismCodeEditorOptions = {
// More on configuration later
}
/** @type {import("next").NextConfig} */
const nextConfig = {
pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"],
}
const withMDX = createMDX({
options: {
rehypePlugins: [
[prismCodeEditor, rehypePrismCodeEditorOptions]
],
},
})
export default withMDX(nextConfig)

With Astro:

astro.config.mjs
import { defineConfig } from "astro/config"
import prismCodeEditor from "rehype-prism-code-editor"
import "prism-code-editor/prism/languages/common"
/** @type {import("rehype-prism-code-editor").PcePluginOptions} */
const rehypePrismCodeEditorOptions = {
// More on configuration later
}
export default defineConfig({
markdown: {
rehypePlugins: [
[prismCodeEditor, rehypePrismCodeEditorOptions]
]
}
})

To create editors, add the editor meta property to the code block. The meta properties use HTML-attribute-like syntax.

```js editor
// Some code
```

These plugins will also highlight code blocks without the editor property as static code blocks, but this can be disabled in the config if you want to use a different plugin for code blocks.

The following meta properties can change the behavior or appearance of an editor: tabSize, insertSpaces, lineNumbers, readOnly, wordWrap, rtl, and class. They have the same defaults as when creating editors with createEditor().

Static code blocks can be modified with meta properties like tabSize, lineNumbers, lineNumberStart, wordWrap, preverveIndent, guideIndents, rtl, and class. These have the same defaults as when creating code blocks with renderCodeBlock(). Additionally, code blocks support highlighting lines with the highlight, ins, and del properties. More about these on the features page.

These plugins don’t inject any JavaScript to make the editors interactive. This means you have full control over when and how they’re mounted and which extensions are added. To mount them, run mountEditorsUnder() in client-side JavaScript. See the SSR guide for more info.

With JavaScript on the client, you can add features like a copy button, bracket/tag highlighting and hover descriptions using forEachCodeBlock(). The code blocks guide tells you how.

These plugins don’t inject any CSS into the page, allowing full customization of the editors’ and code blocks’ appearance. See the styling guide and the code blocks guide for more info.

By default, zero languages have syntax highlighting and you must import the languages you want to support. The examples shown on this page all import prism-code-editor/prism/languages/common for 42 common languages, but you can import prism-code-editor/prism/languages for all languages, or import individual languages. See the working with Prism guide.