escwxyz

payload shiki code

by escwxyz
Rich text & editors
v3
^3.55.1
MIT

Overview

Summary generated from the README

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.

Features

  • Server-side highlighting with Shiki, so no extra frontend JavaScript is needed
  • Configurable language list that limits bundle size
  • Light and dark theme selection globally or per block
  • Line numbers, language label, filename header and line wrapping options
  • Line highlight, add and remove notation by line ranges
  • Copy button that can be replaced, plus lifecycle hooks and custom Shiki transformers

Use cases

  • Display syntax-highlighted code samples in blog posts or documentation managed in Payload
  • Show diffs with added and removed lines inside richtext content
  • Match code block themes to a site's light and dark modes

From the author

A server rendered code block component powered by Shiki for Payload CMS

Installation

npm i payload-shiki-code

3

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

GitHub stars

30 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20263
Dec 4, 20263
Dec 3, 20263
Dec 2, 20263
Dec 1, 20263
Nov 30, 20263
Nov 29, 20263
Nov 28, 20263
Nov 27, 20263
Nov 26, 20263
Nov 25, 20263
Nov 24, 20263
Nov 23, 20263
Nov 22, 20263
Nov 21, 20263
Nov 20, 20263
Nov 19, 20263
Nov 18, 20263
Nov 17, 20263
Nov 16, 20263
Nov 15, 20263
Nov 14, 20263
Nov 13, 20263
Nov 12, 20263
Nov 11, 20263
Nov 10, 20263
Nov 9, 20263
Nov 8, 20263
Nov 7, 20263
Nov 6, 20263
Nov 5, 20263
Nov 4, 20263
Nov 3, 20263
Nov 2, 20263
Nov 1, 20263
Oct 31, 20263
Oct 30, 20263
Oct 29, 20263
Oct 28, 20263
Oct 27, 20263
Oct 26, 20263
Oct 25, 20263
Oct 24, 20263
Oct 23, 20263
Oct 22, 20263
Oct 21, 20263
Oct 20, 20263
Oct 19, 20263
Oct 18, 20263
Oct 17, 20263
Oct 16, 20263
Oct 15, 20263
Oct 14, 20263
Oct 13, 20263
Oct 12, 20263
Oct 11, 20263
Oct 10, 20263
Oct 9, 20263
Oct 8, 20263
Oct 7, 20263
Oct 6, 20263
Oct 5, 20263
Oct 4, 20263
Oct 3, 20263
Oct 2, 20263
Oct 1, 20263
Sep 30, 20263
Sep 29, 20263
Sep 28, 20263
Sep 27, 20263
Sep 26, 20263
Sep 25, 20263
Sep 24, 20263
Sep 23, 20263
Sep 22, 20263
Sep 21, 20263
Sep 20, 20263
Sep 19, 20263
Sep 18, 20263
Sep 17, 20263
Sep 16, 20263
Sep 15, 20263
Sep 14, 20263
Sep 13, 20263
Sep 12, 20263
Sep 11, 20263
Sep 10, 20263
Sep 9, 20263
Sep 8, 20263
Sep 7, 20263
Sep 6, 20263
npm weekly downloads

2K+1.7K since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20261,982
Dec 4, 20261,820
Dec 3, 20261,669
Dec 2, 20261,562
Dec 1, 20261,892
Nov 30, 20262,062
Nov 29, 20262,146
Nov 28, 20262,532
Nov 27, 20262,873
Nov 26, 20263,065
Nov 25, 20263,291
Nov 24, 20262,924
Nov 23, 20262,676
Nov 22, 20262,746
Nov 21, 20262,969
Nov 20, 20263,091
Nov 19, 20263,208
Nov 18, 20263,110
Nov 17, 20263,520
Nov 16, 20263,523
Nov 15, 20263,345
Nov 14, 20263,093
Nov 13, 20262,700
Nov 12, 20262,495
Nov 11, 20262,275
Nov 10, 20262,411
Nov 9, 20262,437
Nov 8, 20262,318
Nov 7, 20262,216
Nov 6, 20262,082
Nov 5, 20262,262
Nov 4, 20262,590
Nov 3, 20262,441
Nov 2, 20262,556
Nov 1, 20262,563
Oct 31, 20262,299
Oct 30, 20262,285
Oct 29, 20261,871
Oct 28, 20261,331
Oct 27, 2026829
Oct 26, 2026544
Oct 25, 2026475
Oct 24, 2026513
Oct 23, 2026383
Oct 22, 2026395
Oct 21, 2026398
Oct 20, 2026363
Oct 19, 2026262
Oct 18, 2026181
Oct 17, 202670
Oct 16, 202674
Oct 15, 2026120
Oct 14, 2026207
Oct 13, 2026336
Oct 12, 2026419
Oct 11, 2026459
Oct 10, 2026470
Oct 9, 2026627
Oct 8, 2026651
Oct 7, 2026714
Oct 6, 2026603
Oct 5, 2026508
Oct 4, 2026450
Oct 3, 2026425
Oct 2, 2026283
Oct 1, 2026244
Sep 30, 202696
Sep 29, 2026105
Sep 28, 2026124
Sep 27, 2026143
Sep 26, 2026281
Sep 25, 2026313
Sep 24, 2026300
Sep 23, 2026300
Sep 22, 2026288
Sep 21, 2026289
Sep 20, 2026283
Sep 19, 2026167
Sep 18, 2026228
Sep 17, 2026240
Sep 16, 2026346
Sep 15, 2026401
Sep 14, 2026476
Sep 13, 2026505
Sep 12, 2026544
Sep 11, 2026490
Sep 10, 2026489
Sep 9, 2026407
Sep 8, 2026391
Sep 7, 2026302
Sep 6, 2026282

