Enhanced source code editing

The enhanced source code editing feature allows for viewing and editing the source code of the document in a handy modal window (compatible with all editor types) with syntax highlighting, autocompletion and more.

Unlock this feature with a CKEditor Paid Plan. Sign up for a free trial, or select the Plan that provides access to all the premium features you need.

# Demo

Use any of the editors below to see the enhanced source code editing plugin in action. Please note that enhanced source code editing works with editor types, taking advantage of the built–in modal system.

Toggle the source editing mode Source editing and make some changes in the HTML code (for example, add a new paragraph or an ordered list). Then leave the source editing mode and see that the changes are present in the document content.

You can also use one of the many CKEditor 5 features available in the toolbar and check how they render in the HTML source. Notice the collapsible table of contents, available thanks to the general HTML support feature, which introduces HTML elements not yet covered by the official plugins.

This demo presents a limited set of features. Visit the feature-rich editor example to see more in action.

# Additional feature information

# Themes

The enhanced source code editing feature enables you to customize the look and feel of the editor containing source code, allowing for seamless visual integration with your application. You can use one of the many already available themes or create your own custom theme.

# Adding a theme

To load a custom theme, set the sourceEditingEnhanced.theme configuration option to the name of the provided theme.

import { oneDark } from '@codemirror/theme-one-dark';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options

        sourceEditingEnhanced: {
            theme: oneDark
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

# Creating a custom theme

To learn how to create your own custom themes, take a look at the official CodeMirror documentation.

# Code completion

The feature provides code completion support for HTML tags and attributes. The attribute completion list is context-aware and will only show ones that are allowed in the current context. It will also help you close open HTML tags. Start typing and the list will appear next to the caret.

Use and to browse the list. Press Enter to select the item.

# Code folding

The feature allows you to fold and unfold parts of the document source code. This is especially useful when working with large documents.

Click the folding icon in the gutter to collapse or expand the code block. Use the Ctrl+Shift+[ (⌘⇧[ on macOS) and Ctrl+Shift+] (⌘⇧] on macOS) keyboard shortcuts to fold and unfold the code. Use Ctrl+Alt+[ (⌘⌥[ on macOS) and Ctrl+Alt+] (⌘⌥] on macOS) to fold and unfold all code blocks.

# Bracket and tag matching

The feature highlights matching brackets in the source code. When you place the cursor next to a bracket, the matching bracket will be highlighted. If you move the caret inside a tag, the opening and closing tags will be highlighted. If a HTML tag is not closed properly, the editor will also highlight it to let you know.

# Keyboard shortcuts

The source editor supports multiple keyboard shortcuts to make your work more efficient. Please refer to the full list of available keyboard shortcuts in a dedicated guide.

# Installation

⚠️ New import paths

Starting with version 42.0.0, we changed the format of import paths. This guide uses the new, shorter format. Refer to the Packages in the legacy setup guide if you use an older version of CKEditor 5.

After installing the editor, add the feature to your plugin list and toolbar configuration:

import { ClassicEditor } from 'ckeditor5';
import { SourceEditingEnhanced } from 'ckeditor5-premium-features';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '<YOUR_LICENSE_KEY>',
		plugins: [ SourceEditingEnhanced, /* ... */ ],
		toolbar: [ 'sourceEditingEnhanced', /* ... */ ],
		sourceEditingEnhanced: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );

# Activating the feature

To use this premium feature, you need to activate it with proper credentials. Refer to the License key and activation guide for details.

# Markdown source view

The source editing plugin also works well with the Markdown output plugin. You do not need any special configuration: just add the plugin to the editor, and the source editing mode will display Markdown instead of HTML.

To utilize the Markdown source editing mode, just add the Markdown output plugin to the editor.

import { ClassicEditor, Markdown } from 'ckeditor5';
import { SourceEditingEnhanced } from 'ckeditor5-premium-features';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '<YOUR_LICENSE_KEY>',
		plugins: [ SourceEditingEnhanced, Markdown, /* ... */ ],
		toolbar: [ 'sourceEditingEnhanced', /* ... */ ],
		sourceEditingEnhanced: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );

Remember that Markdown syntax is simple and does not cover all the rich-text features. This means that some features provided by CKEditor 5 – either native or introduced by the GHS feature – can only be presented as native HTML as they have no Markdown equivalent. Such features will be stripped in the source view below.

Markdown output 🛫

CKEditor 5 can be configured to output Markdown instead of HTML. Markdown is a lightweight markup language that you can use to add formatting to plain text documents. Use the Source button to check and edit the Markdown source code of this content.

The editor-produced Markdown output supports most essential features, like links, different kinds of emphasis, inline code formatting, or code blocks:

p {
    text-align: center;
    color: red;
}

Markdown input 🛬

Thanks to the autoformatting feature, you can use Markdown syntax when writing. Try it out - use these (or any other) Markdown shortcuts in the editor to format the content on the fly 🚀!

Inline formatting Shortcut
Bold Type ** or __ around your text.
Italic Type * or _ around your text.
Code Type ˋ around your text.
Strikethrough Type ~~ around your text.

