Skip to content

Commit 12dc83a

Browse files
committed
unify versioning code
1 parent 0880139 commit 12dc83a

File tree

2 files changed

+250
-33
lines changed

2 files changed

+250
-33
lines changed

v1.0/header.js

Lines changed: 128 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
const TRUNK_VERSION = "trunk/develop";
2+
const NONE_VERSION = "none"
3+
4+
function loadVersions() {
5+
return import('/versions.js').then((imported_versions_module) => {
6+
versions = imported_versions_module.versions
7+
return versions
8+
}
9+
).catch(() => {
10+
console.log("Versions loading failed")
11+
});
12+
}
13+
14+
115
const addModal = () => {
216
const modalHandler = () => {
317
const modal = document.getElementById('main-nav')
@@ -62,41 +76,136 @@ const remove_legacy_searchbox = () => {
6276
}
6377

6478
const old_docs_version = () => {
65-
const version = "v1.0"
66-
const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0];
67-
brief.textContent += " " + version;
68-
const base_page_url = window.location.pathname.split('/' + version + '/')[1];
79+
const version = get_current_version()
80+
81+
if (version != TRUNK_VERSION) {
82+
const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0];
83+
brief.textContent += " " + version;
84+
}
6985

7086
var warning = document.createElement("div");
7187
warning.style.width = '100%';
7288
warning.style.display = 'flex';
7389
warning.style.flexDirection = 'column';
7490
warning.innerHTML = `
75-
<a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/` + base_page_url + `">
91+
<a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/">
7692
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
7793
</a>
7894
`;
7995
const titlearea = document.getElementById('titlearea');
8096
titlearea.parentNode.insertBefore(warning, titlearea);
8197
}
8298

99+
const get_current_version = () => {
100+
const { pathname } = window.location;
101+
102+
const urlSplitLimiter = 3;
103+
const versionTokenPosition = 2;
104+
105+
if (pathname.startsWith("/docs/")) {
106+
return pathname.split("/", urlSplitLimiter)[versionTokenPosition]
107+
}
108+
109+
if (pathname.startsWith("/d4/de0/versions.html")) {
110+
return NONE_VERSION
111+
}
112+
113+
return TRUNK_VERSION;
114+
}
115+
116+
const getPreviousVersion = (versions, current_version) => {
117+
let index = -1;
118+
119+
if (typeof current_version.index === "number") {
120+
index = current_version.index;
121+
} else if ("value" in current_version) {
122+
index = versions.indexOf(current_version.value);
123+
}
124+
125+
if (index <= 0 || index >= versions.length) {
126+
return undefined;
127+
}
128+
129+
return {
130+
index: index - 1,
131+
value: versions[index - 1]
132+
};
133+
}
134+
135+
const getVersionMajorPart = (version) => {
136+
const major = version.split(".")[0].replace("v", "");
137+
138+
const major_as_number = parseInt(major, 10);
139+
140+
return major_as_number;
141+
};
142+
143+
144+
const floor_to_major_version = (versions, target_version) => {
145+
let target_major_version = getVersionMajorPart(target_version)
146+
let version_iterator = { value: target_version, index: versions.length - 1 }
147+
148+
while (getVersionMajorPart(getPreviousVersion(versions, version_iterator).value) == target_major_version) {
149+
version_iterator = getPreviousVersion(versions, version_iterator)
150+
}
151+
152+
return version_iterator.value
153+
}
154+
155+
const add_docs_versioning = () => {
156+
157+
loadVersions().then((versions) => {
158+
const latest_version = versions[versions.length - 1]
159+
let second_likely_popular_version = floor_to_major_version(versions, latest_version)
160+
161+
if (second_likely_popular_version == latest_version) {
162+
second_likely_popular_version = getPreviousVersion(versions, { value: latest_version }).value
163+
}
164+
165+
const current_version = get_current_version()
166+
167+
if (current_version != latest_version &&
168+
current_version != TRUNK_VERSION &&
169+
current_version != NONE_VERSION) {
170+
old_docs_version()
171+
}
172+
173+
const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0];
174+
const footer_prefix = `
175+
<li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;">
176+
<span style="color: var(--toc-foreground);">Docs version:</span>
177+
`
178+
179+
let footer_infix = ""
180+
181+
if (current_version == TRUNK_VERSION) {
182+
footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${TRUNK_VERSION}</span>, `
183+
} else {
184+
footer_infix += `<a href="/index.html">${TRUNK_VERSION}</a>, `
185+
}
186+
187+
not_trunk_versions = [latest_version, second_likely_popular_version]
188+
189+
for (let version of not_trunk_versions) {
190+
if (current_version != version) {
191+
footer_infix += `<a href="/docs/${version}/index.html">${version}</a>, `
192+
} else {
193+
footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${version}</span>, `
194+
}
195+
}
196+
197+
footer_infix += `<a href="/d4/de0/versions.html">others</a>`
198+
199+
footer.innerHTML = footer_prefix + footer_infix
200+
+ footer.innerHTML;
201+
})
202+
203+
}
204+
83205
const init_header = () => {
84206
addModal();
85207
create_nav_wrapper();
86208
remove_legacy_searchbox();
87209
onBurger();
88-
old_docs_version();
89-
90-
const breif = document.getElementById('projectbrief').getElementsByTagName('a')[0];
91-
breif.textContent += " v1.0";
92-
93-
const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0];
94-
footer.innerHTML = `
95-
<li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;">
96-
<span style="color: var(--toc-foreground);">Docs version:</span>
97-
<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">v1.0</span>,
98-
<a href="/docs/v2.0/` + window.location.pathname.split('/v1.0/')[1] + `">v2.0</a>,
99-
<a href="/` + window.location.pathname.split('/v1.0/')[1] + `">trunk/develop</a>
100-
</li>`
101-
+ footer.innerHTML;
210+
add_docs_versioning();
102211
}

