Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add blog post estimated reading time #2531

Merged
merged 6 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
12 changes: 10 additions & 2 deletions packages/docusaurus-theme-classic/src/theme/BlogPostItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const MONTHS = [
'December',
];

function ReadingTime(props) {
const {readingTime} = props;
if (typeof readingTime === 'undefined') {
return null;
}
return <> 路 {Math.ceil(readingTime)} min read</>;
}

function BlogPostItem(props) {
const {
children,
Expand All @@ -37,7 +45,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 +68,7 @@ function BlogPostItem(props) {
</TitleHeading>
<div className="margin-vert--md">
<time dateTime={date} className={styles.blogPostDate}>
{month} {day}, {year}
{month} {day}, {year} <ReadingTime readingTime={readingTime} />
</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
50 changes: 49 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2587,13 +2587,41 @@
"@types/node" "*"
"@types/webpack" "*"

"@types/lodash.flatmap@^4.5.6":
version "4.5.6"
resolved "https://registry.yarnpkg.com/@types/lodash.flatmap/-/lodash.flatmap-4.5.6.tgz#5f1ea80cebe403f0fbfcc1b5ad75cd09dd8b5785"
integrity sha512-ELNrUL9q+MB7AACaHivWIsKDFDgYhHE3/svXhqvDJgONtn2c467Cy87nEb7CEDvfaGCPv91lPaW596I8s5oiNQ==
dependencies:
"@types/lodash" "*"

"@types/lodash.groupby@^4.6.6":
version "4.6.6"
resolved "https://registry.yarnpkg.com/@types/lodash.groupby/-/lodash.groupby-4.6.6.tgz#4d9b61a4d8b0d83d384975cabfed4c1769d6792e"
integrity sha512-kwg3T7Ia63KtDNoQQR8hKrLHCAgrH4I44l5uEMuA6JCbj7DiSccaV4tNV1vbjtAOpX990SolVthJCmBVtRVRgw==
dependencies:
"@types/lodash" "*"

"@types/lodash.kebabcase@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@types/lodash.kebabcase/-/lodash.kebabcase-4.1.6.tgz#07b07aeca6c0647836de46f87a3cdfff72166c8e"
integrity sha512-+RAD9pCAa8kuVyCYTeDNiwBXwD/0u0p+hos3NSqD+tXTjJextbfF3farfYB+ssAKgEssoewXEtBsfwBpsI7gsA==
dependencies:
"@types/lodash" "*"

"@types/lodash.pick@^4.4.6":
version "4.4.6"
resolved "https://registry.yarnpkg.com/@types/lodash.pick/-/lodash.pick-4.4.6.tgz#ae4e8f109e982786313bb6aac4b1a73aefa6e9be"
integrity sha512-u8bzA16qQ+8dY280z3aK7PoWb3fzX5ATJ0rJB6F+uqchOX2VYF02Aqa+8aYiHiHgPzQiITqCgeimlyKFy4OA6g==
dependencies:
"@types/lodash" "*"

"@types/lodash.pickby@^4.6.6":
version "4.6.6"
resolved "https://registry.yarnpkg.com/@types/lodash.pickby/-/lodash.pickby-4.6.6.tgz#3dc39c2b38432f7a0c5e5627b0d5c0e3878b4f35"
integrity sha512-NFa13XxlMd9eFi0UFZFWIztpMpXhozbijrx3Yb1viYZphT7jyopIFVoIRF4eYMjruWNEG1rnyrRmg/8ej9T8Iw==
dependencies:
"@types/lodash" "*"

"@types/lodash@*", "@types/lodash@^4.14.149", "@types/lodash@^4.14.53":
version "4.14.149"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
Expand Down Expand Up @@ -10229,6 +10257,11 @@ lodash.filter@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.filter/-/lodash.filter-4.6.0.tgz#668b1d4981603ae1cc5a6fa760143e480b4c4ace"
integrity sha1-ZosdSYFgOuHMWm+nYBQ+SAtMSs4=

lodash.flatmap@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.flatmap/-/lodash.flatmap-4.5.0.tgz#ef8cbf408f6e48268663345305c6acc0b778702e"
integrity sha1-74y/QI9uSCaGYzRTBcaswLd4cC4=

lodash.flatten@^4.2.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
Expand All @@ -10249,6 +10282,11 @@ lodash.get@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

lodash.groupby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
integrity sha1-Cwih3PaDl8OXhVwyOXg4Mt90A9E=

lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
Expand Down Expand Up @@ -10284,11 +10322,16 @@ lodash.padstart@^4.6.1:
resolved "https://registry.yarnpkg.com/lodash.padstart/-/lodash.padstart-4.6.1.tgz#d2e3eebff0d9d39ad50f5cbd1b52a7bce6bb611b"
integrity sha1-0uPuv/DZ05rVD1y9G1KnvOa7YRs=

lodash.pick@^4.2.1:
lodash.pick@^4.2.1, lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.pickby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.pickby/-/lodash.pickby-4.6.0.tgz#7dea21d8c18d7703a27c704c15d3b84a67e33aff"
integrity sha1-feoh2MGNdwOifHBMFdO4SmfjOv8=

lodash.reduce@^4.4.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.reduce/-/lodash.reduce-4.6.0.tgz#f1ab6b839299ad48f784abbf476596f03b914d3b"
Expand Down Expand Up @@ -13890,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