Shh! 🤫 Markdown has very basic support for tables. Some advanced table-related features like table or cell styling were disabled in this demo.

Block formatting

You can also use Markdown to create various text blocks, such as:

  • Block quotes - Start a line with followed by a space.
  • Headings:
    1. Heading 1 - Start a line with # followed by a space.
    2. Heading 2 - Start a line with ## followed by a space.
    3. Heading 3 - Start a line with ### followed by a space.
  • Lists, including nested ones:
    • Numbered lists - Start a line with 1. or 1) followed by a space.
    • Bulleted lists - Start a line with * or - followed by a space.
    • To-do lists - Start a line with [ ] or [x] followed by a space to insert an unchecked or checked list item.
  • Code blocks - Start a line with ˋˋˋ.
  • Horizontal lines - Start a line with ---.

# Limitations and incompatibilities

The source editing plugin is a low-level document editing method, which allows you to directly alter the document data source. This presents incompatibilities with some editor features which highly rely on the editor mechanisms and architecture.

Please read this section carefully, and check if these problems may apply to your editor configuration.

# Real-time collaboration

Source editing used during real-time collaboration brings a severe risk of data loss in a way that may be difficult for a user to notice and understand.

After you switch to source editing, incoming changes performed by remote users are not reflected in the source code. When you switch back (saving the source code), all changes done in the meantime by other users will be overwritten.

Due to this risk, these features are not allowed to be used together by default. When both are added to the editor, it will throw an error. You have to explicitly enable source editing mode for real-time collaboration, acknowledging this risk.

To enable the features, set the sourceEditingEnhanced.allowCollaborationFeatures configuration flag to true.

# Comments and track changes

The comments and track changes features use markers to mark affected parts of the document.

In the source editing mode, it is possible for a user to modify these markers’ boundaries. The user can change the range of a comment or tracked change, or remove them. This presents a potential problem related to users permissions.

The editor will show a warning in the browser’s console when these plugins and source editing plugin are used together.

To turn off the warning, set the sourceEditingEnhanced.allowCollaborationFeatures configuration flag to true.

# Support for various HTML elements

The editor will only save changes made to the document data source if it “understands” them. It means one of the loaded plugins must recognize the given syntax (HTML or Markdown). Any syntax unsupported by the editor will be filtered out upon save.

For example, if the editor does not have the horizontal line plugin loaded, the <hr> tag added in the document source will be removed upon exit from the source editing mode.

To avoid that, make sure that your editor configuration has all the necessary plugins that handle various HTML tags.

In many cases, to enable advanced modifications through the source editing, you might need to enable the HTML embed feature and the General HTML support feature, or write a plugin that will enable a given HTML tag or attribute support.

# HTML normalization

When the editor reads the source data, it converts the data to a normalized, high-level, abstract data model structure, on which it operates. This structure differs from the HTML or Markdown code.

Note, that the same document “state” may be described using HTML in various ways.

For example, <strong><em>Foo</em></strong> and <i><b>Foo</b></i> both produce the “Foo” text with bold and italic styling. Both will be represented the same when loaded to the internal editor data model.

When the editor data model is later converted back to the document source data, it will be normalized regardless of the original input. In effect, <i><b>Foo</b></i> will eventually become <strong><em>Foo</em></strong>.

This limitation is a direct consequence of the core editor architecture and cannot be worked around. Although it is possible to change the editor’s final output, the input data will always be normalized to that output format.

# Revision history

Saving the modified document source is internally executed through replacing the old data with the new one. As a consequence, it will be represented in revision history as a total replace change (insertion + deletion).

The editor will show a warning in the browser’s console when revision history and source editing are loaded together.

To turn the warning off, set the sourceEditingEnhanced.allowCollaborationFeatures configuration flag to true.

# Restricted editing

Restricted editing is not enforced in the source editing mode. The user will be able to edit any part of the document, as well as remove the markers that mark the boundaries of restricted areas.

The editor will show a warning in the browser’s console when restricted editing and source editing are loaded together.

There are other source-related CKEditor 5 features you may want to check:

  • Source editing support – The open-source source editing feature for CKEditor 5. Limited to the classic editor. only, does not support syntax highlighting, keyboard shortcuts, autocompletion, or themes.
  • General HTML support – Allows you to enable HTML features (elements, attributes, classes, styles) that are not supported by other dedicated CKEditor 5 plugins.
  • Full page HTML – Allows using CKEditor 5 to edit entire HTML pages, from <html> to </html>, including the page metadata.
  • HTML embed – Allows embedding an arbitrary HTML snippet in the editor.
  • Markdown output – Allows for Markdown output instead of HTML output.

# Common API

The SourceEditingEnhanced plugin registers:

  • The 'sourceEditingEnhanced' UI button component.
  • The 'editSource' command implemented by EditSourceCommand.

You can execute the command using the editor.execute() method:

// Open the "Edit source" modal window.
editor.execute( 'editSource' );

We recommend using the official CKEditor 5 inspector for development and debugging. It will give you tons of useful information about the state of the editor such as internal data structures, selection, commands, and many more.

# Contribute

The source code of the feature is available on GitHub at https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-source-editing.