Skip to content

Commit 73e0382

Browse files
feat(pages): add support for Markdown file path links (#11666)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent 3f07791 commit 73e0382

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import type {LoadedContent, Metadata} from '@docusaurus/plugin-content-pages';
9+
10+
function indexPagesBySource(content: LoadedContent): Map<string, Metadata> {
11+
return new Map(content.map((page) => [page.source, page]));
12+
}
13+
14+
// TODO this is bad, we should have a better way to do this (new lifecycle?)
15+
// The source to page/permalink is a mutable map passed to the mdx loader
16+
// See https://github.com/facebook/docusaurus/pull/10457
17+
// See https://github.com/facebook/docusaurus/pull/10185
18+
export function createContentHelpers() {
19+
const sourceToPage = new Map<string, Metadata>();
20+
const sourceToPermalink = new Map<string, string>();
21+
22+
// Mutable map update :/
23+
function updateContent(content: LoadedContent): void {
24+
sourceToPage.clear();
25+
sourceToPermalink.clear();
26+
indexPagesBySource(content).forEach((value, key) => {
27+
sourceToPage.set(key, value);
28+
sourceToPermalink.set(key, value.permalink);
29+
});
30+
}
31+
32+
return {updateContent, sourceToPage, sourceToPermalink};
33+
}

packages/docusaurus-plugin-content-pages/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import {
1313
addTrailingPathSeparator,
1414
createAbsoluteFilePathMatcher,
1515
getContentPathList,
16+
resolveMarkdownLinkPathname,
1617
} from '@docusaurus/utils';
1718
import {createMDXLoaderRule} from '@docusaurus/mdx-loader';
1819
import {createAllRoutes} from './routes';
1920
import {createPagesContentPaths, loadPagesContent} from './content';
21+
import {createContentHelpers} from './contentHelpers';
2022
import type {LoadContext, Plugin} from '@docusaurus/types';
2123
import type {
2224
PluginOptions,
@@ -32,6 +34,7 @@ export default async function pluginContentPages(
3234
const {siteConfig, siteDir, generatedFilesDir} = context;
3335

3436
const contentPaths = createPagesContentPaths({context, options});
37+
const contentHelpers = createContentHelpers();
3538

3639
const pluginDataDirRoot = path.join(
3740
generatedFilesDir,
@@ -82,6 +85,14 @@ export default async function pluginContentPages(
8285
image: frontMatter.image,
8386
}),
8487
markdownConfig: siteConfig.markdown,
88+
resolveMarkdownLink: ({linkPathname, sourceFilePath}) => {
89+
return resolveMarkdownLinkPathname(linkPathname, {
90+
sourceFilePath,
91+
sourceToPermalink: contentHelpers.sourceToPermalink,
92+
siteDir,
93+
contentPaths,
94+
});
95+
},
8596
},
8697
});
8798
}
@@ -109,6 +120,7 @@ export default async function pluginContentPages(
109120
if (!content) {
110121
return;
111122
}
123+
contentHelpers.updateContent(content);
112124
await createAllRoutes({content, options, actions});
113125
},
114126

website/_dogfooding/_pages tests/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ import Readme from "../README.mdx"
4343
- [Embeds](/tests/pages/embeds)
4444
- [Style Isolation tests](/tests/pages/style-isolation)
4545
- [IdealImage tests](/tests/pages/ideal-image)
46+
- [Linking tests](./linking/index.md)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Markdown Linking Test
3+
description: Test pages to verify markdown file path links work in the pages plugin
4+
---
5+
6+
# Linking Test
7+
8+
This folder contains test pages to verify markdown file path links work correctly in the `@docusaurus/plugin-content-pages`.
9+
10+
## Relative file path links
11+
12+
- [`./index.md`](./index.md)
13+
- [`./page-a.md`](./page-a.md)
14+
- [`./nested/page-b.md`](./nested/page-b.md)
15+
16+
## Absolute file path links
17+
18+
- [`/index.md`](/linking/index.md)
19+
- [`/page-a.md`](/linking/page-a.md)
20+
- [`/nested/page-b.md`](/linking/nested/page-b.md)
21+
22+
## Site alias file path links
23+
24+
- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
25+
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
26+
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Linking Test - Page B
2+
3+
## Relative file path links
4+
5+
- [`../index.md`](../index.md)
6+
- [`../page-a.md`](../page-a.md)
7+
- [`./page-b.md`](./page-b.md)
8+
- [`../nested/page-b.md`](../nested/page-b.md)
9+
10+
## Absolute file path links
11+
12+
- [`/index.md`](/linking/index.md)
13+
- [`/page-a.md`](/linking/page-a.md)
14+
- [`/nested/page-b.md`](/linking/nested/page-b.md)
15+
16+
## Site alias file path links
17+
18+
- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
19+
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
20+
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Linking Test - Page A
2+
3+
## Relative file path links
4+
5+
- [`./index.md`](./index.md)
6+
- [`./page-a.md`](./page-a.md)
7+
- [`./nested/page-b.md`](./nested/page-b.md)
8+
9+
## Absolute file path links
10+
11+
- [`/index.md`](/linking/index.md)
12+
- [`/page-a.md`](/linking/page-a.md)
13+
- [`/nested/page-b.md`](/linking/nested/page-b.md)
14+
15+
## Site alias file path links
16+
17+
- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
18+
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
19+
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)

0 commit comments

Comments
 (0)