mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 14:06:59 +02:00
feat(content-blog): new readingTime plugin option (#5702)
This commit is contained in:
parent
92002b6bd3
commit
9ad6de2b85
8 changed files with 179 additions and 1 deletions
|
@ -77,6 +77,8 @@ describe('blogFeed', () => {
|
|||
type: [feedType],
|
||||
copyright: 'Copyright',
|
||||
},
|
||||
readingTime: ({content, defaultReadingTime}) =>
|
||||
defaultReadingTime({content}),
|
||||
} as PluginOptions,
|
||||
);
|
||||
|
||||
|
@ -111,6 +113,8 @@ describe('blogFeed', () => {
|
|||
type: [feedType],
|
||||
copyright: 'Copyright',
|
||||
},
|
||||
readingTime: ({content, defaultReadingTime}) =>
|
||||
defaultReadingTime({content}),
|
||||
} as PluginOptions,
|
||||
);
|
||||
const feedContent =
|
||||
|
|
|
@ -16,6 +16,7 @@ import {
|
|||
BlogContentPaths,
|
||||
BlogMarkdownLoaderOptions,
|
||||
BlogTags,
|
||||
ReadingTimeFunction,
|
||||
} from './types';
|
||||
import {
|
||||
parseMarkdownFile,
|
||||
|
@ -111,6 +112,10 @@ async function parseBlogPostMarkdownFile(blogSourceAbsolute: string) {
|
|||
};
|
||||
}
|
||||
|
||||
const defaultReadingTime: ReadingTimeFunction = ({content, options}) => {
|
||||
return readingTime(content, options).minutes;
|
||||
};
|
||||
|
||||
async function processBlogSourceFile(
|
||||
blogSourceRelative: string,
|
||||
contentPaths: BlogContentPaths,
|
||||
|
@ -227,7 +232,13 @@ async function processBlogSourceFile(
|
|||
date,
|
||||
formattedDate,
|
||||
tags: normalizeFrontMatterTags(tagsBasePath, frontMatter.tags),
|
||||
readingTime: showReadingTime ? readingTime(content).minutes : undefined,
|
||||
readingTime: showReadingTime
|
||||
? options.readingTime({
|
||||
content,
|
||||
frontMatter,
|
||||
defaultReadingTime,
|
||||
})
|
||||
: undefined,
|
||||
truncated: truncateMarker?.test(content) || false,
|
||||
authors,
|
||||
},
|
||||
|
|
|
@ -41,6 +41,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
|
|||
path: 'blog',
|
||||
editLocalizedFiles: false,
|
||||
authorsMapPath: 'authors.yml',
|
||||
readingTime: ({content, defaultReadingTime}) => defaultReadingTime({content}),
|
||||
};
|
||||
|
||||
export const PluginOptionSchema = Joi.object<PluginOptions>({
|
||||
|
@ -113,4 +114,5 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
|
|||
language: Joi.string(),
|
||||
}).default(DEFAULT_OPTIONS.feedOptions),
|
||||
authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath),
|
||||
readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime),
|
||||
});
|
||||
|
|
|
@ -12,6 +12,7 @@ import type {
|
|||
ContentPaths,
|
||||
} from '@docusaurus/utils/lib/markdownLinks';
|
||||
import {Overwrite} from 'utility-types';
|
||||
import {BlogPostFrontMatter} from './blogFrontMatter';
|
||||
|
||||
export type BlogContentPaths = ContentPaths;
|
||||
|
||||
|
@ -46,6 +47,24 @@ export type EditUrlFunction = (editUrlParams: {
|
|||
locale: string;
|
||||
}) => string | undefined;
|
||||
|
||||
// Duplicate from ngryman/reading-time to keep stability of API
|
||||
type ReadingTimeOptions = {
|
||||
wordsPerMinute?: number;
|
||||
wordBound?: (char: string) => boolean;
|
||||
};
|
||||
|
||||
export type ReadingTimeFunction = (params: {
|
||||
content: string;
|
||||
frontMatter?: BlogPostFrontMatter & Record<string, unknown>;
|
||||
options?: ReadingTimeOptions;
|
||||
}) => number;
|
||||
|
||||
export type ReadingTimeFunctionOption = (
|
||||
params: Required<Omit<Parameters<ReadingTimeFunction>[0], 'options'>> & {
|
||||
defaultReadingTime: ReadingTimeFunction;
|
||||
},
|
||||
) => number | undefined;
|
||||
|
||||
export type PluginOptions = RemarkAndRehypePluginOptions & {
|
||||
id?: string;
|
||||
path: string;
|
||||
|
@ -76,6 +95,7 @@ export type PluginOptions = RemarkAndRehypePluginOptions & {
|
|||
editLocalizedFiles?: boolean;
|
||||
admonitions: Record<string, unknown>;
|
||||
authorsMapPath: string;
|
||||
readingTime: ReadingTimeFunctionOption;
|
||||
};
|
||||
|
||||
// Options, as provided in the user config (before normalization)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue