test(reading-time): Unit tests for calculating reading time (#11116)

This commit is contained in:
Shreedhar Bhat 2025-04-30 16:17:51 +05:30 committed by GitHub
parent 16ebd55ef6
commit fadb6d2607
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 233 additions and 8 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,23 @@
<!doctype html>
<html lang="en" dir="ltr" class="plugin-native plugin-id-default" data-has-hydrated="false">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.7.0">
<title data-rh="true">Page Not Found | Docusaurus blog website fixture</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://docusaurus.io/img/docusaurus-social-card.jpg"><meta data-rh="true" name="twitter:image" content="https://docusaurus.io/img/docusaurus-social-card.jpg"><meta data-rh="true" property="og:url" content="https://docusaurus.io/blog/tags/global-tag-permalink (en)"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="Page Not Found | Docusaurus blog website fixture"><link data-rh="true" rel="icon" href="/img/docusaurus.ico"><link data-rh="true" rel="canonical" href="https://docusaurus.io/blog/tags/global-tag-permalink (en)"><link data-rh="true" rel="alternate" href="https://docusaurus.io/blog/tags/global-tag-permalink (en)" hreflang="en"><link data-rh="true" rel="alternate" href="https://docusaurus.io/blog/tags/global-tag-permalink (en)" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://X1Z85QJPUV-dsn.algolia.net" crossorigin="anonymous"><link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="Docusaurus blog website fixture RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="Docusaurus blog website fixture Atom Feed">
<link rel="alternate" type="application/json" href="/blog/feed.json" title="Docusaurus blog website fixture JSON Feed">
<link rel="search" type="application/opensearchdescription+xml" title="Docusaurus blog website fixture" href="/opensearch.xml"><link rel="stylesheet" href="/assets/css/styles.5eca0ed4.css">
<script src="/assets/js/runtime~main.972f9f8c.js" defer="defer"></script>
<script src="/assets/js/main.52184938.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><defs>
<symbol id="theme-svg-external-link" viewBox="0 0 24 24"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"/></symbol>
</defs></svg>
<script>!function(){var t="light";var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return window.localStorage.getItem("theme")}catch(t){}}();document.documentElement.setAttribute("data-theme",e||t),document.documentElement.setAttribute("data-theme-choice",e||t)}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><div role="region" aria-label="Skip to main content"><a class="skipToContent_c5VT" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="theme-layout-navbar navbar navbar--fixed-top navbarHideable_gzXY"><div class="navbar__inner"><div class="theme-layout-navbar-left navbar__items"><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/docusaurus.svg" alt="Docusaurus Logo" class="themedComponent_NoEC themedComponent--light_xEpK"><img src="/img/docusaurus_keytar.svg" alt="Docusaurus Logo" class="themedComponent_NoEC themedComponent--dark__8yu"></div><b class="navbar__title text--truncate">Docusaurus</b></a></div><div class="theme-layout-navbar-right navbar__items navbar__items--right"><div class="toggle_vYTj colorModeToggle_XGli"><button class="clean-btn toggleButton_dQHP toggleButtonDisabled_T3qH" type="button" disabled="" title="system mode" aria-label="Switch between dark and light mode (currently system mode)"><svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" class="toggleIcon_dOhG lightToggleIcon_VoLy"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" class="toggleIcon_dOhG darkToggleIcon_Y09b"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" class="toggleIcon_dOhG systemToggleIcon_hBI2"><path fill="currentColor" d="m12 21c4.971 0 9-4.029 9-9s-4.029-9-9-9-9 4.029-9 9 4.029 9 9 9zm4.95-13.95c1.313 1.313 2.05 3.093 2.05 4.95s-0.738 3.637-2.05 4.95c-1.313 1.313-3.093 2.05-4.95 2.05v-14c1.857 0 3.637 0.737 4.95 2.05z"></path></svg></button></div><div class="navbarSearchContainer_J_Aa"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search (Command+K)"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20" aria-hidden="true"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="theme-layout-main main-wrapper mainWrapper_cF7l"><main class="container margin-vert--xl"><div class="row"><div class="col col--6 col--offset-3"><h1 class="hero__title">Page Not Found</h1><p>We could not find what you were looking for.</p><p>Please contact the owner of the site that linked you to the original URL and let them know their link is broken.</p></div></div></main></div></div>
</body>
</html>

File diff suppressed because one or more lines are too long

View file

