Skip to content

Commit 97896f4

Browse files
committed
Adapting repl code to apply to all code blocks automatically
1 parent df94b8d commit 97896f4

File tree

3 files changed

+101
-25
lines changed

3 files changed

+101
-25
lines changed

_includes/default.njk

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
11
---
22
layout: base.njk
33
---
4+
<<<<<<< HEAD
45

56
<h1>{{ title }}</h1>
67

7-
{{ content | safe }}
8+
{{ content | safe }}
9+
=======
10+
<!doctype html>
11+
<html lang="en">
12+
<head>
13+
<meta charset="utf-8">
14+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
15+
<title>{{ title }}</title>
16+
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
17+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.css">
18+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.css">
19+
<link rel="stylesheet" href="{{ "/css/main.css" | url }}">
20+
<link rel="stylesheet"
21+
href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/styles/default.min.css">
22+
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/highlight.min.js"></script>
23+
<script>
24+
hljs.initHighlightingOnLoad();
25+
</script>
26+
<script src="{{ "/js/main.js" | url }}"></script>
27+
</head>
28+
<body>
29+
<header class="container">
30+
<div class="row">
31+
<div class="column column-50">
32+
<h1 class="logo">
33+
<a href="/">ONAD Study Guide</a>
34+
</h1>
35+
</div>
36+
<div class="top-links column column-50">
37+
<ul>
38+
<li>
39+
<a href="https://training.linuxfoundation.org/certification/jsnad/" target="_blank" title="OpenJS Node.js Application Developer (JSNAD)">JSNAD Certification</a>
40+
</li>
41+
</ul>
42+
</div>
43+
</div>
44+
</header>
45+
<main class="container">
46+
<div class="row">
47+
<div class="sidebar column">
48+
<nav>
49+
<ul class="topics">
50+
{% for topic in topics %}
51+
<li data-topic="{{topic.url}}">
52+
<a href="{{ topic.url | url }}" {% if page.url === topic.url %} class="current" {% endif %}>{{topic.title}}</a>
53+
</li>
54+
{% endfor %}
55+
</ul>
56+
</nav>
57+
</div>
58+
<div class="main-content column column-75">
59+
<h1>{{ title }}</h1>
60+
{{ content | safe }}
61+
</div>
62+
</div>
63+
</main>
64+
<footer class="container">
65+
<div class="row">
66+
<div class="column column-75 column-offset-25">
67+
<p>
68+
<a href="https://github.com/Node-Study-Guide/openjs-nodejs-application-developer-study-guide/tree/master/{{ page.inputPath }}">Edit this page on GitHub</a>
69+
</p>
70+
</div>
71+
</div>
72+
73+
</footer>
74+
</body>
75+
</html>
76+
>>>>>>> Adapting repl code to apply to all code blocks automatically

events/index.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ 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-
2119
```javascript
2220
const { EventEmitter } = require('events');
2321

@@ -37,39 +35,27 @@ eventEmitter.on('foo', foo);
3735
eventEmitter.emit('foo');
3836
```
3937

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

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

6142
For example:
6243

63-
```
44+
```javascript
45+
const { EventEmitter } = require("events");
46+
47+
// create an emitter and bind some events to it
48+
const eventEmitter = new EventEmitter();
49+
6450
const foo = function foo(bar) {
65-
console.log(`foo has been passed ${bar}`)
66-
}
51+
console.log(`foo has been passed ${bar}`);
52+
};
6753

6854
// Bind the connection event with the listner1 function
69-
eventEmitter.on('foo', foo)
55+
eventEmitter.on("foo", foo);
7056

7157
// fire the event
72-
eventEmitter.emit('foo', 'bar')
58+
eventEmitter.emit("foo", "bar");
7359
```
7460

7561
## 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)