Word count and character count
The WordCount
feature provides a possibility to track the number of words and characters written in the rich-text editor.
It is a feature crucial for writing professionals but also for students and marketers. Keeping track of the document’s word or character count helps planning the overall content volume, meeting assignment requirements or keeping within the preset text length goal. The word or character count aids quick assessment of work that was done and needs yet to be done as well as supports the writing process planning and management.
# Demo
Type some more or edit the content and observe the counter below the main editor window react in real-time.
The example above was created by using the following HTML page structure:
You can use the code below to set up the WYSIWYG editor with the word and character count features as in the example above.
# Related features
CKEditor 5 provides other productivity-boosting features that you may find helpful:
- Proofreading, spelling and grammar checking – Track and correct any possible errors as you type.
- Autosave – Never lose you content by accident, stay safe and automatically save.
- Autoformatting – Employ Markdown syntax for a faster and more efficient editing process.
- Automatic text transformation – Automatically turn predefined snippets into their improved forms using the autocorrect feature.
# Configuration
The word count and character count feature is quite flexible and there are a few configuration options available.
# Configuring the container
There are two ways how you can inject the word count statistics into your page:
- By using the
WordCount#wordCountContainer
property as shown in the example above. - By specifying where the word count feature should insert its container which can be done by using
config.wordCount.container
.
The word count plugin renders its output as:
If you wish to render the statistics differently, see the update
event.
# Changing the output
There are two configuration options available that change the output of the word count and character count features:
- If the
config.wordCount.displayWords
option is set tofalse
, the word counter will be hidden. - If the
config.wordCount.displayCharacters
option is set tofalse
, the character counter will be hidden.
# Reacting to updates
You can execute your custom callback every time content statistics change by defining config.wordCount.onUpdate
in the editor configuration:
Note: For performance reasons, your callback will be throttled and may not be up–to–date. Use the characters
and words
plugin properties to retrieve the precise numbers on demand.
Below you can play with a demo post editor with a soft 120 characters limit and a progress chart below indicating how many characters are in the content. The progress chart changes its color as the limit is near or exceeded. Type in the editor to see the feature in action. See the code used to create the demo listed below in this section.
Post editor with word count
Here is the HTML structure used to create the customized word and character count implementation above:
# Installation
To add this feature to your rich-text editor, install the @ckeditor/ckeditor5-word-count
package:
And add it to your plugin list configuration:
Read more about installing plugins.
# Common API
The WordCount
plugin provides:
-
The
wordCountContainer
property. It returns a self-updating HTML element which is updated with the current number of words and characters in the editor. You can remove the “Words” or “Characters” counters with a proper configuration of theconfig.wordCount.displayWords
andconfig.wordCount.displayCharacters
options. -
The
update
event, fired whenever the plugins update the number of counted words and characters. You can use it to run a custom callback function with updated values:Alternatively, you can use
config.wordCount.onUpdate
to register a similar callback via the editor configuration.Note: For performance reasons, the
update
event is throttled so the statistics may not be up–to–date. Use thecharacters
andwords
plugin properties to retrieve the precise numbers on demand. -
The
characters
andwords
properties from which you can retrieve the stats at any moment.
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 in https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-word-count.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.