-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
110 lines (96 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function getVersionPath(path, versions, defaultVersion) {
/**
* If the path is /, /README.md or /index.html, we want to redirect to the default version.
* Otherwise, we want to redirect to the versioned path.
*/
const parts = path.split('/');
const version = versions.find((v) => v.folder === parts[1]);
return version ? version.folder : defaultVersion;
}
function versionedDocsPlugin(hook, vm) {
var versions = vm.config.versions || [];
var defaultVersion = vm.config.versions.find((v) => v.default).folder;
function updateVersion(version) {
var newPath = vm.route.path.split('/').slice(2).join('/');
if (newPath === '' || newPath === 'README.md') {
newPath = '/';
}
window.location.href = '/' + version + newPath;
vm.basePath = '/' + version + newPath;
}
function initVersionSelector() {
// Version selector
var selector = document.createElement('div');
selector.className = 'version-selector';
selector.innerHTML = `
<select>
${versions
.map(
(v) =>
`<option value="${v.folder}" ${
v.default ? 'selected' : ''
}>${v.label}</option>`
)
.join('')}
</select>
`;
// Adding event listener
var versionPath = (window.location.pathname && window.location.pathname.split('/')[1]) || defaultVersion;
selector.querySelector('select').value = versionPath;
selector.querySelector('select').addEventListener('change', function () {
updateVersion(this.value);
});
// Adding label
var labelText = vm.config.versionSelectorLabel || 'Version:';
var label = document.createElement('span');
label.className = 'version-selector-label';
label.textContent = labelText;
selector.insertBefore(label, selector.querySelector('select'));
var nameEl = document.querySelector('.app-name');
if (nameEl) {
var versionLabel = versions.find((v) => v.folder === versionPath).label;
nameEl.innerHTML += ` <small>${versionLabel}</small>`;
nameEl.parentNode.insertBefore(selector, nameEl.nextElementSibling);
}
return selector;
}
hook.ready(function () {
// Set coverpage to false if it's not a versioned coverpage
if (vm.route.path !== '/_coverpage.md') {
vm.config.coverpage = false;
}
// Initialize the version selector
versionSelector = initVersionSelector();
});
hook.beforeEach(function (html, next) {
// Replace {{versionLabel}} with the current version label in all markdown files
var versionPath = getVersionPath(vm.compiler.contentBase, versions, defaultVersion);
var version = versions.find((v) => v.folder === versionPath);
if (version) {
var versionLabel = version.label;
var updatedHtml = html.replace(/{{versionLabel}}/g, versionLabel);
next(updatedHtml);
} else {
next(html);
}
});
hook.doneEach(function () {
// Replace {{versionLabel}} with the current version label in coverpage
var versionPath = getVersionPath(vm.compiler.contentBase, versions, defaultVersion);
var version = versions.find((v) => v.folder === versionPath);
if (version) {
var versionLabel = version.label;
var cover = document.querySelector('.cover');
if (cover) {
cover.innerHTML = cover.innerHTML.replace(/{{versionLabel}}/g, versionLabel);
}
}
});
}
window.$docsify.plugins = [].concat(versionedDocsPlugin, window.$docsify.plugins);
(function() {
if (window.location.pathname === '/' || window.location.pathname === '/index.html') {
var defaultVersion = window.$docsify.versions.find((v) => v.default).folder;
window.location.replace('/' + defaultVersion + '/');
}
})();