krzysztofradomski

payload plugin reading time

by krzysztofradomski
Custom fields
v3
^3.84.1

Overview

Summary generated from the README

Adds computed readingTime and wordCount number fields to configured collections and fills them in a beforeChange hook, so values are stored with the document. It walks the Lexical state, including nested block fields, and supports per-locale words-per-minute plus character counting for CJK locales. Requires Payload 3 and a Lexical rich-text field on each collection.

Features

  • Adds readingTime (minutes) and wordCount fields, each renamable or disableable
  • Lexical walker that recurses into block node fields and nested rich-text states
  • Per-locale words-per-minute settings with a default fallback
  • Character-based counting for CJK locales such as zh, ja and ko
  • Admin placement in sidebar or main form, stacked or in a row
  • Reuses existing fields with the same name so labels can be customized

Use cases

  • Show estimated reading time on blog posts without computing it at render time
  • Query or sort documents by word count using persisted fields
  • Support multilingual sites where reading speed differs by locale

From the author

Computed reading time and word count for Payload CMS Lexical content

Installation

npm i payload-plugin-reading-time

1

Stars

0

Forks

0

Open Issues

6 months ago

Last Updated

117

Weekly Downloads

v1.0.0

npm Version

70.8 KB

Unpacked Size

0

Dependencies

GitHub stars

10 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20261
Dec 5, 20261
Dec 4, 20261
Dec 3, 20261
Dec 2, 20261
Dec 1, 20261
Nov 30, 20261
Nov 29, 20261
Nov 28, 20261
Nov 27, 20261
Nov 26, 20261
Nov 25, 20261
Nov 24, 20261
Nov 23, 20261
Nov 22, 20261
Nov 21, 20261
Nov 20, 20261
Nov 19, 20261
Nov 18, 20261
Nov 17, 20261
Nov 16, 20261
Nov 15, 20261
Nov 14, 20261
Nov 13, 20261
Nov 12, 20261
Nov 11, 20261
Nov 10, 20261
Nov 9, 20261
Nov 8, 20261
Nov 7, 20261
Nov 6, 20261
Nov 5, 20261
Nov 4, 20261
Nov 3, 20261
Nov 2, 20261
Nov 1, 20261
Oct 31, 20261
Oct 30, 20261
Oct 29, 20261
Oct 28, 20261
Oct 27, 20261
Oct 26, 20261
Oct 25, 20261
Oct 24, 20261
Oct 23, 20261
Oct 22, 20261
Oct 21, 20261
Oct 20, 20261
Oct 19, 20261
Oct 18, 20261
Oct 17, 20261
Oct 16, 20261
Oct 15, 20261
Oct 14, 20261
Oct 13, 20261
Oct 12, 20261
Oct 11, 20261
Oct 10, 20261
Oct 9, 20261
Oct 8, 20261
Oct 7, 20261
Oct 6, 20261
Oct 5, 20261
Oct 4, 20261
Oct 3, 20261
Oct 2, 20261
Oct 1, 20261
Sep 30, 20261
Sep 29, 20261
Sep 28, 20261
Sep 27, 20261
Sep 26, 20261
Sep 25, 20261
Sep 24, 20261
Sep 23, 20261
Sep 22, 20261
Sep 21, 20261
Sep 20, 20261
Sep 19, 20261
Sep 18, 20261
Sep 17, 20261
Sep 16, 20261
Sep 15, 20261
Sep 14, 20261
Sep 13, 20261
Sep 12, 20261
Sep 11, 20261
Sep 10, 20261
Sep 9, 20261
Sep 8, 20261
Sep 7, 20261
npm weekly downloads

105+7 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026105
Dec 5, 202694
Dec 4, 202690
Dec 3, 202622
Dec 2, 202614
Dec 1, 202618
Nov 30, 202618
Nov 29, 202615
Nov 28, 202612
Nov 27, 202612
Nov 26, 202615
Nov 25, 202619
Nov 24, 202611
Nov 23, 202611
Nov 22, 202613
Nov 21, 202620
Nov 20, 202650
Nov 19, 202671
Nov 18, 202687
Nov 17, 2026105
Nov 16, 2026108
Nov 15, 2026113
Nov 14, 2026113
Nov 13, 2026101
Nov 12, 202687
Nov 11, 202680
Nov 10, 2026107
Nov 9, 2026109
Nov 8, 2026117
Nov 7, 2026141
Nov 6, 2026168
Nov 5, 2026161
Nov 4, 2026195
Nov 3, 2026154
Nov 2, 2026154
Nov 1, 2026142
Oct 31, 2026113
Oct 30, 202673
Oct 29, 202670
Oct 28, 202622
Oct 27, 202628
Oct 26, 202628
Oct 25, 202627
Oct 24, 202630
Oct 23, 202623
Oct 22, 202635
Oct 21, 202650
Oct 20, 202645
Oct 19, 202644
Oct 18, 202647
Oct 17, 202647
Oct 16, 202651
Oct 15, 202648
Oct 14, 202650
Oct 13, 202666
Oct 12, 202696
Oct 11, 202695
Oct 10, 202693
Oct 9, 2026107
Oct 8, 2026134
Oct 7, 2026141
Oct 6, 2026123
Oct 5, 202694
Oct 4, 202695
Oct 3, 202695
Oct 2, 2026103
Oct 1, 2026100
Sep 30, 202690
Sep 29, 202691
Sep 28, 202689
Sep 27, 202694
Sep 26, 202696
Sep 25, 202678
Sep 24, 202662
Sep 23, 202696
Sep 22, 202696
Sep 21, 202696
Sep 20, 202695
Sep 19, 2026111
Sep 18, 2026116
Sep 17, 2026107
Sep 16, 202686
Sep 15, 202697
Sep 14, 2026109
Sep 13, 2026104
Sep 12, 202687
Sep 11, 202679
Sep 10, 202697
Sep 9, 2026100
Sep 8, 2026104
Sep 7, 202698

