Skip to content

Replace reading-time npm package by Intl.Segmenter API #11086

@slorber

Description

@slorber

Have you read the Contributing Guidelines on issues?

Motivation

We could get rid of some npm lib/code in our blog readingTime API by relying on built-in Intl.Segmenter APIs (Node.js v16+)

const segmenter = new Intl.Segmenter('en', { granularity: 'word' });
const segments = segmenter.segment('This is a test');

let count = 0;
for (const seg of segments) {
  console.log({seg});
  count++;
}

console.log(count);

The library code is more complex, likely less performant, and less accurate (doesn't take the locale as param) than the native implementation
https://github.com/ngryman/reading-time/blob/master/src/reading-time.ts


This is not a critical feature but if someone from the community want to contribute, PRs welcome.

I'd appreciate if you start by writing unit tests / snapshots for the reading-time lib usage.

Then we can see how the new implementation gives a different result depending on inputs.

Based on that result we can decide if it's safe to ship immediately, or if it requires a future flag + breaking change.

Note that Docusaurus exposes a way to pass your own readingTime function, and we may need to change that API: https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-blog#ReadingTimeFn

Self-service

  • I'd be willing to do some initial work on this proposal myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIf you are just getting started with Docusaurus, this issue should be a good place to begin.help wantedAsking for outside help and/or contributions to this particular issue or PR.proposalThis issue is a proposal, usually non-trivial change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions