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

@ -19,6 +19,8 @@ import {
createSlugger,
resolveMarkdownLinkPathname,
DEFAULT_PLUGIN_ID,
type SourceToPermalink,
type TagsFile,
} from '@docusaurus/utils';
import {
getTagsFile,
@ -47,7 +49,6 @@ import {
} from './translations';
import {createAllRoutes} from './routes';
import {createSidebarsUtils} from './sidebars/utils';
import type {TagsFile} from '@docusaurus/utils';
import type {Options as MDXLoaderOptions} from '@docusaurus/mdx-loader';
import type {
@ -59,9 +60,32 @@ import type {
LoadedVersion,
} from '@docusaurus/plugin-content-docs';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {SourceToPermalink, DocFile, FullVersion} from './types';
import type {DocFile, FullVersion} from './types';
import type {RuleSetUseItem} from 'webpack';
// 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: LoadedContent): SourceToPermalink {
const allDocs = content.loadedVersions.flatMap((v) => v.docs);
return new Map(allDocs.map(({source, permalink}) => [source, permalink]));
}
// Mutable map update :/
function update(content: LoadedContent): void {
sourceToPermalink.clear();
computeSourceToPermalink(content).forEach((value, key) => {
sourceToPermalink.set(key, value);
});
}
return {get: () => sourceToPermalink, update};
}
export default async function pluginContentDocs(
context: LoadContext,
options: PluginOptions,
@ -88,6 +112,8 @@ export default async function pluginContentDocs(
// TODO env should be injected into all plugins
const env = process.env.NODE_ENV as DocEnv;
const sourceToPermalinkHelper = createSourceToPermalinkHelper();
return {
name: 'docusaurus-plugin-content-docs',
@ -244,6 +270,8 @@ export default async function pluginContentDocs(
},
async contentLoaded({content, actions}) {
sourceToPermalinkHelper.update(content);
const versions: FullVersion[] = content.loadedVersions.map(toFullVersion);
await createAllRoutes({
@ -274,16 +302,6 @@ export default async function pluginContentDocs(
// Trailing slash is important, see https://github.com/facebook/docusaurus/pull/3970
.map(addTrailingPathSeparator);
// TODO this does not re-run when content gets updated in dev!
// it's probably better to restore a mutable cache in the plugin
function getSourceToPermalink(): SourceToPermalink {
const allDocs = content.loadedVersions.flatMap((v) => v.docs);
return Object.fromEntries(
allDocs.map(({source, permalink}) => [source, permalink]),
);
}
const sourceToPermalink = getSourceToPermalink();
function createMDXLoader(): RuleSetUseItem {
const loaderOptions: MDXLoaderOptions = {
admonitions: options.admonitions,
@ -318,7 +336,7 @@ export default async function pluginContentDocs(
);
const permalink = resolveMarkdownLinkPathname(linkPathname, {
sourceFilePath,
sourceToPermalink,
sourceToPermalink: sourceToPermalinkHelper.get(),
siteDir,
contentPaths: version,
});