Skip to content

Commit 62e4f84

Browse files
committed
Adapting repl code to apply to all code blocks automatically
1 parent 321119e commit 62e4f84

3 files changed

Lines changed: 37 additions & 98 deletions

File tree

_includes/default.njk

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ title: OpenJS NodeJS Application Developer Study Guide
4040
<div class="row">
4141
<div class="sidebar column">
4242
<nav>
43-
<<<<<<< HEAD
4443
<ul class="topics">
4544
{% for topic in topics %}
4645
<li data-topic="{{topic.url}}">
@@ -59,79 +58,12 @@ title: OpenJS NodeJS Application Developer Study Guide
5958
<footer class="container">
6059
<div class="row">
6160
<div class="column column-75 column-offset-25">
62-
<p><a href="https://github.com/Node-Study-Guide/openjs-nodejs-application-developer-study-guide/tree/master/{{ page.inputPath }}">Edit this page on GitHub</a></p>
61+
<p>
62+
<a href="https://github.com/Node-Study-Guide/openjs-nodejs-application-developer-study-guide/tree/master/{{ page.inputPath }}">Edit this page on GitHub</a>
63+
</p>
6364
</div>
6465
</div>
65-
66-
</footer>
67-
</body>
68-
</html>
69-
=======
70-
<ul>
71-
<li>
72-
<a href="#" {% if page.url === '/buffer/' %} class="current" {% endif %}>Buffer and Streams</a>
73-
</li>
74-
<li>
75-
<a href="#">Control flow</a>
76-
</li>
77-
<li>
78-
<a href="#">Child Processes</a>
79-
</li>
80-
<li>
81-
<a href="#">Diagnostics</li>
82-
<li>
83-
<a href="#">Error Handling</a>
84-
</li>
85-
<li>
86-
<a href="#">Node.js CLI</a>
87-
</li>
88-
<li>
89-
<a href="{{ "/events" | url }}" {% if page.url === '/events/' %} class="current" {% endif %}>Events</li>
90-
<li>
91-
<a href="#">File System</a>
92-
</li>
93-
<li>
94-
<a href="#">JavaScript Prerequisites</a>
95-
</li>
96-
<li>
97-
<a href="#">Module system</a>
98-
</li>
99-
<li>
100-
<a href="#">Process/Operating System</a>
101-
</li>
102-
<li>
103-
<a href="#">Package.json</a>
104-
</li>
105-
<li>
106-
<a href="#">Unit Testing</a>
107-
</li>
108-
</ul>
109-
</nav>
110-
<nav class="secondary">
111-
<ul>
112-
<li>
113-
<a href="{{ "/repl/" | url }}" {% if page.url === '/repl/' %} class="current" {% endif %}>Node.js REPL</a>
114-
</li>
115-
</ul>
116-
</nav>
117-
</div>
118-
<div class="main-content column column-75">
119-
<h1>{{ title }}</h1>
12066

121-
{{ content | safe }}
122-
</div>
123-
</div>
124-
</main>
125-
<footer class="container">
126-
<div class="row">
127-
<div class="column column-75 column-offset-25">
128-
<p>
129-
<a href="https://github.com/Node-Study-Guide/openjs-nodejs-application-developer-study-guide/tree/master/{{ page.inputPath }}">Edit this page on GitHub</a>
130-
</p>
131-
</div>
132-
</div>
133-
134-
</footer>
135-
</body>
136-
</html>
137-
>>>>>>> Adding repl
67+
</footer>
68+
</body>
69+
</html>

events/index.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ When the EventEmitter object emits an event, all of the functions attached to th
1414

1515
This example creates an event listener for `foo` events, and an event emitter to fire these events.
1616

17-
<div class="repl-code">
18-
1917
```javascript
2018
const { EventEmitter } = require("events");
2119

@@ -35,39 +33,27 @@ eventEmitter.on("foo", foo);
3533
eventEmitter.emit("foo");
3634
```
3735

38-
</div>
39-
40-
<script>
41-
// TODO - move to main.js when other PRs merged
42-
const replCode = document.querySelectorAll('.repl-code');
43-
[...replCode].forEach(code => {
44-
const codeText = encodeURI(code.innerText);
45-
const link = document.createElement('a');
46-
link.title = "Run this code in the REPL";
47-
link.innerText = "Run this code in the REPL";
48-
link.href = "/repl/?code=" + codeText;
49-
const paragraph = document.createElement('p');
50-
paragraph.appendChild(link);
51-
code.appendChild(paragraph);
52-
});
53-
</script>
54-
5536
## Passing parameters
5637

5738
When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners.
5839

5940
For example:
6041

61-
```
42+
```javascript
43+
const { EventEmitter } = require("events");
44+
45+
// create an emitter and bind some events to it
46+
const eventEmitter = new EventEmitter();
47+
6248
const foo = function foo(bar) {
63-
console.log(`foo has been passed ${bar}`)
64-
}
49+
console.log(`foo has been passed ${bar}`);
50+
};
6551

6652
// Bind the connection event with the listner1 function
67-
eventEmitter.on('foo', foo)
53+
eventEmitter.on("foo", foo);
6854

6955
// fire the event
70-
eventEmitter.emit('foo', 'bar')
56+
eventEmitter.emit("foo", "bar");
7157
```
7258

7359
## Summary

js/main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
/* Add an edit in repl link to code blocks */
2+
3+
function checkForReplLinks() {
4+
const replCode = document.querySelectorAll("pre");
5+
[...replCode].forEach(pre => {
6+
console.log(pre);
7+
const text = encodeURI(pre.innerText);
8+
const link = document.createElement("a");
9+
link.title = "Run this code in the REPL";
10+
link.innerText = "Run this code in the REPL";
11+
link.href = "/repl/?code=" + text;
12+
const paragraph = document.createElement("p");
13+
paragraph.appendChild(link);
14+
const wrapper = document.createElement("div");
15+
pre.parentNode.insertBefore(wrapper, pre);
16+
wrapper.appendChild(pre);
17+
wrapper.appendChild(paragraph);
18+
});
19+
}
20+
121
window.addEventListener("DOMContentLoaded", event => {
222
if (window.localStorage) {
323
window.topicsCompleted = getTopicsFromLocalStorage();
424
updateUI();
525
}
26+
checkForReplLinks();
627
});
728

829
function updateUI() {

0 commit comments

Comments
 (0)