-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add VersionedDocsLink and use for evergreen links to "latest" docs pa…
…ge (#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.
- Loading branch information
Showing
3 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Link from "@docusaurus/Link"; | ||
import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; | ||
|
||
const docsPluginPath = "./src/js/docsPluginWithTopLevel404.js"; | ||
|
||
// extract the plugin configuration from docusaurus and use this to deduce the "version" references | ||
// for the link component | ||
const useVersionConfigs = () => { | ||
const { siteConfig } = useDocusaurusContext(); | ||
|
||
const [_, docsPluginConfig] = siteConfig.plugins.find( | ||
(pair) => pair[0] == docsPluginPath | ||
); | ||
if (!docsPluginConfig) { | ||
throw new Error( | ||
`failed to find docs plugin at ${docsPluginPath} in docusaurus.config.js; has something been renamed?` | ||
); | ||
} | ||
|
||
const current = docsPluginConfig.versions.current; | ||
return { | ||
"current-dev": current, | ||
// When running the dev server, we might be only showing the "current" docs (unless | ||
// PANTSBUILD_ORG_INCLUDE_VERSIONS is set), in which lastVersion may not be set, so we just | ||
// fallback to the current version to keep things working | ||
"last-stable": docsPluginConfig.lastVersion | ||
? docsPluginConfig.versions[docsPluginConfig.lastVersion] | ||
: current, | ||
}; | ||
}; | ||
|
||
/** | ||
* Link to a particular path within auto-generated docs & reference, "live" for the appropriate version. | ||
* | ||
* For instance, to link to /2.19/docs/introduction/welcome-to-pants | ||
* for whatever the current stable version is, use: | ||
* | ||
* <VersionedDocsLink version="latest-stable" unversionedPath="docs/introduction/welcome-to-pants">Welcome!</VersionedDocsLink> | ||
* | ||
* @param unversionedPath - the URL path without the leading /2.x/ version bit | ||
* @param version - the description of the version to link to: `current-dev` (Pants' main branch), `last-stable` (most recent stable) | ||
* @param linkProps - any other parameters to pass to @docusaurus/Link (including `children`) | ||
*/ | ||
export default function VersionedDocsLink({ | ||
unversionedPath, | ||
version, | ||
...linkProps | ||
}) { | ||
const versionConfigs = useVersionConfigs(); | ||
|
||
const versionConfig = versionConfigs[version]; | ||
if (!versionConfig) { | ||
const supported = Object.keys(versionConfigs).join(", "); | ||
throw new Error( | ||
`failed to find configuration for version="${version}" with unversionedPath="${unversionedPath}"; supported version values are: ${supported}` | ||
); | ||
} | ||
|
||
const to = `/${versionConfig.path}/${unversionedPath}`; | ||
return <Link to={to} {...linkProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters