Contribute to this guide

guideSource editing

The source editing feature lets you view and edit the source of your document.

# Demo

Use the editor below to see the source editing plugin in action. 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. The feature introduces HTML elements not yet covered by the official plugins.

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

# Additional feature information

The source editing plugin is a low-level document editing interface, while all the buttons and dropdowns located in the toolbar are high-level ones.

Changes made to the document source will be applied to the editor’s data model only if the editor understands (via loaded plugins) the given syntax. You will lose all changes that the editor features cannot understand. For example, if the editor does not have a horizontal line plugin loaded, the <hr> tag added in the document source will be removed upon exit from the source editing mode.

# 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.

Remember that Markdown syntax is very 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.

# Installation

Currently, the source editing mode is supported in the classic editor. The source editing feature can be used with CKEditor 5 collaboration features except for CKEditor 5 real-time collaboration. If you would like to use the real-time collaboration mode and, for some reason, you would like to also enable source editing, please contact us.

To add this feature to your rich-text editor, install the @ckeditor/ckeditor5-source-editing package:

npm install --save @ckeditor/ckeditor5-source-editing

And add it to your plugin list configuration:

import { SourceEditing } from '@ckeditor/ckeditor5-source-editing';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ SourceEditing, /* ... */ ],
        toolbar: [ 'sourceEditing', /* ... */ ]
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

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

import { SourceEditing } from '@ckeditor/ckeditor5-source-editing';
import { Markdown } from '@ckeditor/ckeditor5-markdown-gfm';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ SourceEditing, Markdown, /* ... */ ],
        toolbar: [ 'sourceEditing', /* ... */ ]
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

Read more about installing plugins.

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

  • 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 SourceEditing plugin registers:

  • The 'sourceEditing' UI button component.

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.