Adds a code block for Lexical richtext fields that is highlighted on the server with Shiki. It is registered as a Payload plugin, added to the editor through BlocksFeature, and rendered on the frontend with JSX converters as a React server component. Requires Payload 3.55.1 or later.
A server rendered code block component powered by Shiki for Payload CMS
Installation
npm i payload-shiki-code3
Stars
0
Forks
2
Open Issues
1 years ago
Last Updated
2.1k
Weekly Downloads
v0.0.3
npm Version
170.2 KB
Unpacked Size
1
Dependencies
30 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 3 |
| Dec 4, 2026 | 3 |
| Dec 3, 2026 | 3 |
| Dec 2, 2026 | 3 |
| Dec 1, 2026 | 3 |
| Nov 30, 2026 | 3 |
| Nov 29, 2026 | 3 |
| Nov 28, 2026 | 3 |
| Nov 27, 2026 | 3 |
| Nov 26, 2026 | 3 |
| Nov 25, 2026 | 3 |
| Nov 24, 2026 | 3 |
| Nov 23, 2026 | 3 |
| Nov 22, 2026 | 3 |
| Nov 21, 2026 | 3 |
| Nov 20, 2026 | 3 |
| Nov 19, 2026 | 3 |
| Nov 18, 2026 | 3 |
| Nov 17, 2026 | 3 |
| Nov 16, 2026 | 3 |
| Nov 15, 2026 | 3 |
| Nov 14, 2026 | 3 |
| Nov 13, 2026 | 3 |
| Nov 12, 2026 | 3 |
| Nov 11, 2026 | 3 |
| Nov 10, 2026 | 3 |
| Nov 9, 2026 | 3 |
| Nov 8, 2026 | 3 |
| Nov 7, 2026 | 3 |
| Nov 6, 2026 | 3 |
| Nov 5, 2026 | 3 |
| Nov 4, 2026 | 3 |
| Nov 3, 2026 | 3 |
| Nov 2, 2026 | 3 |
| Nov 1, 2026 | 3 |
| Oct 31, 2026 | 3 |
| Oct 30, 2026 | 3 |
| Oct 29, 2026 | 3 |
| Oct 28, 2026 | 3 |
| Oct 27, 2026 | 3 |
| Oct 26, 2026 | 3 |
| Oct 25, 2026 | 3 |
| Oct 24, 2026 | 3 |
| Oct 23, 2026 | 3 |
| Oct 22, 2026 | 3 |
| Oct 21, 2026 | 3 |
| Oct 20, 2026 | 3 |
| Oct 19, 2026 | 3 |
| Oct 18, 2026 | 3 |
| Oct 17, 2026 | 3 |
| Oct 16, 2026 | 3 |
| Oct 15, 2026 | 3 |
| Oct 14, 2026 | 3 |
| Oct 13, 2026 | 3 |
| Oct 12, 2026 | 3 |
| Oct 11, 2026 | 3 |
| Oct 10, 2026 | 3 |
| Oct 9, 2026 | 3 |
| Oct 8, 2026 | 3 |
| Oct 7, 2026 | 3 |
| Oct 6, 2026 | 3 |
| Oct 5, 2026 | 3 |
| Oct 4, 2026 | 3 |
| Oct 3, 2026 | 3 |
| Oct 2, 2026 | 3 |
| Oct 1, 2026 | 3 |
| Sep 30, 2026 | 3 |
| Sep 29, 2026 | 3 |
| Sep 28, 2026 | 3 |
| Sep 27, 2026 | 3 |
| Sep 26, 2026 | 3 |
| Sep 25, 2026 | 3 |
| Sep 24, 2026 | 3 |
| Sep 23, 2026 | 3 |
| Sep 22, 2026 | 3 |
| Sep 21, 2026 | 3 |
| Sep 20, 2026 | 3 |
| Sep 19, 2026 | 3 |
| Sep 18, 2026 | 3 |
| Sep 17, 2026 | 3 |
| Sep 16, 2026 | 3 |
| Sep 15, 2026 | 3 |
| Sep 14, 2026 | 3 |
| Sep 13, 2026 | 3 |
| Sep 12, 2026 | 3 |
| Sep 11, 2026 | 3 |
| Sep 10, 2026 | 3 |
| Sep 9, 2026 | 3 |
| Sep 8, 2026 | 3 |
| Sep 7, 2026 | 3 |
| Sep 6, 2026 | 3 |
2K+1.7K since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 1,982 |
| Dec 4, 2026 | 1,820 |
| Dec 3, 2026 | 1,669 |
| Dec 2, 2026 | 1,562 |
| Dec 1, 2026 | 1,892 |
| Nov 30, 2026 | 2,062 |
| Nov 29, 2026 | 2,146 |
| Nov 28, 2026 | 2,532 |
| Nov 27, 2026 | 2,873 |
| Nov 26, 2026 | 3,065 |
| Nov 25, 2026 | 3,291 |
| Nov 24, 2026 | 2,924 |
| Nov 23, 2026 | 2,676 |
| Nov 22, 2026 | 2,746 |
| Nov 21, 2026 | 2,969 |
| Nov 20, 2026 | 3,091 |
| Nov 19, 2026 | 3,208 |
| Nov 18, 2026 | 3,110 |
| Nov 17, 2026 | 3,520 |
| Nov 16, 2026 | 3,523 |
| Nov 15, 2026 | 3,345 |
| Nov 14, 2026 | 3,093 |
| Nov 13, 2026 | 2,700 |
| Nov 12, 2026 | 2,495 |
| Nov 11, 2026 | 2,275 |
| Nov 10, 2026 | 2,411 |
| Nov 9, 2026 | 2,437 |
| Nov 8, 2026 | 2,318 |
| Nov 7, 2026 | 2,216 |
| Nov 6, 2026 | 2,082 |
| Nov 5, 2026 | 2,262 |
| Nov 4, 2026 | 2,590 |
| Nov 3, 2026 | 2,441 |
| Nov 2, 2026 | 2,556 |
| Nov 1, 2026 | 2,563 |
| Oct 31, 2026 | 2,299 |
| Oct 30, 2026 | 2,285 |
| Oct 29, 2026 | 1,871 |
| Oct 28, 2026 | 1,331 |
| Oct 27, 2026 | 829 |
| Oct 26, 2026 | 544 |
| Oct 25, 2026 | 475 |
| Oct 24, 2026 | 513 |
| Oct 23, 2026 | 383 |
| Oct 22, 2026 | 395 |
| Oct 21, 2026 | 398 |
| Oct 20, 2026 | 363 |
| Oct 19, 2026 | 262 |
| Oct 18, 2026 | 181 |
| Oct 17, 2026 | 70 |
| Oct 16, 2026 | 74 |
| Oct 15, 2026 | 120 |
| Oct 14, 2026 | 207 |
| Oct 13, 2026 | 336 |
| Oct 12, 2026 | 419 |
| Oct 11, 2026 | 459 |
| Oct 10, 2026 | 470 |
| Oct 9, 2026 | 627 |
| Oct 8, 2026 | 651 |
| Oct 7, 2026 | 714 |
| Oct 6, 2026 | 603 |
| Oct 5, 2026 | 508 |
| Oct 4, 2026 | 450 |
| Oct 3, 2026 | 425 |
| Oct 2, 2026 | 283 |
| Oct 1, 2026 | 244 |
| Sep 30, 2026 | 96 |
| Sep 29, 2026 | 105 |
| Sep 28, 2026 | 124 |
| Sep 27, 2026 | 143 |
| Sep 26, 2026 | 281 |
| Sep 25, 2026 | 313 |
| Sep 24, 2026 | 300 |
| Sep 23, 2026 | 300 |
| Sep 22, 2026 | 288 |
| Sep 21, 2026 | 289 |
| Sep 20, 2026 | 283 |
| Sep 19, 2026 | 167 |
| Sep 18, 2026 | 228 |
| Sep 17, 2026 | 240 |
| Sep 16, 2026 | 346 |
| Sep 15, 2026 | 401 |
| Sep 14, 2026 | 476 |
| Sep 13, 2026 | 505 |
| Sep 12, 2026 | 544 |
| Sep 11, 2026 | 490 |
| Sep 10, 2026 | 489 |
| Sep 9, 2026 | 407 |
| Sep 8, 2026 | 391 |
| Sep 7, 2026 | 302 |
| Sep 6, 2026 | 282 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1052533373-root)<a href="https://payloaddirectory.dev/plugins/1052533373-root"><img src="https://payloaddirectory.dev/badge/1052533373-root.svg" alt="Payload Directory"></a>README
A Payload CMS plugin that integrates Shiki syntax highlighting for code blocks with support for richtext fields. Provides server-side syntax highlighting with extensive theme and language support, lazy loading, and maximum customization options.