@ -237,6 +237,10 @@ exports[`atom has custom xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -273,7 +277,11 @@ exports[`atom has custom xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;
@ -683,6 +691,10 @@ exports[`atom has xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -719,7 +731,11 @@ exports[`atom has xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;
@ -900,6 +916,10 @@ exports[`json has custom xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -936,7 +956,11 @@ exports[`json has custom xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;
@ -1253,6 +1277,10 @@ exports[`json has xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -1289,7 +1317,11 @@ exports[`json has xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;
@ -1527,6 +1559,10 @@ exports[`rss has custom xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -1563,7 +1599,11 @@ exports[`rss has custom xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;
@ -1949,6 +1989,10 @@ exports[`rss has xslt files for feed: blog tree 1`] = `
├── atom.css ├── atom.css
├── atom.xml ├── atom.xml
├── atom.xsl ├── atom.xsl
├── authors
│ ├── index.html
│ └── slorber-custom-permalink-localized
│ └── index.html
├── blog-with-links ├── blog-with-links
│ └── index.html │ └── index.html
├── custom-atom.css ├── custom-atom.css
@ -1985,7 +2029,11 @@ exports[`rss has xslt files for feed: blog tree 1`] = `
│ │ └── index.html │ │ └── index.html
│ ├── date │ ├── date
│ │ └── index.html │ │ └── index.html
│ └── index.html │ ├── global-tag-permalink (en)
│ │ └── index.html
│ ├── index.html
│ └── inline-tag
│ └── index.html
└── unlisted └── unlisted
└── index.html" └── index.html"
`; `;

View file

@ -0,0 +1,56 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {calculateReadingTime} from '../readingTime';
describe('calculateReadingTime', () => {
it('calculates reading time for empty content', () => {
expect(calculateReadingTime('')).toBe(0);
});
it('calculates reading time for short content', () => {
const content = 'This is a short test content.';
expect(calculateReadingTime(content)).toBe(0.03);
});
it('calculates reading time for long content', () => {
const content = 'This is a test content. '.repeat(100);
expect(calculateReadingTime(content)).toBe(2.5);
});
it('respects custom words per minute', () => {
const content = 'This is a test content. '.repeat(100);
expect(calculateReadingTime(content, {wordsPerMinute: 100})).toBe(5);
});
it('handles content with special characters', () => {
const content = 'Hello! How are you? This is a test...';
expect(calculateReadingTime(content)).toBe(0.04);
});
it('handles content with multiple lines', () => {
const content = `This is line 1.
This is line 2.
This is line 3.`;
expect(calculateReadingTime(content)).toBe(0.06);
});
it('handles content with HTML tags', () => {
const content = '<p>This is a <strong>test</strong> content.</p>';
expect(calculateReadingTime(content)).toBe(0.025);
});
it('handles content with markdown', () => {
const content = '# Title\n\nThis is **bold** and *italic* text.';
expect(calculateReadingTime(content)).toBe(0.04);
});
it('handles CJK content', () => {
const content = '你好,世界!这是一段测试内容。';
expect(calculateReadingTime(content)).toBe(0.06);
});
});

View file

@ -9,7 +9,6 @@ import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import _ from 'lodash'; import _ from 'lodash';
import logger from '@docusaurus/logger'; import logger from '@docusaurus/logger';
import readingTime from 'reading-time';
import { import {
parseMarkdownFile, parseMarkdownFile,
normalizeUrl, normalizeUrl,
@ -32,6 +31,7 @@ import {getTagsFile} from '@docusaurus/utils-validation';
import {validateBlogPostFrontMatter} from './frontMatter'; import {validateBlogPostFrontMatter} from './frontMatter';
import {getBlogPostAuthors} from './authors'; import {getBlogPostAuthors} from './authors';
import {reportAuthorsProblems} from './authorsProblems'; import {reportAuthorsProblems} from './authorsProblems';
import {calculateReadingTime} from './readingTime';
import type {TagsFile} from '@docusaurus/utils'; import type {TagsFile} from '@docusaurus/utils';
import type {LoadContext, ParseFrontMatter} from '@docusaurus/types'; import type {LoadContext, ParseFrontMatter} from '@docusaurus/types';
import type { import type {
@ -211,7 +211,7 @@ async function parseBlogPostMarkdownFile({
} }
const defaultReadingTime: ReadingTimeFunction = ({content, options}) => const defaultReadingTime: ReadingTimeFunction = ({content, options}) =>
readingTime(content, options).minutes; calculateReadingTime(content, options);
async function processBlogSourceFile( async function processBlogSourceFile(
blogSourceRelative: string, blogSourceRelative: string,

View file

@ -0,0 +1,29 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import readingTime from 'reading-time';
const DEFAULT_WORDS_PER_MINUTE = 200;
interface ReadingTimeOptions {
wordsPerMinute?: number;
wordBound?: (char: string) => boolean;
}
/**
* Calculates the reading time for a given content string.
* Uses the reading-time package under the hood.
*/
export function calculateReadingTime(
content: string,
options: ReadingTimeOptions = {},
): number {
const wordsPerMinute = options.wordsPerMinute ?? DEFAULT_WORDS_PER_MINUTE;
const {wordBound} = options;
return readingTime(content, {wordsPerMinute, ...(wordBound && {wordBound})})
.minutes;
}