Skip to content

Commit 900f951

Browse files
committed
add ES5/ESnext switcher on about page
1 parent d5fbdec commit 900f951

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

layouts/about.hbs

+78
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,84 @@
4444
</div>
4545
</div>
4646

47+
<style>
48+
.es-switcher > p.active, .es-switcher > p:hover {
49+
color: #FFF;
50+
background-color: rgb(51, 51, 51);
51+
border-color: rgb(51, 51, 51);
52+
}
53+
.es-switcher > p {
54+
display: inline-block;
55+
margin: 0px;
56+
cursor: pointer;
57+
padding: 0em 21px;
58+
border-style: solid;
59+
border-color: rgb(255, 255, 255);
60+
border-width: 3px;
61+
background-color: rgb(238, 238, 238);
62+
border-top-right-radius: 3px;
63+
border-top-left-radius: 3px;
64+
}
65+
.es-switcher > pre {
66+
border-top-left-radius: 0px !important;
67+
margin: 0px;
68+
}
69+
</style>
70+
71+
<script>
72+
var esNextText = 'Modern JavaScript:'
73+
var es5Text = 'ES5:'
74+
75+
function nextSiblingMatch (e, name, text) {
76+
var sib = e.nextSibling
77+
while (sib) {
78+
if (sib.nodeType == 1) {
79+
if (sib.nodeName == name && (!text || sib.textContent == text))
80+
return sib
81+
return null
82+
}
83+
sib = sib.nextSibling
84+
}
85+
}
86+
87+
Array.prototype.slice.call(document.querySelectorAll('p'))
88+
.filter(function (p) { return p.textContent == esNextText })
89+
.map(function (esNextP) {
90+
var esNextPre = nextSiblingMatch(esNextP, 'PRE')
91+
if (!esNextPre) return null
92+
var es5P = nextSiblingMatch(esNextPre, 'P')
93+
if (!es5P) return null
94+
var es5Pre = nextSiblingMatch(es5P, 'PRE')
95+
if (!es5Pre) return null
96+
return { esNextP: esNextP, esNextPre: esNextPre, es5P: es5P, es5Pre: es5Pre }
97+
})
98+
.filter(Boolean)
99+
.forEach(function (block) {
100+
var div = document.createElement('div')
101+
div.className = 'es-switcher'
102+
block.esNextP.parentElement.insertBefore(div, block.esNextP)
103+
block.esNextP.textContent = esNextText.replace(/:$/, '')
104+
block.es5P.textContent = es5Text.replace(/:$/, '')
105+
block.esNextP.className = 'active'
106+
div.appendChild(block.esNextP)
107+
div.appendChild(block.es5P)
108+
div.appendChild(block.esNextPre)
109+
div.appendChild(block.es5Pre)
110+
block.es5Pre.style.display = 'none'
111+
block.esNextP.addEventListener('click', function () {
112+
block.esNextPre.style.display = 'block'
113+
block.es5Pre.style.display = 'none'
114+
block.esNextP.className = 'active'
115+
block.es5P.className = ''
116+
})
117+
block.es5P.addEventListener('click', function () {
118+
block.esNextPre.style.display = 'none'
119+
block.es5Pre.style.display = 'block'
120+
block.esNextP.className = ''
121+
block.es5P.className = 'active'
122+
})
123+
})
124+
</script>
47125
{{> footer }}
48126
</body>
49127
</html>

locale/en/about/index.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ scalable network applications. In the following "hello world" example, many
1010
connections can be handled concurrently. Upon each connection the callback is
1111
fired, but if there is no work to be done Node is sleeping.
1212

13-
```javascript
13+
Modern JavaScript:
14+
15+
```js
1416
const http = require('http');
1517

1618
const hostname = '127.0.0.1';
@@ -24,6 +26,22 @@ http.createServer((req, res) => {
2426
});
2527
```
2628

29+
ES5:
30+
31+
```js
32+
var http = require('http');
33+
34+
var hostname = '127.0.0.1';
35+
var port = 1337;
36+
37+
http.createServer(function(req, res) {
38+
res.writeHead(200, { 'Content-Type': 'text/plain' });
39+
res.end('Hello World\n');
40+
}).listen(port, hostname, function() {
41+
console.log('Server running at http://%s:%d/', hostname, port);
42+
});
43+
```
44+
2745
This is in contrast to today's more common concurrency model where OS threads
2846
are employed. Thread-based networking is relatively inefficient and very
2947
difficult to use. Furthermore, users of Node are free from worries of

0 commit comments

Comments
 (0)