Skip to content

Commit 7477270

Browse files
authored
Add VersionedDocsLink and use for evergreen links to "latest" docs page (#177)
This PR fixes #174 with a new `VersionedDocsLink` component that reads the docusaurus config to determine what the latest stable version is. It also adjusts the other dynamic link to the contribution guide to use this. This link is changed from the latest RC versions (currently 2.20) to the current `main` version (2.21), i.e. the absolute most up-to-date version of the contribution guide, which seems most relevant to contributors since they'll be starting in the `main` branch. This has a few issues with the component due to prettier/prettier#12209, which forces us into a very long line on the `_index.mdx` page. I'm unclear if a broken link will be detected or not.
1 parent 04263c1 commit 7477270

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/components/VersionedDocsLink.jsx

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Link from "@docusaurus/Link";
2+
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
3+
4+
const docsPluginPath = "./src/js/docsPluginWithTopLevel404.js";
5+
6+
// extract the plugin configuration from docusaurus and use this to deduce the "version" references
7+
// for the link component
8+
const useVersionConfigs = () => {
9+
const { siteConfig } = useDocusaurusContext();
10+
11+
const [_, docsPluginConfig] = siteConfig.plugins.find(
12+
(pair) => pair[0] == docsPluginPath
13+
);
14+
if (!docsPluginConfig) {
15+
throw new Error(
16+
`failed to find docs plugin at ${docsPluginPath} in docusaurus.config.js; has something been renamed?`
17+
);
18+
}
19+
20+
const current = docsPluginConfig.versions.current;
21+
return {
22+
"current-dev": current,
23+
// When running the dev server, we might be only showing the "current" docs (unless
24+
// PANTSBUILD_ORG_INCLUDE_VERSIONS is set), in which lastVersion may not be set, so we just
25+
// fallback to the current version to keep things working
26+
"last-stable": docsPluginConfig.lastVersion
27+
? docsPluginConfig.versions[docsPluginConfig.lastVersion]
28+
: current,
29+
};
30+
};
31+
32+
/**
33+
* Link to a particular path within auto-generated docs & reference, "live" for the appropriate version.
34+
*
35+
* For instance, to link to /2.19/docs/introduction/welcome-to-pants
36+
* for whatever the current stable version is, use:
37+
*
38+
* <VersionedDocsLink version="latest-stable" unversionedPath="docs/introduction/welcome-to-pants">Welcome!</VersionedDocsLink>
39+
*
40+
* @param unversionedPath - the URL path without the leading /2.x/ version bit
41+
* @param version - the description of the version to link to: `current-dev` (Pants' main branch), `last-stable` (most recent stable)
42+
* @param linkProps - any other parameters to pass to @docusaurus/Link (including `children`)
43+
*/
44+
export default function VersionedDocsLink({
45+
unversionedPath,
46+
version,
47+
...linkProps
48+
}) {
49+
const versionConfigs = useVersionConfigs();
50+
51+
const versionConfig = versionConfigs[version];
52+
if (!versionConfig) {
53+
const supported = Object.keys(versionConfigs).join(", ");
54+
throw new Error(
55+
`failed to find configuration for version="${version}" with unversionedPath="${unversionedPath}"; supported version values are: ${supported}`
56+
);
57+
}
58+
59+
const to = `/${versionConfig.path}/${unversionedPath}`;
60+
return <Link to={to} {...linkProps} />;
61+
}

src/pages/_index.mdx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import clsx from "clsx";
22
import styles from "./index.module.css";
3-
import Link from "@docusaurus/Link";
3+
import VersionedDocsLink from "@site/src/components/VersionedDocsLink";
44

55
export const Card = ({ children }) => {
66
return (
@@ -115,8 +115,7 @@ You'll find no subsets like Starlark here. Pants empowers you with full support
115115
## Pants is a multilingual multitool.
116116

117117
Pants supports Python, Docker, Go, Java, Kotlin, Pex, Protodoc, Scala, Shell, Thrift, Protobuf,
118-
Docker, Helm, many linting and formatting tools, packaging, coverage, and more.
119-
[Learn more.](/2.19/docs/introduction/welcome-to-pants)
118+
Docker, Helm, many linting and formatting tools, packaging, coverage, and more. <VersionedDocsLink version="last-stable" unversionedPath="docs/introduction/welcome-to-pants"> Learn more.</VersionedDocsLink>
120119

121120
</div>
122121
</div>

src/pages/community/members.mdx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { default as versions } from "@site/versions.json";
2-
import Link from "@docusaurus/Link";
1+
import VersionedDocsLink from "@site/src/components/VersionedDocsLink";
32

43
# The Pants Community
54

@@ -25,7 +24,7 @@ Contributions come in many forms, and we appreciate all of them! Examples includ
2524

2625
Whatever your area of expertise and your skill level, there may be valuable contributions you can make. Are you a graphic designer? A technical writer? Do you know how to make videos? There might be a cool contribution in your future.
2726

28-
We try and make contributions easy. For example, you can suggest documentation fixes by clicking on the Suggest Edits link on any page. And you can report bugs by opening a [GitHub issue](https://github.com/pantsbuild/pants/issues). If you want to hack on the Pants codebase itself there is a <Link to={`/${versions[0]}/docs/contributions`}>helpful guide</Link>.
27+
We try and make contributions easy. For example, you can suggest documentation fixes by clicking on the Suggest Edits link on any page. And you can report bugs by opening a [GitHub issue](https://github.com/pantsbuild/pants/issues). If you want to hack on the Pants codebase itself there is a <VersionedDocsLink version="current-dev" unversionedPath="docs/contributions">helpful guide</VersionedDocsLink>.
2928

3029
For some contributions, such as adding new features, the best place to get started is our [Slack workspace](/community/getting-help). You can make suggestions, solicit feedback and connect with like-minded contributors. That way we know who is working on what, and can help you avoid duplicating efforts or hitting known pitfalls.
3130

0 commit comments

Comments
 (0)