Each point is npm's rolling 7-day download count.

Health Score

42/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1250710535-root.svg)](https://payloaddirectory.dev/plugins/1250710535-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1250710535-root"><img src="https://payloaddirectory.dev/badge/1250710535-root.svg" alt="Payload Directory"></a>
Updated May 27, 2026Created May 26, 2026Published May 27, 2026

README

payload-plugin-reading-time

Computed reading time and word count for Payload CMS Lexical content.

  • Walks Lexical state JSON correctly, including block-node children (the place where the naive getTextContent() shortcut quietly loses text).
  • Computes on beforeChange, so reading time and word count are persisted fields — fast to query, no per-render cost.
  • Per-locale words-per-minute with CJK character counting.

Features

  • Adds two computed fields to configured collections (each can be renamed or disabled):
    • readingTime — number (minutes), sidebar
    • wordCount — number, sidebar
  • Block-node-aware Lexical walker (recurses into block fields, picks up nested rich-text states)
  • Per-locale WPM, with CJK fallback to character counting

Requirements

  • Payload ^3.84.0
  • A Lexical-based rich-text field on the collection you want to enrich

Installation

pnpm add payload-plugin-reading-time

Setup

import { payloadReadingTime } from 'payload-plugin-reading-time'

export default buildConfig({
  plugins: [
    payloadReadingTime({
      collections: {
        // Use defaults: rich-text field is `content`, adds `readingTime`
        // and `wordCount` fields.
        posts: true,

        // Override per-collection:
        docs: {
          richTextField: 'body',
          wordCountField: false, // skip word count for this collection
        },
      },

      // Optional — defaults below.
      defaultWordsPerMinute: 250,
      wordsPerMinute: { 'en-US': 280, zh: 500 },
      characterBasedLocales: ['zh', 'ja', 'ko'],
    }),
  ],
})

The plugin appends fields to your collection (it never replaces existing fields with the same name, so you can pre-declare them if you want to customize labels).

Configuration

Plugin options

OptionTypeDefaultDescription
collectionsRecord<string, true | {…}>—Map of slug → per-collection config.
defaultWordsPerMinutenumber250Used when no per-locale value matches.
wordsPerMinuteRecord<string, number>{}Words-per-minute per locale or language prefix.
characterBasedLocalesstring[]['zh','ja','ko']Locales that count characters instead of words.
disabledbooleanfalseSkip hook wiring but keep schema additions.

Per-collection options

Pass true to use defaults, or an object with any of:

OptionTypeDefaultDescription
richTextFieldstring'content'Dot-path to the Lexical rich-text field used as input.
readingTimeFieldstring | false'readingTime'Name of the field to store reading time. Pass false to skip.
wordCountFieldstring | false'wordCount'Name of the field to store word count. Pass false to skip.
adminobjectsee belowAdmin-UI placement for readingTime and wordCount. See Admin placement.

If a field with the chosen name already exists on the collection, the plugin reuses it — handy when you want a custom label, description, or admin position.

Admin placement

admin controls where the two computed fields render in the document view.

OptionTypeDefaultDescription
position'main' | 'sidebar''sidebar'Render the fields in the sidebar (compact, default) or inline at the bottom of the main form.
layout'stacked' | 'row''stacked'stacked keeps each field on its own line; row puts both side-by-side at 50% width inside a Row Field.
payloadReadingTime({
  collections: {
    // Default: each on its own line in the sidebar.
    posts: true,

    // Both fields in one row, in the main form.
    docs: {
      richTextField: 'body',
      admin: { position: 'main', layout: 'row' },
    },

    // Sidebar, side-by-side.
    notes: {
      admin: { position: 'sidebar', layout: 'row' },
    },
  },
})

Notes:

  • layout: 'row' is only applied when both readingTimeField and wordCountField are enabled and neither already exists on the collection. Otherwise the layout silently falls back to stacked.
  • If you pre-declare either field on your collection, the plugin reuses your declaration as-is (so you keep full control over its admin config) and never wraps it in a row.

Exports

ImportDescription
payload-plugin-reading-timepayloadReadingTime plugin, types, and pure helpers (walkLexical, computeReadingTime, countWords, resolveWordsPerMinute, isCharacterBased, createComputeReadingTimeHook).
payload-plugin-reading-time/rscServer-component-safe re-exports of the walker and computation helpers.

How it works

  1. On every save, the plugin's beforeChange hook reads the configured rich-text field from data (falling back to originalDoc).
  2. The walker traverses the Lexical state, accumulating plain text. Block nodes are entered through their fields property — nested Lexical states inside blocks contribute text to the parent document.
  3. Words (or CJK characters, per locale) are counted from the plain text and divided by the configured WPM.

Out of scope

  • AI summarization or auto-tagging (separate plugin category).
  • Reading time per section (only total is computed; possible future addition).
  • Lexical 1.x (Payload 3 / current Lexical only).

Local development

The plugin's source lives in src/ and is consumed by the local app in dev/ under its published name (payload-plugin-reading-time, payload-plugin-reading-time/rsc) via dev/tsconfig.json paths and the next.config.mjs alias map — no link:. self-dependency required.

Run pnpm dev for the local Payload admin (defaults to http://localhost:3000), pnpm test:unit for the Vitest unit suite, pnpm test:int for the integration suite (in-memory MongoDB), and pnpm test:e2e for the Playwright suite.

License

MIT

Similar plugins