Skip to content

Commit

Permalink
feat(v2): rename functions and fix a corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
anshul committed Jun 12, 2020
1 parent d0f1ff3 commit 2fe191b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('simple website', () => {
source: path.join('@site', pluginPath, 'hello.md'),
title: 'Hello, World !',
description: 'Hi, Endilie here :)',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});

expect(docsMetadata['foo/bar']).toEqual({
Expand All @@ -177,7 +177,7 @@ describe('simple website', () => {
source: path.join('@site', pluginPath, 'foo', 'bar.md'),
title: 'Bar',
description: 'This is custom description',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});

expect(docsSidebars).toMatchSnapshot();
Expand Down Expand Up @@ -337,7 +337,7 @@ describe('versioned website', () => {
title: 'bar',
permalink: '/docs/foo/bar',
},
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
expect(docsMetadata['version-1.0.0/foo/baz']).toEqual({
id: 'version-1.0.0/foo/baz',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ describe('simple site', () => {
source: path.join('@site', routeBasePath, sourceA),
title: 'Bar',
description: 'This is custom description',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
expect(dataB).toEqual({
id: 'hello',
permalink: '/docs/hello',
source: path.join('@site', routeBasePath, sourceB),
title: 'Hello, World !',
description: `Hi, Endilie here :)`,
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
});

Expand Down Expand Up @@ -87,7 +87,7 @@ describe('simple site', () => {
editUrl:
'https://github.com/facebook/docusaurus/edit/master/website/docs/foo/baz.md',
description: 'Images',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
});

Expand All @@ -112,7 +112,7 @@ describe('simple site', () => {
title: 'lorem',
editUrl: 'https://github.com/customUrl/docs/lorem.md',
description: 'Lorem ipsum.',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});

// unrelated frontmatter is not part of metadata
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('simple site', () => {
description: 'Lorem ipsum.',
lastUpdatedAt: 1539502055,
lastUpdatedBy: 'Author',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
});

Expand Down Expand Up @@ -173,7 +173,7 @@ describe('simple site', () => {
description: 'Lorem ipsum.',
lastUpdatedAt: 1539502055,
lastUpdatedBy: 'Author',
latestPermalink: undefined,
latestVersionMainDocPermalink: undefined,
});
});

Expand Down
19 changes: 11 additions & 8 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ const DEFAULT_OPTIONS: PluginOptions = {
admonitions: {},
};

function getHrefFromSideBar(sidebarItems: DocsSidebarItem[]): string | null {
function getFirstDocLinkOfSidebar(sidebarItems: DocsSidebarItem[]): string | null {
for (let sidebarItem of sidebarItems) {
if (sidebarItem.type === 'category') {
const url = getHrefFromSideBar(sidebarItem.items);
const url = getFirstDocLinkOfSidebar(sidebarItem.items);
if (url) return url;
} else {
return sidebarItem.href;
Expand Down Expand Up @@ -472,17 +472,20 @@ Available document ids=
Object.values(content.docsMetadata),
'version',
);
const rootUrl = options.homePageId
? normalizeUrl([baseUrl, homePageDocsRoutePath])
: getHrefFromSideBar(
content.docsSidebars[`version-${versioning.latestVersion}/docs`],
);
const rootUrl =
options.homePageId && content.docsMetadata[options.homePageId]
? content.docsMetadata[options.homePageId].permalink
: getFirstDocLinkOfSidebar(
content.docsSidebars[
`version-${versioning.latestVersion}/docs`
],
);
if (!rootUrl) {
throw new Error('Bad sidebars file. No document linked');
}
Object.values(content.docsMetadata).forEach((docMetadata) => {
if (docMetadata.version !== versioning.latestVersion)
docMetadata.latestPermalink = rootUrl;
docMetadata.latestVersionMainDocPermalink = rootUrl;
});
await Promise.all(
Object.keys(docsMetadataByVersion).map(async (version) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export interface MetadataRaw extends LastUpdateData {
sidebar_label?: string;
editUrl?: string;
version?: string;
latestPermalink?: string;
latestVersionMainDocPermalink?: string;
}

export interface Paginator {
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function DocItem(props) {
lastUpdatedAt,
lastUpdatedBy,
version,
latestPermalink,
latestVersionMainDocPermalink,
} = metadata;
const {
frontMatter: {
Expand Down Expand Up @@ -110,7 +110,7 @@ function DocItem(props) {
className={clsx('col', {
[styles.docItemCol]: !hideTableOfContents,
})}>
{latestPermalink && (
{latestVersionMainDocPermalink && (
<div
className="alert alert--warning margin-bottom--md"
role="alert">
Expand All @@ -129,7 +129,7 @@ function DocItem(props) {
<div className="margin-top--md">
For up-to-date documentation, see the{' '}
<strong>
<Link to={latestPermalink}>latest version</Link>.
<Link to={latestVersionMainDocPermalink}>latest version</Link>.
</strong>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
'@docusaurus/preset-classic',
{
docs: {
homePageId: 'introduction',
homePageId: 'ding',
path: 'docs',
sidebarPath: require.resolve('./sidebars.js'),
editUrl:
Expand Down

0 comments on commit 2fe191b

Please sign in to comment.