v2.0/header.js

Lines changed: 122 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
const TRUNK_VERSION = "trunk/develop";
2+
const NONE_VERSION = "none"
3+
4+
function loadVersions() {
5+
return import('/versions.js').then((imported_versions_module) => {
6+
versions = imported_versions_module.versions
7+
return versions
8+
}
9+
).catch(() => {
10+
console.log("Versions loading failed")
11+
});
12+
}
13+
14+
115
const addModal = () => {
216
const modalHandler = () => {
317
const modal = document.getElementById('main-nav')
@@ -62,36 +76,130 @@ const remove_legacy_searchbox = () => {
6276
}
6377

6478
const old_docs_version = () => {
65-
const version = "v2.0"
66-
const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0];
67-
brief.textContent += " " + version;
68-
const base_page_url = window.location.pathname.split('/' + version + '/')[1];
79+
const version = get_current_version()
80+
81+
if (version != TRUNK_VERSION) {
82+
const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0];
83+
brief.textContent += " " + version;
84+
}
6985

7086
var warning = document.createElement("div");
7187
warning.style.width = '100%';
7288
warning.style.display = 'flex';
7389
warning.style.flexDirection = 'column';
7490
warning.innerHTML = `
75-
<a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/` + base_page_url + `">
91+
<a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/">
7692
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
7793
</a>
7894
`;
7995
const titlearea = document.getElementById('titlearea');
8096
titlearea.parentNode.insertBefore(warning, titlearea);
8197
}
8298

99+
const get_current_version = () => {
100+
const { pathname } = window.location;
101+
102+
const urlSplitLimiter = 3;
103+
const versionTokenPosition = 2;
104+
105+
if (pathname.startsWith("/docs/")) {
106+
return pathname.split("/", urlSplitLimiter)[versionTokenPosition]
107+
}
108+
109+
if (pathname.startsWith("/d4/de0/versions.html")) {
110+
return NONE_VERSION
111+
}
112+
113+
return TRUNK_VERSION;
114+
}
115+
116+
const getPreviousVersion = (versions, current_version) => {
117+
let index = -1;
118+
119+
if (typeof current_version.index === "number") {
120+
index = current_version.index;
121+
} else if ("value" in current_version) {
122+
index = versions.indexOf(current_version.value);
123+
}
124+
125+
if (index <= 0 || index >= versions.length) {
126+
return undefined;
127+
}
128+
129+
return {
130+
index: index - 1,
131+
value: versions[index - 1]
132+
};
133+
}
134+
135+
const getVersionMajorPart = (version) => {
136+
const major = version.split(".")[0].replace("v", "");
137+
138+
const major_as_number = parseInt(major, 10);
139+
140+
return major_as_number;
141+
};
142+
143+
144+
const floor_to_major_version = (versions, target_version) => {
145+
let target_major_version = getVersionMajorPart(target_version)
146+
let version_iterator = { value: target_version, index: versions.length - 1 }
147+
148+
while (getVersionMajorPart(getPreviousVersion(versions, version_iterator).value) == target_major_version) {
149+
version_iterator = getPreviousVersion(versions, version_iterator)
150+
}
151+
152+
return version_iterator.value
153+
}
154+
83155
const add_docs_versioning = () => {
84-
old_docs_version();
85156

86-
const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0];
87-
footer.innerHTML = `
88-
<li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;">
157+
loadVersions().then((versions) => {
158+
const latest_version = versions[versions.length - 1]
159+
let second_likely_popular_version = floor_to_major_version(versions, latest_version)
160+
161+
if (second_likely_popular_version == latest_version) {
162+
second_likely_popular_version = getPreviousVersion(versions, { value: latest_version }).value
163+
}
164+
165+
const current_version = get_current_version()
166+
167+
if (current_version != latest_version &&
168+
current_version != TRUNK_VERSION &&
169+
current_version != NONE_VERSION) {
170+
old_docs_version()
171+
}
172+
173+
const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0];
174+
const footer_prefix = `
175+
<li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;">
89176
<span style="color: var(--toc-foreground);">Docs version:</span>
90-
<a href="/docs/v1.0/` + window.location.pathname.split('/v2.0/')[1] + `">v1.0</a>,
91-
<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">v2.0</span>,
92-
<a href="/` + window.location.pathname.split('/v2.0/')[1] + `">trunk/develop</a>
93-
</li>`
94-
+ footer.innerHTML;
177+
`
178+
179+
let footer_infix = ""
180+
181+
if (current_version == TRUNK_VERSION) {
182+
footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${TRUNK_VERSION}</span>, `
183+
} else {
184+
footer_infix += `<a href="/index.html">${TRUNK_VERSION}</a>, `
185+
}
186+
187+
not_trunk_versions = [latest_version, second_likely_popular_version]
188+
189+
for (let version of not_trunk_versions) {
190+
if (current_version != version) {
191+
footer_infix += `<a href="/docs/${version}/index.html">${version}</a>, `
192+
} else {
193+
footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${version}</span>, `
194+
}
195+
}
196+
197+
footer_infix += `<a href="/d4/de0/versions.html">others</a>`
198+
199+
footer.innerHTML = footer_prefix + footer_infix
200+
+ footer.innerHTML;
201+
})
202+
95203
}
96204

97205
const init_header = () => {

0 commit comments

Comments
 (0)