Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): do not create route for document that serve as docs home page #2861

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Object {
"type": "link",
},
Object {
"href": "/docs/hello",
"href": "/docs",
"label": "Hello, World !",
"type": "link",
},
Expand All @@ -40,7 +40,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/hello",
"href": "/docs",
"label": "Hello, World !",
"type": "link",
},
Expand Down Expand Up @@ -87,14 +87,6 @@ Array [
},
"path": "/docs/foo/baz",
},
Object {
"component": "@theme/DocItem",
"exact": true,
"modules": Object {
"content": "@site/docs/hello.md",
},
"path": "/docs/hello",
},
Object {
"component": "@theme/DocItem",
"exact": true,
Expand Down Expand Up @@ -169,14 +161,6 @@ Array [
},
"path": "/docs/1.0.0/foo/baz",
},
Object {
"component": "@theme/DocItem",
"exact": true,
"modules": Object {
"content": "@site/versioned_docs/version-1.0.0/hello.md",
},
"path": "/docs/1.0.0/hello",
},
],
},
Object {
Expand All @@ -195,14 +179,6 @@ Array [
},
"path": "/docs/next/foo/bar",
},
Object {
"component": "@theme/DocItem",
"exact": true,
"modules": Object {
"content": "@site/docs/hello.md",
},
"path": "/docs/next/hello",
},
],
},
Object {
Expand All @@ -221,14 +197,6 @@ Array [
},
"path": "/docs/foo/bar",
},
Object {
"component": "@theme/DocItem",
"exact": true,
"modules": Object {
"content": "@site/versioned_docs/version-1.0.1/hello.md",
},
"path": "/docs/hello",
},
],
},
]
Expand All @@ -253,7 +221,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/next/hello",
"href": "/docs/next",
"label": "hello",
"type": "link",
},
Expand Down Expand Up @@ -284,7 +252,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/1.0.0/hello",
"href": "/docs/1.0.0",
"label": "hello",
"type": "link",
},
Expand All @@ -310,7 +278,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/hello",
"href": "/docs",
"label": "hello",
"type": "link",
},
Expand Down Expand Up @@ -347,7 +315,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/1.0.0/hello",
"href": "/docs/1.0.0",
"label": "hello",
"type": "link",
},
Expand Down Expand Up @@ -386,7 +354,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/hello",
"href": "/docs",
"label": "hello",
"type": "link",
},
Expand Down Expand Up @@ -424,7 +392,7 @@ Object {
"collapsed": true,
"items": Array [
Object {
"href": "/docs/next/hello",
"href": "/docs/next",
"label": "hello",
"type": "link",
},
Expand Down
38 changes: 27 additions & 11 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export default function pluginContentDocs(
opts: Partial<PluginOptions>,
): Plugin<LoadedContent | null> {
const options = {...DEFAULT_OPTIONS, ...opts};
const homePageDocsRoutePath =
options.routeBasePath === '' ? '/' : options.routeBasePath;

if (options.admonitions) {
options.remarkPlugins = options.remarkPlugins.concat([
Expand All @@ -98,6 +96,24 @@ export default function pluginContentDocs(
} = versioning;
const versionsNames = versions.map((version) => `version-${version}`);

// Docs home page.
const homePageDocsRoutePath =
options.routeBasePath === '' ? '/' : options.routeBasePath;
const isDocsHomePagePath = (permalink: string) => {
const documentIdMatch = new RegExp(
`^\/(?:${homePageDocsRoutePath}\/)?(?:(?:${versions.join(
'|',
)}|next)\/)?(.*)`,
'i',
).exec(permalink);

if (documentIdMatch) {
return documentIdMatch[1] === options.homePageId;
}

return false;
};

return {
name: 'docusaurus-plugin-content-docs',

Expand Down Expand Up @@ -268,10 +284,14 @@ export default function pluginContentDocs(
);
}

const {title, permalink, sidebar_label} = linkMetadata;

return {
type: 'link',
label: linkMetadata.sidebar_label || linkMetadata.title,
href: linkMetadata.permalink,
label: sidebar_label || title,
href: isDocsHomePagePath(permalink)
? permalink.replace(`/${options.homePageId}`, '')
: permalink,
};
};

Expand Down Expand Up @@ -361,7 +381,6 @@ export default function pluginContentDocs(
baseUrl,
homePageDocsRoutePath,
versionDocsPathPrefix,
options.homePageId,
]);
const docsBaseMetadataPath = await createData(
`${docuHash(metadataItem.source)}-base.json`,
Expand Down Expand Up @@ -404,12 +423,9 @@ export default function pluginContentDocs(

return (
routes
// Do not create a route for a page created specifically for docs home page.
.filter(
({path}) =>
path.substr(path.lastIndexOf('/') + 1) !==
REVERSED_DOCS_HOME_PAGE_ID,
)
// Do not create a route for a document serve as docs home page.
// TODO: need way to do this filtering when generating routes for better perf.
.filter(({path}) => !isDocsHomePagePath(path))
.sort((a, b) => (a.path > b.path ? 1 : b.path > a.path ? -1 : 0))
);
};
Expand Down
2 changes: 1 addition & 1 deletion website/docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
};
```

Given the example above, now when you navigate to the path `/docs` you will see that the document content with id is `getting-started`. This functionality also works for docs with versioning enabled.
Given the example above, now when you navigate to the path `/docs` you will see that the document content with id is `getting-started`. This functionality also works for docs with versioning enabled. Importantly, with document serves as home docs page, it will not be available at its URL. Following the example above, this means that the `docs/getting-started` URL will be lead to a 404 error.

:::important

Expand Down