Each point is npm's rolling 7-day download count.

Health Score

37/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1052533373-root.svg)](https://payloaddirectory.dev/plugins/1052533373-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1052533373-root"><img src="https://payloaddirectory.dev/badge/1052533373-root.svg" alt="Payload Directory"></a>
Updated Sep 19, 2025Created Sep 8, 2025Published Sep 17, 2025

README

Payload Shiki Code Plugin

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.

light dark

Features

  • Server-side syntax highlighting using Shiki
  • 60+ programming languages supported
  • 60+ themes (light and dark variants)
  • Lazy loading of languages
  • Field-based notation for line highlighting, diff markers
  • Customizable display options (line numbers, language labels)
  • Lifecycle hooks for extensibility
  • TypeScript support with full type safety
  • Cached highlighter instance for optimal performance

Installation

npm install payload-shiki-code
# or
pnpm add payload-shiki-code
# or
yarn add payload-shiki-code

Basic Usage

1. Configure the Plugin

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;
} 

2. Add Code Block to Richtext Fields

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
            ],
          }),
        ],
      }),
    },
  ],
};

3. Add JSX Converters for Frontend Rendering

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} />

Configuration Options

Plugin Options

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>;
  };
}

Language Configuration

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:

  • Reduced Bundle Size: Only specified languages are included in the server bundle
  • Better Performance: All languages are preloaded during highlighter initialization
  • Type Safety: Uses BundledLanguage type for autocomplete and validation
  • Simplified Configuration: Single languages array instead of multiple options

Available Languages: The languages option accepts:

  • BundledLanguage: All standard programming languages supported by Shiki (60+ languages including javascript, typescript, python, go, rust, etc.)
  • SpecialLanguage: Special languages like text, ansi for non-syntax-highlighted content
  • LanguageRegistration: Custom language definitions for unsupported languages

See the complete language list for all available options.

Advanced Usage

Custom Transformers

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;
            },
          }),
        ],
      },
    }),
  ],
});

Custom Copy Button

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 copy
  • className: Optional CSS class name
  • ariaLabel: Optional aria-label for accessibility
  • onCopySuccess: Callback when copy succeeds
  • onCopyError: Callback when copy fails
  • successDuration: Duration to show success state

You 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);
}

Lifecycle Hooks

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
        },
      },
    }),
  ],
});

Block Fields

The code block includes the following fields that users can configure:

Code Tab

  • Language: Select from preloaded languages
  • File Name: Optional filename display
  • Notation Type: Choose from 'add', 'remove', or 'highlight'
  • Notation Range: Specify line ranges as an array (e.g., ["1-5", "7", "10-12"])
  • Code: The actual code content

Config Tab

  • Light Theme: Select theme for light mode
  • Dark Theme: Select theme for dark mode
  • Show Line Numbers: Toggle line number display
  • Show Language Label: Toggle language label display
  • Wrap: Enable/disable line wrapping

Additional Exports

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";

CSS Classes

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 elements

Performance Considerations

  • Cached Highlighter: By default, the plugin caches the Shiki highlighter instance for optimal performance
  • Bundle Size Optimization: Only the languages specified in the languages config are loaded, reducing server bundle size, the code block is a React server component, no extra JavaScript code for your frontend
  • Server-Side Rendering: All syntax highlighting happens on the server for better performance
  • Language Preloading: All configured languages are loaded when the highlighter is created, eliminating runtime loading delays

License

MIT

Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.

Support

For issues and questions:

Similar plugins