Skip to content

Commit 2443d5b

Browse files
authored
Merge pull request #18 from Node-Study-Guide/repl
Adding repl
2 parents 8735407 + bd8f471 commit 2443d5b

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

_includes/default.njk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
layout: base.njk
3-
templateClass: tmpl-post
43
---
54

65
<h1>{{ title }}</h1>

events/index.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ When the EventEmitter object emits an event, all of the functions attached to th
1616

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

19+
<div class="repl-code">
20+
1921
```javascript
2022
const { EventEmitter } = require('events');
2123

@@ -35,24 +37,35 @@ eventEmitter.on('foo', foo);
3537
eventEmitter.emit('foo');
3638
```
3739

40+
</div>
41+
3842
## Passing parameters
3943

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

4246
For example:
4347

44-
```
48+
<div class="repl-code">
49+
50+
```javascript
51+
const { EventEmitter } = require('events');
52+
53+
// create an emitter and bind some events to it
54+
const eventEmitter = new EventEmitter();
55+
4556
const foo = function foo(bar) {
46-
console.log(`foo has been passed ${bar}`)
47-
}
57+
console.log(`foo has been passed ${bar}`);
58+
};
4859

4960
// Bind the connection event with the listner1 function
50-
eventEmitter.on('foo', foo)
61+
eventEmitter.on('foo', foo);
5162

5263
// fire the event
53-
eventEmitter.emit('foo', 'bar')
64+
eventEmitter.emit('foo', 'bar');
5465
```
5566

67+
</div>
68+
5669
## Summary
5770

5871
Listeners _listen_ for events, that are _emitted_ from emitters.

js/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
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+
const replCode = document.querySelectorAll('.repl-code');
20+
[...replCode].forEach(code => {
21+
const codeText = encodeURI(code.innerText);
22+
const link = document.createElement('a');
23+
link.title = 'Run this code in the REPL';
24+
link.innerText = 'Run this code in the REPL';
25+
link.href = '/repl/?code=' + codeText;
26+
const paragraph = document.createElement('p');
27+
paragraph.appendChild(link);
28+
code.appendChild(paragraph);
29+
});
30+
}
31+
132
window.addEventListener('DOMContentLoaded', event => {
233
if (window.localStorage) {
334
window.topicsCompleted = getTopicsFromLocalStorage();
435
updateUI();
536
}
37+
checkForReplLinks();
638
});
739

840
function updateUI() {

repl/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
layout: default
3+
title: Node.js REPL
4+
---
5+
6+
Use this space to try out code from the examples!
7+
8+
<script src="https://embed.runkit.com"></script>
9+
10+
<div id="repl"></div>
11+
12+
<script>
13+
14+
const urlParams = new URLSearchParams(window.location.search);
15+
const code = urlParams.get('code');
16+
17+
const notebook = RunKit.createNotebook({
18+
element: document.getElementById("repl"),
19+
source: code || "// Your JavaScript code goes here",
20+
minHeight: "500px",
21+
});
22+
23+
</script>

0 commit comments

Comments
 (0)