CKEditor for Strapi

repository·master·Indexed 41 days ago

https://github.com/nshenderov/strapi-plugin-ckeditor

Integrates CKEditor 5 into Strapi as a customizable custom field supporting HTML and Markdown outputs. Features include Strapi Media Library integration, responsive images, i18n support, and configurable presets. Version 8.0.0 is compatible with Strapi v5, while version 3 is compatible with Strapi v4.

Tokens
9.9K
Snippets
29
Records
39
Agent score
50%

What's inside CKEditor

  1. Configure Responsive Images and i18n in CKEditor

    master

    Responsive Images

    The plugin automatically generates srcset attributes for inserted images, provided the image has any available formats other than thumbnail.

    UI Language

    The editor's UI language defaults to the Strapi admin's preferred language. If no preference is set in Strapi, it falls back to English.

    Content Localization (i18n)

    To enable i18n for the editor's content language, go to the Advanced Settings tab within the field configuration and check the Enable localization for this field option.

  2. Configure the Plugin Theme

    master

    The Theme object defines global CSS styles applied to every editor instance. Styles are applied in a specific order:

    1. common: Core styles applied first.
    2. light or dark: Styles applied based on the user's preference.
    3. additional: Final styles applied to complement the theme.

    EditorStyles can be a plain CSS string or an array of interpolations for dynamic styling (e.g., using styled-components).

    import { type Theme } from '@_sh/strapi-plugin-ckeditor';
    
    const myTheme: Theme = {
      common: '.ck { font-size: 16px; }',
      light: '.ck { background: white; }',
      dark: '.ck { background: black; }',
      additional: '.ck { border: 1px solid gray; }',
    };
  3. Define CKEditor Presets

    master

    A Preset defines a specific set of features and appearance for an editor instance. This allows you to offer different editor modes (e.g., HTML vs Markdown) in the Content-Type Builder.

    Each Preset object contains:

    • name: The name of the preset (used in the schema).
    • description: A description shown in the Content-Type Builder.
    • editorConfig: The standard CKEditor 5 configuration object (refer to CKEditor documentation for details).
    • styles (optional): Additional CSS styles applied to the editor instance after the theme styles.
    import { type Preset } from '@_sh/strapi-plugin-ckeditor';
    
    const myCustomPreset: Preset = {
      name: 'myCustomPreset',
      description: 'A custom editor preset',
      editorConfig: {
        licenseKey: 'GPL',
        plugins: [ /* CKEditor plugins */ ],
        toolbar: [ /* toolbar items */ ],
      },
      styles: '.ck { color: red; }',
    };
  4. Use CKEditor in the Content-Type Builder

    master

    Once installed, you can add the CKEditor field to your content types. In the Strapi Content-Type Builder, navigate to the CUSTOM tab to find the CKEditor field type.

    Presets

    A preset is a predefined set of settings that determines the editor's features and appearance. You can assign a specific preset to each individual field.

    The plugin includes two built-in presets:

    • Default HTML editor: Outputs content in HTML format.
    • Default Markdown editor: Outputs content in Markdown format.

    Both presets include an extensive set of non-premium CKEditor plugins.

  5. Migrate from v1 to v2

    master

    When upgrading from version 1 to version 2, ensure the following requirements and changes are met:

    1. Strapi Version Compatibility

    • For plugin v2.0.x: You must use Strapi v4.4.x.
    • For plugin v2.1.x: You must use Strapi v4.13.0+.

    2. Schema Updates

    Starting with v2, the plugin utilizes the Strapi Custom Field API. You will need to manually update your content-type schemas to reflect this change.

    3. Configuration File

    Move your plugin configuration to /config/ckeditor.txt.

  6. Migrate from v7 to v8

    master

    Upgrading to version 8 updates CKEditor from 47.x to 48.x. Most setups require no code changes, but be aware of the following:

    • CKEditor 5 Update: If you use custom plugins or presets, consult the official CKEditor 5 migration guide.
    • CSS Specificity: CKEditor selectors are now wrapped in :is(). If you override styles via the plugin theme, ensure your rules are still specific enough to apply.
    • HTML Output Changes: Table alignment now uses CSS classes instead of inline styles, header cells include a scope attribute, and media embeds use width/height and aspect-ratio instead of padding-bottom. Existing content is not automatically rewritten.
    • HTML Sanitization: The default HTML preset now uses dompurify instead of sanitize-html. This allows more elements (images, video, audio, inline styles) in the editor preview. To revert to previous behavior, override htmlEmbed.sanitizeHtml in your custom preset.
    • Preset Defaults: The preset field option now has a default value (the first configured preset) instead of a validator. If using setPluginConfig(), the default is the first preset defined, not defaultHtml.
    • Dependencies: yup and sanitize-html are no longer dependencies of this plugin. If your code relies on them being installed via this plugin, add them to your project explicitly.
    • Node.js Support: Supported versions are now >= 20.0.0 <= 26.x.x.
  7. Migrate from v4 to v5

    master

    Version 5 introduces breaking changes, including a new configuration method and the renaming of the default preset to defaultHtml.

    Scenario 1: No custom configuration

    If you do not use custom configuration, you must update your fields to use the defaultHtml preset to avoid errors.

    Option 1 (Recommended): Update your Content-Type schemas manually or via the Content-Type Builder to use defaultHtml instead of default.

    Option 2: Apply custom configuration in src/admin/app.[js|tsx] and rename the defaultHtmlPreset.name to default to maintain compatibility with existing schemas.

    Scenario 2: Custom configuration

    If you use custom configuration, you must migrate from the old object-based presets format to an array-based format.

    Key changes for custom config:

    • presets is now an array of objects (previously an object).
    • The field property in a Preset is replaced by name and description.
    • CKEditor packages are no longer on globalThis. You must import them directly from ckeditor5.
    • theme and presets are no longer merged with defaults; they overwrite them.
    • The style plugin and material color palette have been removed from the default preset.
    • The plugin theme now uses Strapi's theme variables.

    Move your configuration from config/ckeditor.js into src/admin/app.[js|tsx] using setPluginConfig().

    // Example of the new v5 configuration pattern in src/admin/app.tsx
    import { Autoformat, Bold, Italic, Essentials, Heading } from 'ckeditor5';
    import {
      setPluginConfig,
      defaultHtmlPreset,
      StrapiMediaLib,
      StrapiUploadAdapter,
    } from '@_sh/strapi-plugin-ckeditor';
    
    const CKEConfig = () => ({
      presets: [
        {
          ...defaultHtmlPreset,
          editorConfig: {
            ...defaultHtmlPreset.editorConfig,
            toolbar: ['heading', '|', 'bold', 'italic', '|', 'strapiMediaLib', '|', 'undo', 'redo'],
          },
        },
        {
          name: 'myCustomPreset',
          description: 'My custom preset',
          editorConfig: {
            licenseKey: 'GPL',
            plugins: [
              Autoformat,
              Bold,
              Italic,
              Essentials,
              Heading,
              StrapiMediaLib,
              StrapiUploadAdapter,
            ],
            toolbar: ['heading', '|', 'bold', 'italic', '|', 'strapiMediaLib', '|', 'undo', 'redo'],
          },
        },
      ],
    });
    
    export default {
      register() {
        setPluginConfig(CKEConfig());
      },
    };
  8. Install the CKEditor plugin for Strapi v5

    master

    To install the CKEditor plugin in a Strapi v5 project, add the @_sh/strapi-plugin-ckeditor package using your preferred package manager and then rebuild your Strapi application.

    Note for Strapi v4 users: Version 3 of this plugin is the compatible version for Strapi v4. It is supported until March 2026 but only receives essential bug fixes. The configuration and features differ significantly from the v5 version.

    yarn add @_sh/strapi-plugin-ckeditor
    yarn build
  9. Configure the CKEditor plugin in Strapi

    master

    The CKEditor plugin configuration must be defined on the front-end (Strapi Admin panel). You can modify the plugin's configuration using setPluginConfig, getPluginPresets, and getPluginTheme.

    Key Rules for Configuration:

    • Timing: You must invoke configuration functions before the admin panel's bootstrap lifecycle function. It is recommended to call them inside the register lifecycle function.
    • Immutability: The configuration becomes immutable after the first invocation of setPluginConfig.
    • Overwriting: Providing a new configuration object via setPluginConfig will overwrite the default values for the properties provided (e.g., providing a new presets array will replace all default presets).

    Available Functions:

    • setPluginConfig(config: PluginConfig): Sets the global plugin configuration.
    • getPluginPresets(): Returns the current presets object where keys match preset names.
    • getPluginTheme(): Returns the current theme object.
    import { setPluginConfig } from '@_sh/strapi-plugin-ckeditor';
    
    export default {
      register() {
        setPluginConfig({
          presets: [/* your presets */],
          theme: { /* your theme */ }
        });
      },
    };
  10. Integrate Strapi Media Library with CKEditor

    master

    To allow users to upload and manage images/files via the Strapi Media Library directly within the CKEditor interface, include the following two plugins in your preset's editorConfig.plugins and editorConfig.toolbar arrays:

    • StrapiMediaLib: The CKEditor plugin component.
    • StrapiUploadAdapter: The upload logic adapter.
    import { StrapiMediaLib, StrapiUploadAdapter } from '@_sh/strapi-plugin-ckeditor';
    
    // Inside your preset configuration:
    const myPreset = {
      editorConfig: {
        plugins: [ /* ...other plugins */, StrapiMediaLib, StrapiUploadAdapter ],
        toolbar: [ /* ...other toolbar items */, 'strapiMediaLib' ],
      }
    };