handle missing version and 'current' as version

This commit is contained in:
Jakob Schrettenbrunner 2020-04-13 19:23:14 +02:00
parent ee515d0129
commit 61f16c12a7

41
.vuepress/enhanceApp.js Normal file
View File

@ -0,0 +1,41 @@
export default ({
Vue, // the version of Vue being used in the VuePress app
options, // the options for the root Vue instance
router, // the router instance for the app
siteData, // site metadata
isServer // is this enhancement applied in server-rendering or client
}) => {
findVersionedPaths(siteData.themeConfig.sidebar).forEach(vp => {
router.addRoutes(
router.options.routes.map(route => {
if (route.path.startsWith(vp.path + vp.currentVersion)) {
return [
{
path: route.path.replace(vp.currentVersion, "current"),
redirect: route.path
}, {
path: route.path.replace(vp.currentVersion + "/", ""),
redirect: route.path
}
]
}
return undefined
}).filter(x => x).flat()
)
})
}
function findVersionedPaths(paths) {
return Object.entries(paths).map(([path, children]) => {
return children
.filter(child => Array.isArray(child.versions))
.map(child => ({ ...child, path: pathJoin(path, child.path) }))
}).flat()
}
// https://stackoverflow.com/a/29855282/4430124
function pathJoin(...parts) {
var separator = '/';
var replace = new RegExp(separator + '{1,}', 'g');
return parts.join(separator).replace(replace, separator);
}