Skip to content

Commit

Permalink
feat(v2): add blog post estimated reading time (#2531)
Browse files Browse the repository at this point in the history
* feat: add estimated reading time to blog posts

* docs: add showReadingTime on plugin docs

* test: update plugin-content-blog tests to cover readingTime

* Update index.js

* Update using-plugins.md

* Update index.js

Co-authored-by: Yangshun Tay <[email protected]>
  • Loading branch information
JoseRenan and yangshun authored Apr 5, 2020
1 parent c576faa commit 95fdfe7
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"loader-utils": "^1.2.3",
"lodash.kebabcase": "^4.1.1"
"lodash.kebabcase": "^4.1.1",
"reading-time": "^1.2.0"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('loadBlog', () => {
...{prevItem: undefined},
}).toEqual({
permalink: '/blog/2019/01/01/date-matter',
readingTime: 0.02,
source: path.join('@site', pluginPath, 'date-matter.md'),
title: 'date-matter',
description: `date inside front matter`,
Expand All @@ -68,6 +69,7 @@ describe('loadBlog', () => {
.metadata,
).toEqual({
permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
readingTime: 0.01,
source: path.join(
'@site',
pluginPath,
Expand All @@ -89,6 +91,7 @@ describe('loadBlog', () => {
...{prevItem: undefined},
}).toEqual({
permalink: noDatePermalink,
readingTime: 0.01,
source: noDateSource,
title: 'no date',
description: `no date`,
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fs from 'fs-extra';
import globby from 'globby';
import path from 'path';
import readingTime from 'reading-time';
import {Feed} from 'feed';
import {PluginOptions, BlogPost, DateLink} from './types';
import {parse, normalizeUrl, aliasedSitePath} from '@docusaurus/utils';
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function generateBlogPosts(
{siteConfig, siteDir}: LoadContext,
options: PluginOptions,
) {
const {include, routeBasePath, truncateMarker} = options;
const {include, routeBasePath, truncateMarker, showReadingTime} = options;

if (!fs.existsSync(blogDir)) {
return [];
Expand Down Expand Up @@ -144,6 +145,9 @@ export async function generateBlogPosts(
date,
tags: frontMatter.tags,
title: frontMatter.title,
readingTime: showReadingTime
? readingTime(content).minutes
: undefined,
truncated: truncateMarker?.test(content) || false,
},
});
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DEFAULT_OPTIONS: PluginOptions = {
blogPostComponent: '@theme/BlogPostPage',
blogTagsListComponent: '@theme/BlogTagsListPage',
blogTagsPostsComponent: '@theme/BlogTagsPostsPage',
showReadingTime: true,
remarkPlugins: [],
rehypePlugins: [],
truncateMarker: /<!--\s*(truncate)\s*-->/, // Regex.
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface PluginOptions {
remarkPlugins: string[];
rehypePlugins: string[];
truncateMarker: RegExp;
showReadingTime: boolean;
feedOptions?: {
type: FeedType;
title?: string;
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface MetaData {
date: Date;
tags: (Tag | string)[];
title: string;
readingTime?: number;
prevItem?: Paginator;
nextItem?: Paginator;
truncated: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function BlogPostItem(props) {
truncated,
isBlogPostPage = false,
} = props;
const {date, permalink, tags} = metadata;
const {date, permalink, tags, readingTime} = metadata;
const {author, title} = frontMatter;

const authorURL = frontMatter.author_url || frontMatter.authorURL;
Expand All @@ -60,7 +60,8 @@ function BlogPostItem(props) {
</TitleHeading>
<div className="margin-vert--md">
<time dateTime={date} className={styles.blogPostDate}>
{month} {day}, {year}
{month} {day}, {year}{' '}
{readingTime && <> · {Math.ceil(readingTime)} min read</>}
</time>
</div>
<div className="avatar margin-vert--md">
Expand Down
4 changes: 4 additions & 0 deletions website/docs/using-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ module.exports = {
* Truncate marker, can be a regex or string.
*/
truncateMarker: /<!--\s*(truncate)\s*-->/,
/**
* Show estimated reading time for the blog post.
*/
showReadingTime: true,
/**
* Blog feed
* If feedOptions is undefined, no rss feed will be generated
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13933,6 +13933,11 @@ readdirp@~3.3.0:
dependencies:
picomatch "^2.0.7"

reading-time@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/reading-time/-/reading-time-1.2.0.tgz#ced71c06715762f805506328dcc1fd45d8249ac4"
integrity sha512-5b4XmKK4MEss63y0Lw0vn0Zn6G5kiHP88mUnD8UeEsyORj3sh1ghTH0/u6m1Ax9G2F4wUZrknlp6WlIsCvoXVA==

realpath-native@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-1.1.0.tgz#2003294fea23fb0672f2476ebe22fcf498a2d65c"
Expand Down

0 comments on commit 95fdfe7

Please sign in to comment.