Skip to content

Commit 6635ca3

Browse files
garry00107slorber
andauthored
refactor(content-blog): decouple getTagsFile from generateBlogPosts (#11707)
* refactor(content-blog): decouple getTagsFile from generateBlogPosts * read blog authors/tags in parallel --------- Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent d173a77 commit 6635ca3

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

packages/docusaurus-plugin-content-blog/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@docusaurus/utils-common": "3.9.2",
4141
"@docusaurus/utils-validation": "3.9.2",
4242
"cheerio": "1.0.0-rc.12",
43+
"combine-promises": "^1.1.0",
4344
"feed": "^4.2.2",
4445
"fs-extra": "^11.1.1",
4546
"lodash": "^4.17.21",

packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
DEFAULT_VCS_CONFIG,
1414
} from '@docusaurus/utils';
1515
import {fromPartial} from '@total-typescript/shoehorn';
16-
import {normalizePluginOptions} from '@docusaurus/utils-validation';
16+
import {
17+
normalizePluginOptions,
18+
getTagsFile,
19+
} from '@docusaurus/utils-validation';
1720
import tree from 'tree-node-cli';
1821
import {DEFAULT_OPTIONS, validateOptions} from '../options';
1922
import {generateBlogPosts} from '../blogUtils';
@@ -84,10 +87,13 @@ async function testGenerateFeeds(
8487
baseUrl: '/',
8588
});
8689

90+
const tagsFile = await getTagsFile({contentPaths, tags: options.tags});
91+
8792
const blogPosts = await generateBlogPosts(
8893
contentPaths,
8994
context,
9095
options,
96+
tagsFile,
9197
authorsMap,
9298
);
9399

packages/docusaurus-plugin-content-blog/src/blogUtils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
normalizeTags,
2727
aliasedSitePathToRelativePath,
2828
} from '@docusaurus/utils';
29-
import {getTagsFile} from '@docusaurus/utils-validation';
29+
3030
import {validateBlogPostFrontMatter} from './frontMatter';
3131
import {getBlogPostAuthors} from './authors';
3232
import {reportAuthorsProblems} from './authorsProblems';
@@ -388,6 +388,7 @@ export async function generateBlogPosts(
388388
contentPaths: BlogContentPaths,
389389
context: LoadContext,
390390
options: PluginOptions,
391+
tagsFile: TagsFile | null,
391392
authorsMap?: AuthorsMap,
392393
): Promise<BlogPost[]> {
393394
const {include, exclude} = options;
@@ -401,10 +402,6 @@ export async function generateBlogPosts(
401402
ignore: exclude,
402403
});
403404

404-
// TODO this should be done outside of this function
405-
// directly in plugin loadContent()
406-
const tagsFile = await getTagsFile({contentPaths, tags: options.tags});
407-
408405
async function doProcessBlogSourceFile(blogSourceFile: string) {
409406
try {
410407
return await processBlogSourceFile(

packages/docusaurus-plugin-content-blog/src/index.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import path from 'path';
99
import logger from '@docusaurus/logger';
10+
import combinePromises from 'combine-promises';
11+
1012
import {
1113
normalizeUrl,
1214
docuHash,
@@ -20,7 +22,10 @@ import {
2022
resolveMarkdownLinkPathname,
2123
getLocaleConfig,
2224
} from '@docusaurus/utils';
23-
import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
25+
import {
26+
getTagsFilePathsToWatch,
27+
getTagsFile,
28+
} from '@docusaurus/utils-validation';
2429
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
2530
import {
2631
getBlogTags,
@@ -227,22 +232,32 @@ export default async function pluginContentBlog(
227232
const baseBlogUrl = normalizeUrl([baseUrl, routeBasePath]);
228233
const blogTagsListPath = normalizeUrl([baseBlogUrl, tagsBasePath]);
229234

230-
const authorsMap = await getAuthorsMap({
231-
contentPaths,
232-
authorsMapPath,
233-
authorsBaseRoutePath: normalizeUrl([
235+
async function getAuthorsMapChecked() {
236+
const result = await getAuthorsMap({
237+
contentPaths,
238+
authorsMapPath,
239+
authorsBaseRoutePath: normalizeUrl([
240+
baseUrl,
241+
routeBasePath,
242+
authorsBasePath,
243+
]),
234244
baseUrl,
235-
routeBasePath,
236-
authorsBasePath,
237-
]),
238-
baseUrl,
245+
});
246+
checkAuthorsMapPermalinkCollisions(result);
247+
return result;
248+
}
249+
250+
// Read all the input files in parallel
251+
const {authorsMap, tagsFile} = await combinePromises({
252+
authorsMap: getAuthorsMapChecked(),
253+
tagsFile: getTagsFile({contentPaths, tags: options.tags}),
239254
});
240-
checkAuthorsMapPermalinkCollisions(authorsMap);
241255

242256
let blogPosts = await generateBlogPosts(
243257
contentPaths,
244258
context,
245259
options,
260+
tagsFile,
246261
authorsMap,
247262
);
248263
blogPosts = await applyProcessBlogPosts({

0 commit comments

Comments
 (0)