@ckeditor/ckeditor5-autoformat

  • Version 43.1.0
  • Published
  • 297 kB
  • 5 dependencies
  • GPL-2.0-or-later license

Install

npm i @ckeditor/ckeditor5-autoformat
yarn add @ckeditor/ckeditor5-autoformat
pnpm add @ckeditor/ckeditor5-autoformat

Overview

Autoformatting feature for CKEditor 5.

Index

Functions

function blockAutoformatEditing

blockAutoformatEditing: (
editor: Editor,
plugin: Autoformat,
pattern: RegExp,
callbackOrCommand: string | ((context: { match: RegExpExecArray }) => unknown)
) => void;
  • Creates a listener triggered on event in the document. Calls the callback when inserted text matches the regular expression or the command name if provided instead of the callback.

    Examples of usage:

    To convert a paragraph into heading 1 when - is typed, using just the command name:

    blockAutoformatEditing( editor, plugin, /^\- $/, 'heading1' );

    To convert a paragraph into heading 1 when - is typed, using just the callback:

    blockAutoformatEditing( editor, plugin, /^\- $/, ( context ) => {
    const { match } = context;
    const headingLevel = match[ 1 ].length;
    editor.execute( 'heading', {
    formatId: `heading${ headingLevel }`
    } );
    } );

    Parameter editor

    The editor instance.

    Parameter plugin

    The autoformat plugin instance.

    Parameter pattern

    The regular expression to execute on just inserted text. The regular expression is tested against the text from the beginning until the caret position.

    Parameter callbackOrCommand

    The callback to execute or the command to run when the text is matched. In case of providing the callback, it receives the following parameter: * match RegExp.exec() result of matching the pattern to inserted text.

function inlineAutoformatEditing

inlineAutoformatEditing: (
editor: Editor,
plugin: Autoformat,
testRegexpOrCallback: RegExp | TestCallback,
formatCallback: (
writer: Writer,
rangesToFormat: Array<Range>
) => boolean | undefined
) => void;
  • Enables autoformatting mechanism for a given .

    It formats the matched text by applying the given model attribute or by running the provided formatting callback. On every in the model document the autoformatting engine checks the text on the left of the selection and executes the provided action if the text matches given criteria (regular expression or callback).

    Parameter editor

    The editor instance.

    Parameter plugin

    The autoformat plugin instance.

    Parameter testRegexpOrCallback

    The regular expression or callback to execute on text. Provided regular expression *must* have three capture groups. The first and the third capture group should match opening and closing delimiters. The second capture group should match the text to format.

    // Matches the `**bold text**` pattern.
    // There are three capturing groups:
    // - The first to match the starting `**` delimiter.
    // - The second to match the text to format.
    // - The third to match the ending `**` delimiter.
    inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );

    When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter. The function should return proper "ranges" to delete and format.

    {
    remove: [
    [ 0, 1 ], // Remove the first letter from the given text.
    [ 5, 6 ] // Remove the 6th letter from the given text.
    ],
    format: [
    [ 1, 5 ] // Format all letters from 2nd to 5th.
    ]
    }

    Parameter formatCallback

    A callback to apply actual formatting. It should return false if changes should not be applied (e.g. if a command is disabled).

    inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
    const command = editor.commands.get( 'bold' );
    if ( !command.isEnabled ) {
    return false;
    }
    const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
    for ( let range of validRanges ) {
    writer.setAttribute( 'bold', true, range );
    }
    } );

Classes

class Autoformat

class Autoformat extends Plugin {}
  • Enables a set of predefined autoformatting actions.

    For a detailed overview, check the feature guide and the .

property pluginName

static readonly pluginName: string;

property requires

static readonly requires: readonly [any];

method afterInit

afterInit: () => void;

Package Files (4)

Dependencies (5)

Dev Dependencies (0)

No dev dependencies.

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@ckeditor/ckeditor5-autoformat.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@ckeditor/ckeditor5-autoformat)
  • HTML
    <a href="https://www.jsdocs.io/package/@ckeditor/ckeditor5-autoformat"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>