@@ -187,11 +187,19 @@ export default function docusaurusPluginLLMs(
187187
188188 // Write MD files
189189 await Promise . all (
190- [ ...dataRoutes . values ( ) ] . map ( ( { markdown : { markdown, outputPath } } ) =>
191- writeFile ( outputPath , markdown , "utf-8" )
190+ [ ...dataRoutes . values ( ) ] . map (
191+ ( {
192+ markdown : {
193+ markdown,
194+ output : { absolutePath : outputPath }
195+ }
196+ } ) => writeFile ( outputPath , markdown , "utf-8" )
192197 )
193198 ) ;
194199
200+ // Add alternate reference to HTML header
201+ await addAlternateReferences ( { dataRoutes, siteConfig } ) ;
202+
195203 // Create /llms.txt
196204 await generateLlmsTxt ( {
197205 groupedRoutes : sortedGroupedRoutes ,
@@ -228,10 +236,16 @@ type GroupedRoutes = Record<string, GroupedRoute>;
228236
229237type SortedGroupedRoutes = [ string , GroupedRoute ] [ ] ;
230238
231- interface RouteMarkdownData {
239+ interface RouteMarkdownPath {
232240 relativePath : string ;
241+ absolutePath : string ;
242+ }
243+
244+ interface RouteMarkdownData {
245+ source : RouteMarkdownPath ;
246+ output : RouteMarkdownPath ;
247+ html : string ;
233248 markdown : string ;
234- outputPath : string ;
235249}
236250
237251interface RouteMetadata {
@@ -371,9 +385,22 @@ const prepareMarkdown = async ({
371385
372386 const outputPath = join ( outDir , relativePath ) ;
373387
388+ const sourceRelativePath = route . endsWith ( "/" )
389+ ? `${ route } index.html`
390+ : `${ route } /index.html` ;
391+
392+ const sourcePath = join ( outDir , sourceRelativePath ) ;
393+
374394 return {
375- relativePath,
376- outputPath,
395+ source : {
396+ relativePath : sourceRelativePath ,
397+ absolutePath : sourcePath
398+ } ,
399+ output : {
400+ relativePath,
401+ absolutePath : outputPath
402+ } ,
403+ html,
377404 markdown : cleanMd
378405 } ;
379406 } ;
@@ -450,7 +477,9 @@ const generateLlmsTxt = async ({
450477 }
451478
452479 const {
453- markdown : { relativePath } ,
480+ markdown : {
481+ output : { relativePath }
482+ } ,
454483 metadata : { title, description }
455484 } = data ;
456485
@@ -559,3 +588,35 @@ ${content}`;
559588
560589const capitalize = ( text : string ) : string =>
561590 text . replace ( / ./ , ( c ) => c . toUpperCase ( ) ) ;
591+
592+ const addAlternateReferences = async ( {
593+ dataRoutes,
594+ siteConfig : { url }
595+ } : {
596+ dataRoutes : RoutesData ;
597+ } & Pick < LoadContext , "siteConfig" > ) => {
598+ const updateHeadWithAlternateRef = async ( {
599+ html,
600+ source : { absolutePath } ,
601+ output : { relativePath }
602+ } : RouteMarkdownData ) => {
603+ const mdAbsolutePath = "{{MD_ABSOLUTE_PATH}}" ;
604+ const template = `<link href="${ mdAbsolutePath } " rel="alternate" type="text/markdown">` ;
605+
606+ // Maybe there is a better target but, feels like the easier and safer
607+ const headEndTag = "</head>" ;
608+
609+ const updatedHtml = html . replace (
610+ headEndTag ,
611+ `${ template . replace ( mdAbsolutePath , `${ url } ${ relativePath } ` ) } ${ headEndTag } `
612+ ) ;
613+
614+ await writeFile ( absolutePath , updatedHtml , "utf-8" ) ;
615+ } ;
616+
617+ await Promise . all (
618+ [ ...dataRoutes . values ( ) ] . map ( ( { markdown } ) =>
619+ updateHeadWithAlternateRef ( markdown )
620+ )
621+ ) ;
622+ } ;
0 commit comments