@@ -58,6 +58,28 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
5858import type { DocFile , FullVersion } from './types' ;
5959import 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+
6183export 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