mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-23 00:11:41 -04:00
feat(website): add version badge to external plugin docs pages (#11451)
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
} from '@hashicorp/react-docs-page/server'
|
||||
import renderPageMdx from '@hashicorp/react-docs-page/render-page-mdx'
|
||||
import resolveNavData from './utils/resolve-nav-data'
|
||||
import fetchLatestReleaseTag from './utils/fetch-latest-release-tag'
|
||||
|
||||
async function generateStaticPaths({
|
||||
navDataFile,
|
||||
@@ -49,6 +50,14 @@ async function generateStaticProps({
|
||||
const githubFileUrl = remoteFile
|
||||
? remoteFile.sourceUrl
|
||||
: `https://github.com/hashicorp/${product.slug}/blob/${mainBranch}/website/${filePath}`
|
||||
// If this is a plugin, and if
|
||||
// the version has been specified as "latest",
|
||||
// determine the tag this corresponds to, so that
|
||||
// we can show this explicit version number in docs
|
||||
const latestReleaseTag =
|
||||
pluginData?.version === 'latest'
|
||||
? await fetchLatestReleaseTag(pluginData.repo)
|
||||
: pluginData?.version
|
||||
// For plugin pages, prefix the MDX content with a
|
||||
// label that reflects the plugin tier
|
||||
// (current options are "Official" or "Community")
|
||||
@@ -64,6 +73,10 @@ async function generateStaticProps({
|
||||
if (pluginData?.isHcpPackerReady) {
|
||||
badgesMdx.push(`<PluginBadge type="hcp_packer_ready" />`)
|
||||
}
|
||||
// Add badge showing the latest release version number
|
||||
if (latestReleaseTag) {
|
||||
badgesMdx.push(`<Badge label="${latestReleaseTag}" theme="light-gray"/>`)
|
||||
}
|
||||
// If we have badges to add, inject them into the MDX
|
||||
if (badgesMdx.length > 0) {
|
||||
const badgeChildrenMdx = badgesMdx.join('')
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
async function fetchLatestReleaseTag(repo) {
|
||||
const latestReleaseUrl = `https://github.com/${repo}/releases/latest`
|
||||
const redirectedUrl = await getRedirectedUrl(latestReleaseUrl)
|
||||
if (!redirectedUrl) return false
|
||||
const latestTag = redirectedUrl.match(/tag\/(.*)/)[1]
|
||||
return latestTag
|
||||
}
|
||||
|
||||
async function getRedirectedUrl(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const https = require('https')
|
||||
const req = https.request(url, (res) => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400) {
|
||||
resolve(res.headers.location)
|
||||
} else {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
req.on('error', reject)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = fetchLatestReleaseTag
|
||||
@@ -184,8 +184,10 @@ async function resolvePluginEntryDocs(pluginConfigEntry, currentPath) {
|
||||
path: urlPath,
|
||||
remoteFile: { filePath, fileString, sourceUrl },
|
||||
pluginData: {
|
||||
repo,
|
||||
tier: parsedPluginTier,
|
||||
isHcpPackerReady,
|
||||
version,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user