Skip to content

Commit c68c07f

Browse files
authored
docs: version selectors synchronization (#18265)
1 parent abea3b6 commit c68c07f

8 files changed

Lines changed: 78 additions & 96 deletions

File tree

docs/.eleventy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module.exports = function(eleventyConfig) {
4343
pathPrefix = "/docs/latest/";
4444
} else if (process.env.BRANCH === "next") {
4545
pathPrefix = "/docs/next/";
46+
} else if (process.env.BRANCH && /^v\d+\.x$/u.test(process.env.BRANCH)) {
47+
pathPrefix = `/docs/${process.env.BRANCH}/`; // `/docs/v8.x/`, `/docs/v9.x/`, `/docs/v10.x/` ...
4648
}
4749

4850
//------------------------------------------------------------------------------

docs/src/_data/config.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"lang": "en",
3-
"version": "7.26.0",
4-
"showNextVersion": true
2+
"lang": "en"
53
}

docs/src/_data/eslintNextVersion.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

docs/src/_data/eslintVersion.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

docs/src/_data/eslintVersions.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @fileoverview Data for version selectors
3+
* @author Milos Djermanovic
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Requirements
8+
//-----------------------------------------------------------------------------
9+
10+
const eleventyFetch = require("@11ty/eleventy-fetch");
11+
12+
//-----------------------------------------------------------------------------
13+
// Exports
14+
//-----------------------------------------------------------------------------
15+
16+
module.exports = async function() {
17+
18+
const thisBranch = process.env.BRANCH;
19+
const thisVersion = require("../../package.json").version;
20+
21+
// Fetch the current list of ESLint versions from the `main` branch on GitHub
22+
const url = "https://raw.githubusercontent.com/eslint/eslint/main/docs/src/_data/versions.json";
23+
24+
const data = await eleventyFetch(url, {
25+
duration: "1d", // Cache for local development. Netlify does not keep this cache and will therefore always fetch from GitHub.
26+
type: "json"
27+
});
28+
29+
const { items } = data;
30+
31+
let foundItemForThisBranch = false;
32+
33+
for (const item of items) {
34+
const isItemForThisBranch = item.branch === thisBranch;
35+
36+
foundItemForThisBranch ||= isItemForThisBranch;
37+
38+
const isNumberVersion = /^\d/.test(item.version); // `false` for HEAD
39+
40+
if (isNumberVersion) {
41+
42+
// Make sure the version is correct
43+
if (isItemForThisBranch) {
44+
item.version = thisVersion;
45+
}
46+
47+
item.display = `v${item.version}`;
48+
} else {
49+
item.display = item.version;
50+
}
51+
52+
if (isItemForThisBranch) {
53+
item.selected = true;
54+
}
55+
}
56+
57+
// Add an empty item if this is not a production branch
58+
if (!foundItemForThisBranch) {
59+
items.unshift({
60+
version: "",
61+
branch: "",
62+
display: "",
63+
path: "",
64+
selected: true
65+
})
66+
}
67+
68+
return data;
69+
}

docs/src/_includes/components/nav-version-switcher.html

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@
1212
<span class="label__text">Version</span>
1313
</label>
1414
<select name="version selector" id="nav-version-select" aria-describedby="nav-version-infobox" class="c-custom-select switcher__select auto-switcher">
15-
<option value="HEAD" data-url="/docs/head/" {% if HEAD %}selected{% endif %}>HEAD</option>
16-
{% if config.showNextVersion == true %}
17-
<option value="{{ eslintNextVersion }}" data-url="/docs/next/" {% if GIT_BRANCH == "next" %}selected{% endif %}>v{{ eslintNextVersion }}</option>
18-
{% endif %}
19-
<option value="{{ eslintVersion }}" data-url="/docs/latest/" {% if GIT_BRANCH == "latest" %}selected{% endif %}>v{{ eslintVersion }}</option>
20-
{% for version in versions.items %}
21-
<option value="{{ version.number }}"
22-
data-url="{{ version.url }}">
23-
v{{ version.number }}
24-
</option>
15+
{% for item in eslintVersions.items %}
16+
<option value="{{ item.version }}" data-url="{{ item.path }}" {% if item.selected %}selected{% endif %}>{{ item.display }}</option>
2517
{% endfor %}
2618
</select>
2719
</div>

docs/src/_includes/components/version-switcher.html

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@
1212
<span class="label__text">Version</span>
1313
</label>
1414
<select name="version selector" id="version-select" aria-describedby="version-infobox" class="c-custom-select switcher__select auto-switcher">
15-
<option value="HEAD" data-url="/docs/head/" {% if HEAD %}selected{% endif %}>HEAD</option>
16-
{% if config.showNextVersion == true %}
17-
<option value="{{ eslintNextVersion }}" data-url="/docs/next/" {% if GIT_BRANCH=="next" %}selected{% endif %}>v{{ eslintNextVersion }}</option>
18-
{% endif %}
19-
<option value="{{ eslintVersion }}" data-url="/docs/latest/" {% if GIT_BRANCH == "latest" %}selected{% endif %}>v{{ eslintVersion }}</option>
20-
{% for version in versions.items %}
21-
<option value="{{ version.number }}"
22-
data-url="{{ version.url }}">
23-
v{{ version.number }}
24-
</option>
15+
{% for item in eslintVersions.items %}
16+
<option value="{{ item.version }}" data-url="{{ item.path }}" {% if item.selected %}selected{% endif %}>{{ item.display }}</option>
2517
{% endfor %}
2618
</select>
2719
</div>
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<ul class="versions-list">
2-
<li><a href="/docs/head/" {% if HEAD %} data-current="true" {% endif %}>HEAD</a></li>
3-
{% if config.showNextVersion == true %}
4-
<li><a href="/docs/next/" {% if GIT_BRANCH == "next" %} data-current="true" {% endif %}>v{{ eslintNextVersion }}</a></li>
5-
{% endif %}
6-
<li><a href="/docs/latest/" {% if GIT_BRANCH == "latest" %} data-current="true" {% endif %}>v{{ eslintVersion }}</a></li>
7-
{%- for version in versions.items -%}
8-
<li><a href="{{ version.url }}">v{{ version.number }}</a></li>
2+
{% for item in eslintVersions.items %}
3+
<li><a href="{{ item.path }}" {% if item.selected %} data-current="true" {% endif %}>{{ item.display }}</a></li>
94
{%- endfor -%}
105
</ul>

0 commit comments

Comments
 (0)