mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-29 08:57:03 +02:00
fix(docs, blog): Markdown link resolution does not support hot reload (#10185)
This commit is contained in:
parent
0eb7b64aac
commit
a7afd9cc87
7 changed files with 93 additions and 44 deletions
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue