Skip to content

Commit 112ff4f

Browse files
committed
fix sourceToPermalink resolution bug
1 parent d4ad1ca commit 112ff4f

3 files changed

Lines changed: 60 additions & 22 deletions

File tree

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
isUnlisted,
2727
isDraft,
2828
readLastUpdateData,
29-
type SourceToPermalink,
3029
} from '@docusaurus/utils';
3130
import {validateBlogPostFrontMatter} from './frontMatter';
3231
import {type AuthorsMap, getAuthorsMap, getBlogPostAuthors} from './authors';
@@ -44,12 +43,6 @@ export function truncate(fileString: string, truncateMarker: RegExp): string {
4443
return fileString.split(truncateMarker, 1).shift()!;
4544
}
4645

47-
export function getSourceToPermalink(blogPosts: BlogPost[]): SourceToPermalink {
48-
return new Map(
49-
blogPosts.map(({metadata: {source, permalink}}) => [source, permalink]),
50-
);
51-
}
52-
5346
export function paginateBlogPosts({
5447
blogPosts,
5548
basePageUrl,

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import {
1919
getDataFilePath,
2020
DEFAULT_PLUGIN_ID,
2121
resolveMarkdownLinkPathname,
22+
type SourceToPermalink,
2223
} from '@docusaurus/utils';
2324
import {
24-
getSourceToPermalink,
2525
getBlogTags,
2626
paginateBlogPosts,
2727
shouldBeListed,
@@ -49,6 +49,32 @@ import type {RuleSetUseItem} from 'webpack';
4949

5050
const PluginName = 'docusaurus-plugin-content-blog';
5151

52+
// TODO this is bad, we should have a better way to do this (new lifecycle?)
53+
// The source to permalink is currently a mutable map passed to the mdx loader
54+
// for link resolution
55+
function createSourceToPermalinkHelper() {
56+
const sourceToPermalink: SourceToPermalink = new Map();
57+
58+
function computeSourceToPermalink(content: BlogContent): SourceToPermalink {
59+
return new Map(
60+
content.blogPosts.map(({metadata: {source, permalink}}) => [
61+
source,
62+
permalink,
63+
]),
64+
);
65+
}
66+
67+
// Mutable map update :/
68+
function update(content: BlogContent): void {
69+
sourceToPermalink.clear();
70+
computeSourceToPermalink(content).forEach((value, key) => {
71+
sourceToPermalink.set(key, value);
72+
});
73+
}
74+
75+
return {get: () => sourceToPermalink, update};
76+
}
77+
5278
export default async function pluginContentBlog(
5379
context: LoadContext,
5480
options: PluginOptions,
@@ -95,6 +121,8 @@ export default async function pluginContentBlog(
95121
contentPaths,
96122
});
97123

124+
const sourceToPermalinkHelper = createSourceToPermalinkHelper();
125+
98126
return {
99127
name: PluginName,
100128

@@ -193,6 +221,8 @@ export default async function pluginContentBlog(
193221
},
194222

195223
async contentLoaded({content, actions}) {
224+
sourceToPermalinkHelper.update(content);
225+
196226
await createAllRoutes({
197227
baseUrl,
198228
content,
@@ -206,7 +236,7 @@ export default async function pluginContentBlog(
206236
return translateContent(content, translationFiles);
207237
},
208238

209-
configureWebpack(_config, isServer, utils, content) {
239+
configureWebpack() {
210240
const {
211241
admonitions,
212242
rehypePlugins,
@@ -216,7 +246,6 @@ export default async function pluginContentBlog(
216246
beforeDefaultRehypePlugins,
217247
} = options;
218248

219-
const sourceToPermalink = getSourceToPermalink(content.blogPosts);
220249
const contentDirs = getContentPathList(contentPaths);
221250

222251
function createMDXLoader(): RuleSetUseItem {
@@ -263,7 +292,7 @@ export default async function pluginContentBlog(
263292
resolveMarkdownLink: ({linkPathname, sourceFilePath}) => {
264293
const permalink = resolveMarkdownLinkPathname(linkPathname, {
265294
sourceFilePath,
266-
sourceToPermalink,
295+
sourceToPermalink: sourceToPermalinkHelper.get(),
267296
siteDir,
268297
contentPaths,
269298
});

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
5858
import type {DocFile, FullVersion} from './types';
5959
import type {RuleSetUseItem} from 'webpack';
6060

61+
// TODO this is bad, we should have a better way to do this (new lifecycle?)
62+
// The source to permalink is currently a mutable map passed to the mdx loader
63+
// for link resolution
64+
function createSourceToPermalinkHelper() {
65+
const sourceToPermalink: SourceToPermalink = new Map();
66+
67+
function computeSourceToPermalink(content: LoadedContent): SourceToPermalink {
68+
const allDocs = content.loadedVersions.flatMap((v) => v.docs);
69+
return new Map(allDocs.map(({source, permalink}) => [source, permalink]));
70+
}
71+
72+
// Mutable map update :/
73+
function update(content: LoadedContent): void {
74+
sourceToPermalink.clear();
75+
computeSourceToPermalink(content).forEach((value, key) => {
76+
sourceToPermalink.set(key, value);
77+
});
78+
}
79+
80+
return {get: () => sourceToPermalink, update};
81+
}
82+
6183
export default async function pluginContentDocs(
6284
context: LoadContext,
6385
options: PluginOptions,
@@ -84,6 +106,8 @@ export default async function pluginContentDocs(
84106
// TODO env should be injected into all plugins
85107
const env = process.env.NODE_ENV as DocEnv;
86108

109+
const sourceToPermalinkHelper = createSourceToPermalinkHelper();
110+
87111
return {
88112
name: 'docusaurus-plugin-content-docs',
89113

@@ -228,6 +252,8 @@ export default async function pluginContentDocs(
228252
},
229253

230254
async contentLoaded({content, actions}) {
255+
sourceToPermalinkHelper.update(content);
256+
231257
const versions: FullVersion[] = content.loadedVersions.map(toFullVersion);
232258

233259
await createAllRoutes({
@@ -258,16 +284,6 @@ export default async function pluginContentDocs(
258284
// Trailing slash is important, see https://github.com/facebook/docusaurus/pull/3970
259285
.map(addTrailingPathSeparator);
260286

261-
// TODO this does not re-run when content gets updated in dev!
262-
// it's probably better to restore a mutable cache in the plugin
263-
function getSourceToPermalink(): SourceToPermalink {
264-
const allDocs = content.loadedVersions.flatMap((v) => v.docs);
265-
return new Map(
266-
allDocs.map(({source, permalink}) => [source, permalink]),
267-
);
268-
}
269-
const sourceToPermalink = getSourceToPermalink();
270-
271287
function createMDXLoader(): RuleSetUseItem {
272288
const loaderOptions: MDXLoaderOptions = {
273289
admonitions: options.admonitions,
@@ -302,7 +318,7 @@ export default async function pluginContentDocs(
302318
);
303319
const permalink = resolveMarkdownLinkPathname(linkPathname, {
304320
sourceFilePath,
305-
sourceToPermalink,
321+
sourceToPermalink: sourceToPermalinkHelper.get(),
306322
siteDir,
307323
contentPaths: version,
308324
});

0 commit comments

Comments
 (0)