docs/lib/path-utils.js

100 строки
3.0 KiB
JavaScript
Исходник Обычный вид История

const slash = require('slash')
const path = require('path')
const patterns = require('./patterns')
const { latest } = require('./enterprise-server-releases')
const allProducts = require('./all-products')
const allVersions = require('./all-versions')
const supportedVersions = new Set(Object.keys(allVersions))
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
2020-12-17 18:50:52 +03:00
// Add the language to the given HREF
// /articles/foo -> /en/articles/foo
function getPathWithLanguage (href, languageCode) {
return slash(path.posix.join('/', languageCode, getPathWithoutLanguage(href)))
.replace(patterns.trailingSlash, '$1')
}
2020-12-17 18:50:52 +03:00
// Remove the language from the given HREF
// /en/articles/foo -> /articles/foo
function getPathWithoutLanguage (href) {
return slash(href.replace(patterns.hasLanguageCode, '/'))
}
2020-12-17 05:24:50 +03:00
// Remove the version segment from the path
function getPathWithoutVersion (href) {
return href.replace(`/${getVersionStringFromPath(href)}`, '')
}
2020-12-17 05:24:50 +03:00
// Return the version segment in a path
function getVersionStringFromPath (href) {
href = getPathWithoutLanguage(href)
2020-12-17 18:50:52 +03:00
// Return immediately if this is a link to the homepage
2020-12-17 18:19:44 +03:00
if (href === '/') {
return 'homepage'
}
// Get the first segment
2020-12-17 05:24:50 +03:00
const versionFromPath = href.split('/')[1]
// If the first segment is a supported product, assume this is FPT
if (allProducts[versionFromPath]) {
return nonEnterpriseDefaultVersion
}
// Otherwise, check if it's a supported version
2020-12-17 18:19:44 +03:00
if (supportedVersions.has(versionFromPath)) {
2020-12-17 05:24:50 +03:00
return versionFromPath
}
2020-12-17 18:50:52 +03:00
// If the version segment is the latest enterprise-server release, return the latest release
2020-12-17 05:24:50 +03:00
if (versionFromPath === 'enterprise-server@latest') {
2020-12-17 18:19:44 +03:00
return `enterprise-server@${latest}`
2020-12-17 05:24:50 +03:00
}
2020-12-17 18:50:52 +03:00
// If it's just a plan with no @release (e.g., `enterprise-server`), return the latest release
2020-12-17 05:24:50 +03:00
const planObject = Object.values(allVersions).find(v => v.plan === versionFromPath)
if (planObject) {
return allVersions[planObject.latestVersion].version
}
2020-12-17 18:50:52 +03:00
// Otherwise, return the first segment as-is, which may not be a real supported version,
2020-12-17 18:19:44 +03:00
// but additional checks are done on this segment in getVersionedPathWithoutLanguage
return versionFromPath
}
2020-12-17 05:24:50 +03:00
// Return the corresponding object for the version segment in a path
function getVersionObjectFromPath (href) {
2020-12-17 05:24:50 +03:00
const versionFromPath = getVersionStringFromPath(href)
2020-12-17 05:24:50 +03:00
return allVersions[versionFromPath]
}
2020-12-17 05:24:50 +03:00
// Return the product segment from the path
function getProductStringFromPath (href) {
href = getPathWithoutLanguage(href)
if (href === '/') return 'homepage'
return allProducts[href.split('/')[2]]
? href.split('/')[2]
: href.split('/')[1]
}
2020-12-17 05:24:50 +03:00
// Return the corresponding object for the product segment in a path
function getProductObjectFromPath (href) {
2020-12-17 05:24:50 +03:00
const productFromPath = getProductStringFromPath(href)
return allProducts[productFromPath]
}
module.exports = {
getPathWithLanguage,
getPathWithoutLanguage,
getPathWithoutVersion,
getVersionStringFromPath,
getVersionObjectFromPath,
getProductStringFromPath,
getProductObjectFromPath
}