tiptap/docs/api/extensions/character-count.md
Philipp Kühn 5daa870b09
feat: add some improvements to CharacterCount extension (#2256), fix #1049, fix #1550, fix #1839, fix #2245
* fix a bug when exceeding the character limit

* find a better way to limit the doc size

* check paste events

* add storage method

* refactoring

* use textBetween instead of textContent

* return early if no limit is set

* add words method to storage

* show word count in charactercount demo

Co-authored-by: Philipp Kühn <philippkuehn@MacBook-Pro-von-Philipp.local>
2021-12-08 21:26:30 +01:00

73 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: Limit the number of characters in your editor, or at least count them.
icon: calculator-line
---
# CharacterCount
[![Version](https://img.shields.io/npm/v/@tiptap/extension-character-count.svg?label=version)](https://www.npmjs.com/package/@tiptap/extension-character-count)
[![Downloads](https://img.shields.io/npm/dm/@tiptap/extension-character-count.svg)](https://npmcharts.com/compare/@tiptap/extension-character-count?minimal=true)
The `CharacterCount` extension limits the number of allowed character to a specific length. Thats it, thats all.
## Installation
```bash
npm install @tiptap/extension-character-count
```
## Settings
### limit
The maximum number of characters that should be allowed.
Default: `0`
```js
CharacterCount.configure({
limit: 240,
})
```
### mode
The mode by which the size is calculated.
Default: `'textSize'`
```js
CharacterCount.configure({
mode: 'nodeSize',
})
```
## Storage
### characters()
Get the number of characters for the current document.
```js
editor.storage.characterCount.characters()
// Get the size of a specific node.
editor.storage.characterCount.characters({ node: someCustomNode })
// Overwrite the default `mode`.
editor.storage.characterCount.characters({ mode: 'nodeSize' })
```
### words()
Get the number of words for the current document.
```js
editor.storage.characterCount.words()
// Get the number of words for a specific node.
editor.storage.characterCount.words({ node: someCustomNode })
```
## Source code
[packages/extension-character-count/](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-character-count/)
## Usage
https://embed.tiptap.dev/preview/Extensions/CharacterCount