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

@ -18,12 +18,14 @@ describe('resolveMarkdownLinkPathname', () => {
contentPath: 'docs',
contentPathLocalized: 'i18n/docs-localized',
},
sourceToPermalink: {
'@site/docs/intro.md': '/docs/intro',
'@site/docs/foo.md': '/doc/foo',
'@site/docs/bar/baz.md': '/doc/baz',
'@site/docs/http.foo.md': '/doc/http',
},
sourceToPermalink: new Map(
Object.entries({
'@site/docs/intro.md': '/docs/intro',
'@site/docs/foo.md': '/doc/foo',
'@site/docs/bar/baz.md': '/doc/baz',
'@site/docs/http.foo.md': '/doc/http',
}),
),
};
function test(linkPathname: string, expectedOutput: string) {
@ -50,11 +52,13 @@ describe('resolveMarkdownLinkPathname', () => {
contentPathLocalized: 'i18n/docs-localized',
},
sourceToPermalink: {
'@site/docs/intro/intro.md': '/docs/intro',
'@site/docs/intro/another.md': '/docs/another',
'@site/docs/api/classes/divine_uri.URI.md': '/docs/api/classes/uri',
},
sourceToPermalink: new Map(
Object.entries({
'@site/docs/intro/intro.md': '/docs/intro',
'@site/docs/intro/another.md': '/docs/another',
'@site/docs/api/classes/divine_uri.URI.md': '/docs/api/classes/uri',
}),
),
};
function test(linkPathname: string, expectedOutput: string) {

View file

@ -77,7 +77,11 @@ export {
writeMarkdownHeadingId,
type WriteHeadingIDOptions,
} from './markdownUtils';
export {type ContentPaths, resolveMarkdownLinkPathname} from './markdownLinks';
export {
type ContentPaths,
type SourceToPermalink,
resolveMarkdownLinkPathname,
} from './markdownLinks';
export {type SluggerOptions, type Slugger, createSlugger} from './slugger';
export {
isNameTooLong,

View file

@ -40,6 +40,11 @@ export type BrokenMarkdownLink<T extends ContentPaths> = {
link: string;
};
export type SourceToPermalink = Map<
string, // Aliased source path: "@site/docs/content.mdx"
string // Permalink: "/docs/content"
>;
// Note this is historical logic extracted during a 2024 refactor
// The algo has been kept exactly as before for retro compatibility
// See also https://github.com/facebook/docusaurus/pull/10168
@ -47,7 +52,7 @@ export function resolveMarkdownLinkPathname(
linkPathname: string,
context: {
sourceFilePath: string;
sourceToPermalink: {[aliasedFilePath: string]: string};
sourceToPermalink: SourceToPermalink;
contentPaths: ContentPaths;
siteDir: string;
},
@ -66,9 +71,9 @@ export function resolveMarkdownLinkPathname(
const aliasedSourceMatch = sourceDirsToTry
.map((sourceDir) => path.join(sourceDir, decodeURIComponent(linkPathname)))
.map((source) => aliasedSitePath(source, siteDir))
.find((source) => sourceToPermalink[source]);
.find((source) => sourceToPermalink.has(source));
return aliasedSourceMatch
? sourceToPermalink[aliasedSourceMatch] ?? null
? sourceToPermalink.get(aliasedSourceMatch) ?? null
: null;
}