Skip to content

Commit 3047df8

Browse files
add new docs version/redirect javascript processing
1 parent fcd2c3c commit 3047df8

File tree

4 files changed

+168
-99
lines changed

4 files changed

+168
-99
lines changed

_docs/paths_as_json_array.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!env node
2+
3+
var readline = require('readline');
4+
var fs = require('fs')
5+
6+
var lines = [];
7+
process.stdin.pipe(require('split')())
8+
.on('data', lines.push.bind(lines))
9+
.on('close', function() {
10+
lines = lines
11+
.map(function(x) { return x.replace(/^\.\//, ""); })
12+
.filter(function (x) { return x.trim(); });
13+
14+
console.log(JSON.stringify(lines));
15+
});

_docs/process_docs.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ for file in `find . -name "*.html" | grep -v "/partials/"` ; do
2727
) > $file.tmp && mv -f $file.tmp $file;
2828
fi
2929
done
30+
31+
find . -type d -depth 1 | sort | node ./paths_as_json_array.js > versions.json
32+
33+
for i in `find . -type d -depth 1` ; do
34+
pushd $i;
35+
find . -name "*.html" | sort | node ../paths_as_json_array.js > files.json
36+
popd;
37+
done

_layouts/docs.html

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
---
22
---
33

4-
{% assign paths = site.docs | map: "path" %}
5-
{% capture allpaths %}
6-
{% for path in paths %}
7-
{% assign pathel = path | split: "/" %}
8-
{{ pathel[1] }}
9-
{% endfor %}
10-
{% endcapture %}
11-
124
{% include base_path %}
135

14-
{% assign pagepath = page.path | split: "/" %}
15-
166
<!doctype html>
177
<html lang="{{ site.locale | slice: 0,2 }}" class="no-js">
188
<head>
199
{% include head.html %}
2010
{% include head/custom.html %}
11+
<script src="/assets/js/docs-version-switcher.js"></script>
2112
</head>
2213

2314
<body>
@@ -27,95 +18,6 @@
2718

2819
{{ content }}
2920

30-
<div class="docs__version_switcher">
31-
<select id="docs__version_switcher" onchange="switchVersion(this)">
32-
{% assign versions = allpaths | remove: "index.html" | strip | split: " " | uniq | sort | reverse %}
33-
{% for version in versions %}
34-
<option value="{{version}}">{{version}}</option>
35-
{% endfor %}
36-
</select>
37-
38-
<script>
39-
var el = document.getElementById("docs__version_switcher");
40-
var options = Array.prototype.slice.call(el.children);
41-
var uiRouterVersion = "{{ pagepath[1] }}";
42-
console.log("ui-router " + uiRouterVersion + " docs");
43-
44-
function compareDigit(l, r) {
45-
var ln = parseInt(l, 10), rn = parseInt(r, 10);
46-
if (!isNaN(ln) && !isNaN(rn)) return ln - rn;
47-
if (!isNaN(ln)) return -1;
48-
if (!isNaN(rn)) return 1;
49-
if (ln < rn) return -1;
50-
if (ln > rn) return 1;
51-
return 0;
52-
}
53-
54-
var compareVersions = function (left, right) {
55-
var a = left.value.split('.'), b = right.value.split('.');
56-
while (a.length && b.length) {
57-
var diff = compareDigit(a.shift(), b.shift());
58-
if (diff !== 0) return diff;
59-
}
60-
if (a.length) return 1;
61-
if (b.length) return -1;
62-
return 0;
63-
};
64-
65-
function reverse(compareFn) {
66-
return function(left, right) {
67-
return compareFn(right, left);
68-
}
69-
}
70-
71-
options.forEach(function(option) { el.removeChild(option); });
72-
options.sort(reverse(compareVersions));
73-
options.forEach(function(option) { el.appendChild(option); });
74-
var selected = options.filter(function(option) { return option.value == uiRouterVersion; });
75-
if (selected.length) selected[0].selected = true;
76-
77-
function switchVersion(version) {
78-
var current = /(\/docs)\/([^/]*)\/?(.*)/.exec("{{page.url}}");
79-
document.location.pathname = current[1] + "/" + version.value + "/" + current[3];
80-
}
81-
var injectChecks = 0;
82-
83-
function injectVersionSwitcher() {
84-
el.parentElement.removeChild(el);
85-
86-
let tsdBreadcrumb = document.querySelectorAll(".tsd-page-title .tsd-breadcrumb")[0];
87-
if (tsdBreadcrumb) {
88-
let li = document.createElement("li");
89-
li.appendChild(el);
90-
if (tsdBreadcrumb.children.length) {
91-
firstChild = tsdBreadcrumb.children[0];
92-
tsdBreadcrumb.insertBefore(li, firstChild);
93-
} else {
94-
tsdBreadcrumb.appendChild(li);
95-
}
96-
}
97-
98-
let ngDocMenu = document.querySelectorAll("header.header .navbar-inner ul.nav")[0];
99-
if (ngDocMenu) {
100-
let li = document.createElement("li");
101-
li.appendChild(el);
102-
li.style.padding = 0;
103-
li.style.paddingTop = "5px";
104-
li.style.paddingLeft = "1em";
105-
li.style.height = "40px";
106-
ngDocMenu.appendChild(li);
107-
}
108-
109-
if (++injectChecks < 5 && !tsdBreadcrumb && !ngDocMenu) {
110-
setTimeout(injectVersionSwitcher, 250);
111-
}
112-
}
113-
114-
injectVersionSwitcher();
115-
</script>
116-
117-
</div>
118-
11921
<div class="page__footer">
12022
<footer>
12123
{% include footer.html %}

assets/js/docs-version-switcher.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"use strict";
2+
3+
(function () {
4+
function fetchUrl(url, cb, err) {
5+
var request = new XMLHttpRequest();
6+
request.open('GET', url, true);
7+
8+
request.onload = function () {
9+
if (request.status >= 200 && request.status < 400) {
10+
cb(JSON.parse(request.responseText));
11+
} else {
12+
if (typeof err === 'function') err()
13+
}
14+
request.onerror = err
15+
};
16+
17+
request.send();
18+
}
19+
20+
var el = document.getElementById("docs__version_switcher");
21+
el = document.createElement('select');
22+
el.id = "docs__version_switcher";
23+
el.onchange = switchVersion;
24+
25+
// <select id="docs__version_switcher" onchange="switchVersion(this)">
26+
// {% assign versions = allpaths | remove: "index.html" | strip | split: " " | uniq | sort | reverse %}
27+
// {% for version in versions %}
28+
// <option value="{{version}}">{{version}}</option>
29+
// {% endfor %}
30+
// </select>
31+
32+
var verRegExp = new RegExp('(.*/docs/)([^/]*)/?(.*)');
33+
34+
var docsVer = verRegExp.exec(document.location.pathname);
35+
if (!docsVer) {
36+
throw new Error("Could not determine docs version.");
37+
}
38+
39+
// get regexp match
40+
var basePath = docsVer[1];
41+
var uiRouterVersion = docsVer[2];
42+
var documentationPage = docsVer[3];
43+
44+
console.log("ui-router " + uiRouterVersion + " docs");
45+
46+
function compareDigit(l, r) {
47+
var ln = parseInt(l, 10), rn = parseInt(r, 10);
48+
if (!isNaN(ln) && !isNaN(rn)) return ln - rn;
49+
if (!isNaN(ln)) return -1;
50+
if (!isNaN(rn)) return 1;
51+
if (l < r) return -1;
52+
if (l > r) return 1;
53+
return 0;
54+
}
55+
56+
var compareVersions = function (left, right) {
57+
var a = left.split(/[.+-]/), b = right.split(/[.+-]/);
58+
while (a.length && b.length) {
59+
var diff = compareDigit(a.shift(), b.shift());
60+
if (diff !== 0) return diff;
61+
}
62+
if (a.length) return 1;
63+
if (b.length) return -1;
64+
return 0;
65+
};
66+
67+
function reverse(compareFn) {
68+
return function (left, right) {
69+
return compareFn(right, left);
70+
}
71+
}
72+
73+
var injectChecks = 0;
74+
var tsdBreadcrumb, ngDocMenu;
75+
76+
function injectVersionSwitcher() {
77+
var li;
78+
79+
// TypeDoc
80+
tsdBreadcrumb = document.querySelectorAll(".tsd-page-title .tsd-breadcrumb")[0];
81+
if (tsdBreadcrumb) {
82+
li = document.createElement("li");
83+
li.appendChild(el);
84+
if (tsdBreadcrumb.children.length) {
85+
var firstChild = tsdBreadcrumb.children[0];
86+
tsdBreadcrumb.insertBefore(li, firstChild);
87+
} else {
88+
tsdBreadcrumb.appendChild(li);
89+
}
90+
}
91+
92+
// NgDoc
93+
ngDocMenu = document.querySelectorAll("header.header .navbar-inner ul.nav")[0];
94+
if (ngDocMenu) {
95+
li = document.createElement("li");
96+
li.appendChild(el);
97+
li.style.padding = 0;
98+
li.style.paddingTop = "5px";
99+
li.style.paddingLeft = "1em";
100+
li.style.height = "40px";
101+
ngDocMenu.appendChild(li);
102+
}
103+
104+
if (++injectChecks < 5 && !tsdBreadcrumb && !ngDocMenu) {
105+
setTimeout(injectVersionSwitcher, 250);
106+
}
107+
}
108+
109+
function switchVersion(evt) {
110+
var version = evt.target.value;
111+
if (tsdBreadcrumb) window.location.hash = "";
112+
113+
function gotoRootPage() {
114+
document.location.pathname = basePath + version;
115+
}
116+
117+
function validatePage(versionFiles) {
118+
var matches = versionFiles.indexOf(documentationPage) !== -1;
119+
if (matches) {
120+
document.location.pathname = basePath + version + '/' + documentationPage;
121+
} else {
122+
gotoRootPage();
123+
}
124+
}
125+
126+
fetchUrl(basePath + version + "/files.json", validatePage, gotoRootPage);
127+
}
128+
129+
injectVersionSwitcher();
130+
131+
fetchUrl(basePath + 'versions.json', function(versions) {
132+
console.log(versions);
133+
versions.sort(reverse(compareVersions));
134+
console.log(versions);
135+
136+
versions.forEach(function(ver) {
137+
var option = document.createElement('option');
138+
option.value = ver;
139+
option.innerHTML = ver;
140+
option.selected = (ver === uiRouterVersion);
141+
el.appendChild(option);
142+
});
143+
})
144+
})();

0 commit comments

Comments
 (0)