npm install payload-shiki-code
# or
pnpm add payload-shiki-code
# or
yarn add payload-shiki-code
Add the plugin to your Payload configuration:
// payload.config.ts
import { buildConfig } from "payload/config";
import { payloadShikiCode } from "payload-shiki-code";
export default buildConfig({
plugins: [
payloadShikiCode({
languages: ["javascript", "typescript", "python", "html", "css"],
displayOptions: {
lineNumbers: true,
showLanguage: true,
copyButton: true,
},
shiki: {
themes: {
light: "github-light", // default global theme for light mode
dark: "github-dark", // default global theme for dark mode
},
},
// ... other options
}),
],
// ... rest of your config
});
NOTE: If you need dark mode support, make sure add following to your global css:
:root {
color-scheme: light dark;
}
Import and add the code block to your richtext fields:
// In your collection/global configuration
import { lexicalEditor } from "@payloadcms/richtext-lexical";
import { BlocksFeature } from "@payloadcms/richtext-lexical/client";
import { createBlockConfig } from "payload-shiki-code";
export const Posts = {
slug: "posts",
fields: [
{
name: "content",
type: "richText",
editor: lexicalEditor({
features: ({ defaultFeatures }) => [
...defaultFeatures,
BlocksFeature({
blocks: [
createBlockConfig(), // Add the code block
],
}),
],
}),
},
],
};
To render code blocks on your frontend, add the JSX converters:
// For React frontend rendering
import { createCodeBlockJSXConverter, mergeCodeBlockConverter } from 'payload-shiki-code/converters'
// Using a helper function
const jsxConverters: JSXConvertersFunction<NodeTypes> = ({
defaultConverters,
}) => mergeCodeBlockConverter(defaultConverters, "test"); // extra classnames to the code block
// Or manually merging
const jsxConverters: JSXConvertersFunction<NodeTypes> = ({
defaultConverters,
}) => ({
...defaultConverters,
blocks: {
...defaultConverters.blocks,
...createCodeBlockJSXConverter("test").blocks,
},
});
// Use with your richtext rendering
<RichText data={post.content} converters={jsxConverters} />
interface PayloadShikiCodeConfig {
// Plugin control
disabled?: boolean;
blockSlug?: string; // default: 'code'
blockInterfaceName?: string; // default: 'CodeBlock'
cacheHighlighter?: boolean; // default: true
// Language configuration
languages?: Array<BundledLanguage | SpecialLanguage | LanguageRegistration>; // default: ['text'] - only these languages will be loaded
// Shiki configuration
shiki?: {
themes?: {
light: BundledTheme | ThemeRegistration;
dark: BundledTheme | ThemeRegistration;
[key: string]: BundledTheme | ThemeRegistration;
};
highlighterOptions?: Partial<HighlighterCoreOptions>; // e.g., { engine: 'javascript' } for JS regex engine
codeToHastOptions?: Partial<CodeToHastOptions>;
transformers?: Array<ShikiTransformer | TransformerFactory>;
};
// Display defaults
displayOptions?: {
lineNumbers?: boolean; // default: true
showLanguage?: boolean; // default: true
copyButton?: boolean; // default: true
startLineNumber?: number; // default: 1
containerClasses?: string[];
preClasses?: string[];
codeClasses?: string[];
};
// Notation styling
notationOptions?: {
highlight?: {
backgroundColor?: string;
borderColor?: string;
className?: string;
};
add?: {
backgroundColor?: string;
borderColor?: string;
className?: string;
};
remove?: {
backgroundColor?: string;
borderColor?: string;
className?: string;
};
};
// Style customization
styleOptions?: {
cssVariables?: Record<string, string>;
borderColor?: string;
borderRadius?: string;
backgroundColor?: string;
padding?: string;
fontFamily?: string;
fontSize?: string;
lineHeight?: string;
lineNumberMarginRight?: string;
copyButton?: {
// Light theme styles
backgroundColor?: string;
color?: string;
borderColor?: string;
hoverBackgroundColor?: string;
hoverBorderColor?: string;
successColor?: string;
successBackgroundColor?: string;
successBorderColor?: string;
successHoverBackgroundColor?: string;
// Dark theme styles
dark?: {
backgroundColor?: string;
color?: string;
borderColor?: string;
hoverBackgroundColor?: string;
hoverBorderColor?: string;
successColor?: string;
successBackgroundColor?: string;
successBorderColor?: string;
successHoverBackgroundColor?: string;
};
};
};
// Lifecycle hooks
hooks?: {
onHighlighterCreate?: (
highlighter: HighlighterGeneric<BundledLanguage, BundledTheme>
) => void | Promise<void>;
onHighlighterDispose?: () => void | Promise<void>;
beforeRender?: (
code: string,
lang: string,
options: any
) => void | Promise<void>;
afterRender?: (
html: string,
code: string,
lang: string
) => void | Promise<void>;
};
}
The plugin uses a simplified language configuration to optimize bundle size and improve performance:
export default buildConfig({
plugins: [
payloadShikiCode({
// Only these languages will be loaded and available, aliases will be excluded
languages: [
'javascript',
'typescript',
'python',
'html',
'css',
'json',
'markdown',
'bash',
'text', // Special language for plain text
'ansi' // Special language for ANSI colored output
],
})
]
})
Benefits:
BundledLanguage type for autocomplete and validationlanguages array instead of multiple optionsAvailable Languages: The languages option accepts:
javascript, typescript, python, go, rust, etc.)text, ansi for non-syntax-highlighted contentSee the complete language list for all available options.
Add custom Shiki transformers for additional functionality:
import { payloadShikiCode } from "payload-shiki-code";
import type { ShikiTransformer } from "shiki";
const myCustomTransformer: ShikiTransformer = {
name: "my-transformer",
code(node) {
// Modify the code node
return node;
},
};
export default buildConfig({
plugins: [
payloadShikiCode({
shiki: {
transformers: [
myCustomTransformer,
// Or use a factory function that receives plugin options
(options) => ({
name: "context-aware-transformer",
code(node) {
// Access plugin options here
return node;
},
}),
],
},
}),
],
});
Replace the default copy button with your own implementation by passing it as a prop to the CodeBlock component:
"use client";
import { CodeBlock, type CodeBlockProps } from "payload-shiki-code/rsc";
import type { CopyButtonProps } from "payload-shiki-code/client";
// or import the default copy button
// import { CopyButton } from "payload-shiki-code/client";
// Custom copy button component
const CustomCopyButton = ({ code, onCopySuccess, onCopyError }: CopyButtonProps) => {
// const handleClick = ...
return (
<button
onClick={handleClick}
>
Copy to clipboard
</button>
);
};
// Use in your React component
function MyCodeBlock({ data }: { data: CodeBlockProps["data"] }) {
return (
<CodeBlock
data={data}
CopyButton={CustomCopyButton} // Pass your custom copy button here
/>
);
}
The CopyButtonProps interface provides:
code: The code content to copyclassName: Optional CSS class nameariaLabel: Optional aria-label for accessibilityonCopySuccess: Callback when copy succeedsonCopyError: Callback when copy failssuccessDuration: Duration to show success stateYou can customize the copy button appearance through the plugin configuration:
export default buildConfig({
plugins: [
payloadShikiCode({
styleOptions: {
copyButton: {
// Light theme styles
backgroundColor: "rgba(255, 255, 255, 0.95)",
color: "rgb(50, 50, 50)",
borderColor: "rgba(0, 0, 0, 0.15)",
hoverBackgroundColor: "rgba(255, 255, 255, 1)",
hoverBorderColor: "rgba(0, 0, 0, 0.25)",
successColor: "rgb(16, 185, 129)",
successBackgroundColor: "rgba(16, 185, 129, 0.1)",
successBorderColor: "rgba(16, 185, 129, 0.3)",
successHoverBackgroundColor: "rgba(16, 185, 129, 0.15)",
// Dark theme styles
dark: {
backgroundColor: "rgba(40, 40, 40, 0.95)",
color: "rgb(220, 220, 220)",
borderColor: "rgba(255, 255, 255, 0.15)",
hoverBackgroundColor: "rgba(50, 50, 50, 1)",
hoverBorderColor: "rgba(255, 255, 255, 0.25)",
successColor: "rgb(16, 185, 129)",
successBackgroundColor: "rgba(16, 185, 129, 0.1)",
successBorderColor: "rgba(16, 185, 129, 0.3)",
successHoverBackgroundColor: "rgba(16, 185, 129, 0.15)",
},
},
},
}),
],
});
Alternatively, you can override the CSS variables directly:
:root {
/* Light theme */
--shiki-copy-button-bg: rgba(255, 255, 255, 0.9);
--shiki-copy-button-color: rgb(59, 59, 59);
--shiki-copy-button-border: rgba(0, 0, 0, 0.1);
--shiki-copy-button-hover-bg: rgba(255, 255, 255, 1);
--shiki-copy-button-hover-border: rgba(0, 0, 0, 0.2);
--shiki-copy-success-color: rgb(34, 197, 94);
--shiki-copy-success-border: rgba(34, 197, 94, 0.3);
--shiki-copy-success-bg: rgba(34, 197, 94, 0.1);
--shiki-copy-success-hover-bg: rgba(34, 197, 94, 0.15);
/* Dark theme */
--shiki-copy-button-bg-dark: rgba(30, 30, 30, 0.9);
--shiki-copy-button-color-dark: rgb(200, 200, 200);
--shiki-copy-button-border-dark: rgba(255, 255, 255, 0.1);
--shiki-copy-button-hover-bg-dark: rgba(40, 40, 40, 0.95);
--shiki-copy-button-hover-border-dark: rgba(255, 255, 255, 0.2);
--shiki-copy-success-color-dark: rgb(34, 197, 94);
--shiki-copy-success-border-dark: rgba(34, 197, 94, 0.3);
--shiki-copy-success-bg-dark: rgba(34, 197, 94, 0.1);
--shiki-copy-success-hover-bg-dark: rgba(34, 197, 94, 0.15);
}
Hook into the plugin lifecycle for custom functionality:
export default buildConfig({
plugins: [
payloadShikiCode({
hooks: {
onHighlighterCreate: async (highlighter) => {
console.log(
"Highlighter created with themes:",
highlighter.getLoadedThemes()
);
},
beforeRender: async (code, lang, options) => {
// Log or modify before rendering
console.log(`Rendering ${lang} code block`);
},
afterRender: async (html, code, lang) => {
// Process the generated HTML
},
},
}),
],
});
The code block includes the following fields that users can configure:
The plugin also provides these utility exports:
// Admin components
import { CodeFieldClient } from "payload-shiki-code/admin";
// Utilities
import { getLanguageAlias, loadTheme } from "payload-shiki-code/utils";
// Default copy button component
import { CopyButton } from "payload-shiki-code/client";
The plugin provides several CSS classes for custom styling:
.shiki-container - Main container wrapper.shiki-content - Content wrapper.shiki-header - Header section with filename and language.shiki-file-name - File name display.shiki-language - Language label.shiki-code-wrapper - Code area wrapper.shiki-line - Individual code lines.shiki-line-number - Line number elementslanguages config are loaded, reducing server bundle size, the code block is a React server component, no extra JavaScript code for your frontendContributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
For issues and questions:
richtext
Rich-text child block, default Lexical editor preset and JSX renderer for Atomic Payload
6433/wk37v3
ox payload editorial
Editorial Lexical blocks written as markdown directives, with lossless round trip and token-styled rendering
250v3 v4
payload markdown
Markdown fields for Payload with a CodeMirror editor, Shiki code blocks, directives, themes and server-side rendering
64876/wk8v3
payload paste as markdown
Lexical editor feature for pasting raw Markdown through a toolbar button, slash command, or drawer
631.2k/wk0v3
payload lexical typography
Lexical editor features for text color, font size, letter spacing, line height and font family
491.5k/wk109v?
payload code block feature
Adds a code block feature to the Payload Lexical editor using @lexical/code
397/wk11v2