# autoformat/inlineautoformatediting

module

<a id="type-definitions">

## Type Definitions

<a id="typedef-AutoformatTestCallback">

### `AutoformatTestCallback`

<a id="functions">

## Functions

<a id="function-inlineAutoformatEditing">

### `inlineAutoformatEditing( editor, plugin, testRegexpOrCallback, formatCallback ) → void`

Enables autoformatting mechanism for a given [`Editor`](module_core_editor_editor-Editor.md).

It formats the matched text by applying the given model attribute or by running the provided formatting callback. On every [data change](module_engine_model_document-ModelDocument.md#event-change:data) 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).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-autoformat/src/inlineautoformatediting.ts#L95)

#### Parameters

* `editor: Editor`

  The editor instance.

* `plugin: Autoformat`

  The autoformat plugin instance.

* `testRegexpOrCallback: RegExp | AutoformatTestCallback`

  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.

  ```typescript
  // 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.

  ```typescript
  {
  	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.
  	]
  }
  ```

* `formatCallback: ( writer: ModelWriter, rangesToFormat: Array<ModelRange> ) => ( boolean | undefined )`

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

  ```typescript
  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 );
  	}
  } );
  ```

#### Returns

* `void`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
