@simonbrunel/vuepress-plugin-versions
VuePress plugin that allows users to switch between different versions of your documentation,
based on the published versions of your project on npm. Available releases are automatically
retrieved on the client side, so previous versions of your documentation can provide a link
to the newer versions.
Important: this plugin does not help to create and organize different versions of
your documentation but assumes that it's already properly versioned under different URLs.
If it's not the case, you may want to try the vuepress-plugin-versioning
plugin.
Requirements
Please read the following requirements before using this plugin:
-
Your project must be publicly available on npm. Currently, this plugin only allows to
fetch published versions from npm. If you want to see more use cases implemented for projects
not available on npm, please open an issue.
-
The versioned URL format of your documentation should be stable in order to support old docs
linking to newer ones. Rules to convert versions to URLs are part of the build, thus can't be
changed once published. If you deploy newer docs under a different URL format, old docs will
still redirect to the previous format.
-
Your VuePress theme, if not the default one, must provide a dropdown component with a similar
API as the one built-in the default VuePress theme. This component must be accessible via the
@theme/components/DropdownLink.vue
alias.
Installation
npm install -D @simonbrunel/vuepress-plugin-versions
Configuration
Minimal config in .vuepress/config.js
to enable this plugin:
module.exports = {
plugins: [
['@simonbrunel/vuepress-plugin-versions'],
],
};
The following snippet describes available options with their default values:
module.exports = {
plugins: [
['@simonbrunel/vuepress-plugin-versions', {
package: '../../package.json',
filters: undefined,
menu: {
locations: [
'.navbar > .home-link::after',
'.sidebar > .nav-links > :first-child::before',
],
text: '{{version}}',
items: [
{
type: 'versions',
target: '_self',
link: '/{{version}}/',
text: '{{version}}',
exclude: undefined,
limit: undefined,
group: 'minor',
},
],
},
}],
],
};
This plugin supports the same options as the default theme navbar links, with the difference
that the text
and link
properties support template with variables for
the current version. Additionally, a special versions
item can be used to
generate a list of items for each available version.
module.exports = {
plugins: [
['@simonbrunel/vuepress-plugin-versions', {
menu: {
items: [
{ },
{ },
{ },
],
},
}],
],
};
Link Item
Same as the Navbar Links
for the default theme.
{
link: '<required>',
text: '<required>',
target: undefined,
}
Group Item
Sub groups inside the menu are also supported using nested items and follow the same format
as the Navbar Links
for the default theme.
{
text: '',
items: [
{ },
{ },
],
}
Versions Item
A 'versions'
item can be used to generate multiple items, one for each available version.
It's possible to control which versions are displayed by changing the group
, exclude
and limit
options.
{
type: 'versions',
exclude: undefined,
group: 'minor',
limit: undefined,
target: undefined,
link: '/{{version}}/',
text: '{{version}}',
}
Template Syntax
Texts and links in the plugin configuration support a (very) basic template syntax. A template is a
string that contains any number of variables. Variables are indicated by the double curly brackets
that surround them and will be replaced by values derived from version numbers.
Variables
Currently, the following variables are supported for each version:
Variables | Example |
---|
{{core}} | 1.4.2 |
{{major}} | 1 |
{{minor}} | 4 |
{{patch}} | 2 |
{{prerelease}} | beta.2 |
{{tag}} | next |
{{version}} | 1.4.2-beta.2 |
Additionally, it's possible to define your own filters that you can use to transform the value of
these variables (e.g. {{version|slug}}
, where slug
is a function that you provide and taking
the value of version
as first argument).
Filters
Filters allow to modify the value of a template variable. There is currently no built-in filters
but you can provide your own using the filters
plugin option. It must be an object where the key
is the filter name and the value, a function that takes two arguments: the variable value to
transform and an object containing all available variables.
Important: Since filters are serialized in order to be used on the client side, they must
be arrow function expressions
and can not use global variables or functions.
For example:
module.exports = {
plugins: [
['@simonbrunel/vuepress-plugin-versions', {
filters: {
slug: (v) => v.replace(/\./g, '_'),
suffix: (v) => v ? ` (${v})` : '',
}
}]
]
}
With the previous configuration, the following templates are resolved as:
Template | Example 1 | Example 2 |
---|
'{{version}}' | '1.2.3' | '1.2.3-beta.1' |
'{{version|slug}}' | '1_2_3' | '1_2_3-beta_1' |
'{{core}}{{prerelease|suffix}}' | '1.2.3' | '1.2.3 (beta.1)' |
License
@simonbrunel/vuepress-plugin-versions
is available under the MIT license.