fix(docs, blog): Markdown link resolution does not support hot reload (#10185)

This commit is contained in:
Sébastien Lorber 2024-05-31 17:47:36 +02:00 committed by GitHub
parent 0eb7b64aac
commit a7afd9cc87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 93 additions and 44 deletions

View file

@ -45,14 +45,6 @@ export function truncate(fileString: string, truncateMarker: RegExp): string {
return fileString.split(truncateMarker, 1).shift()!;
}
export function getSourceToPermalink(blogPosts: BlogPost[]): {
[aliasedPath: string]: string;
} {
return Object.fromEntries(
blogPosts.map(({metadata: {source, permalink}}) => [source, permalink]),
);
}
export function paginateBlogPosts({
blogPosts,
basePageUrl,

View file

@ -19,10 +19,10 @@ import {
getDataFilePath,
DEFAULT_PLUGIN_ID,
resolveMarkdownLinkPathname,
type SourceToPermalink,
} from '@docusaurus/utils';
import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
import {
getSourceToPermalink,
getBlogTags,
paginateBlogPosts,
shouldBeListed,
@ -50,6 +50,33 @@ import type {RuleSetUseItem} from 'webpack';
const PluginName = 'docusaurus-plugin-content-blog';
// TODO this is bad, we should have a better way to do this (new lifecycle?)
// The source to permalink is currently a mutable map passed to the mdx loader
// for link resolution
// see https://github.com/facebook/docusaurus/pull/10185
function createSourceToPermalinkHelper() {
const sourceToPermalink: SourceToPermalink = new Map();
function computeSourceToPermalink(content: BlogContent): SourceToPermalink {
return new Map(
content.blogPosts.map(({metadata: {source, permalink}}) => [
source,
permalink,
]),
);
}
// Mutable map update :/
function update(content: BlogContent): void {
sourceToPermalink.clear();
computeSourceToPermalink(content).forEach((value, key) => {
sourceToPermalink.set(key, value);
});
}
return {get: () => sourceToPermalink, update};
}
export default async function pluginContentBlog(
context: LoadContext,
options: PluginOptions,
@ -96,6 +123,8 @@ export default async function pluginContentBlog(
contentPaths,
});
const sourceToPermalinkHelper = createSourceToPermalinkHelper();
return {
name: PluginName,
@ -201,6 +230,8 @@ export default async function pluginContentBlog(
},
async contentLoaded({content, actions}) {
sourceToPermalinkHelper.update(content);
await createAllRoutes({
baseUrl,
content,
@ -214,7 +245,7 @@ export default async function pluginContentBlog(
return translateContent(content, translationFiles);
},
configureWebpack(_config, isServer, utils, content) {
configureWebpack() {
const {
admonitions,
rehypePlugins,
@ -224,7 +255,6 @@ export default async function pluginContentBlog(
beforeDefaultRehypePlugins,
} = options;
const sourceToPermalink = getSourceToPermalink(content.blogPosts);
const contentDirs = getContentPathList(contentPaths);
function createMDXLoader(): RuleSetUseItem {
@ -271,7 +301,7 @@ export default async function pluginContentBlog(
resolveMarkdownLink: ({linkPathname, sourceFilePath}) => {
const permalink = resolveMarkdownLinkPathname(linkPathname, {
sourceFilePath,
sourceToPermalink,
sourceToPermalink: sourceToPermalinkHelper.get(),
siteDir,
contentPaths,
});