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

feat(v2): add a banner that links to latest version of documentation #2916

Merged
merged 21 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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 @@ -163,6 +163,7 @@ describe('simple website', () => {
source: path.join('@site', pluginPath, 'hello.md'),
title: 'Hello, World !',
description: 'Hi, Endilie here :)',
latestPermalink: undefined,
});

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

expect(docsSidebars).toMatchSnapshot();
Expand Down Expand Up @@ -335,6 +337,7 @@ describe('versioned website', () => {
title: 'bar',
permalink: '/docs/foo/bar',
},
latestPermalink: 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,13 +50,15 @@ describe('simple site', () => {
source: path.join('@site', routeBasePath, sourceA),
title: 'Bar',
description: 'This is custom description',
latestPermalink: undefined,
});
expect(dataB).toEqual({
id: 'hello',
permalink: '/docs/hello',
source: path.join('@site', routeBasePath, sourceB),
title: 'Hello, World !',
description: `Hi, Endilie here :)`,
latestPermalink: undefined,
});
});

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

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

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

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

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

function getHrefFromSideBar(sidebarItems: DocsSidebarItem[]): string | null {
for (let sidebarItem of sidebarItems) {
if (sidebarItem.type === 'category') {
const url = getHrefFromSideBar(sidebarItem.items);
if (url) return url;
} else {
return sidebarItem.href;
}
}
return null;
}

export default function pluginContentDocs(
context: LoadContext,
opts: Partial<PluginOptions>,
Expand Down Expand Up @@ -320,7 +332,6 @@ Available document ids=
},
{},
);

return {
docsMetadata,
docsDir,
Expand Down Expand Up @@ -368,7 +379,6 @@ Available document ids=
const isDocsHomePage =
metadataItem.id.replace(versionsRegex, '').replace(/^\//, '') ===
options.homePageId;

if (isDocsHomePage) {
const versionDocsPathPrefix =
(metadataItem?.version === versioning.latestVersion
Expand All @@ -384,6 +394,7 @@ Available document ids=
homePageDocsRoutePath,
versionDocsPathPrefix,
]);

const docsBaseMetadataPath = await createData(
`${docuHash(metadataItem.source)}-base.json`,
JSON.stringify(docsBaseMetadata, null, 2),
Expand Down Expand Up @@ -461,6 +472,16 @@ Available document ids=
Object.values(content.docsMetadata),
'version',
);
const rootUrl = getHrefFromSideBar(
content.docsSidebars[`version-${versioning.latestVersion}/docs`],
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the root url is not necessarily the first doc of the sidebar. If the user used a homeDocumentId, we must use it in priority, even if it's not the first doc in the sidebar (even if users usually use the first doc of the sidebar as home documentId).

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;
});
await Promise.all(
Object.keys(docsMetadataByVersion).map(async (version) => {
const routes: RouteConfig[] = await genRoutes(
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface MetadataRaw extends LastUpdateData {
sidebar_label?: string;
editUrl?: string;
version?: string;
latestPermalink?: string;
}

export interface Paginator {
Expand Down
26 changes: 26 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';
import DocPaginator from '@theme/DocPaginator';
import useTOCHighlight from '@theme/hooks/useTOCHighlight';
import Link from '@docusaurus/Link';

import clsx from 'clsx';
import styles from './styles.module.css';
Expand Down Expand Up @@ -68,6 +69,7 @@ function DocItem(props) {
lastUpdatedAt,
lastUpdatedBy,
version,
latestPermalink,
} = metadata;
const {
frontMatter: {
Expand Down Expand Up @@ -108,6 +110,30 @@ function DocItem(props) {
className={clsx('col', {
[styles.docItemCol]: !hideTableOfContents,
})}>
{latestPermalink && (
<div
className="alert alert--warning margin-bottom--md"
role="alert">
{version === 'next' ? (
<div>
This is unreleased documentation for {siteTitle}{' '}
<strong>{version}</strong> version.
</div>
) : (
<div>
This is archived documentation for {siteTitle}{' '}
<strong>v{version}</strong>, which is no longer actively
maintained.
</div>
)}
<div className="margin-top--md">
For up-to-date documentation, see the{' '}
<strong>
<Link to={latestPermalink}>latest version</Link>.
</strong>
</div>
</div>
)}
<div className={styles.docItemContainer}>
<article>
{version && (
Expand Down