From df94b8dce51fc8d62358061fcadd7070983933c1 Mon Sep 17 00:00:00 2001 From: Donovan Hutchinson Date: Mon, 11 Nov 2019 21:19:04 +0000 Subject: [PATCH 1/3] Adding repl --- _includes/default.njk | 1 - events/index.md | 19 +++++++++++++++++++ repl/index.md | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 repl/index.md diff --git a/_includes/default.njk b/_includes/default.njk index 9bdf6b8..dbafc68 100644 --- a/_includes/default.njk +++ b/_includes/default.njk @@ -1,6 +1,5 @@ --- layout: base.njk -templateClass: tmpl-post ---

{{ title }}

diff --git a/events/index.md b/events/index.md index 156891e..8d8f42c 100644 --- a/events/index.md +++ b/events/index.md @@ -16,6 +16,8 @@ When the EventEmitter object emits an event, all of the functions attached to th This example creates an event listener for `foo` events, and an event emitter to fire these events. +
+ ```javascript const { EventEmitter } = require('events'); @@ -35,6 +37,23 @@ eventEmitter.on('foo', foo); eventEmitter.emit('foo'); ``` +
+ + + ## Passing parameters When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners. diff --git a/repl/index.md b/repl/index.md new file mode 100644 index 0000000..f301d80 --- /dev/null +++ b/repl/index.md @@ -0,0 +1,23 @@ +--- +layout: default.njk +title: Node.js REPL +--- + +Use this space to try out code from the examples! + + + +
+ + From 97896f48bbfcc460d1b601b9c88695eadbba4d45 Mon Sep 17 00:00:00 2001 From: Donovan Hutchinson Date: Tue, 12 Nov 2019 19:19:29 +0000 Subject: [PATCH 2/3] Adapting repl code to apply to all code blocks automatically --- _includes/default.njk | 71 ++++++++++++++++++++++++++++++++++++++++++- events/index.md | 34 ++++++--------------- js/main.js | 21 +++++++++++++ 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/_includes/default.njk b/_includes/default.njk index dbafc68..0c5a97a 100644 --- a/_includes/default.njk +++ b/_includes/default.njk @@ -1,7 +1,76 @@ --- layout: base.njk --- +<<<<<<< HEAD

{{ title }}

-{{ content | safe }} \ No newline at end of file +{{ content | safe }} +======= + + + + + + {{ title }} + + + + + + + + + + +
+
+ + +
+
+
+
+ +
+

{{ title }}

+ {{ content | safe }} +
+
+
+ + + +>>>>>>> Adapting repl code to apply to all code blocks automatically diff --git a/events/index.md b/events/index.md index 8d8f42c..9b2c2f2 100644 --- a/events/index.md +++ b/events/index.md @@ -16,8 +16,6 @@ When the EventEmitter object emits an event, all of the functions attached to th This example creates an event listener for `foo` events, and an event emitter to fire these events. -
- ```javascript const { EventEmitter } = require('events'); @@ -37,39 +35,27 @@ eventEmitter.on('foo', foo); eventEmitter.emit('foo'); ``` -
- - - ## Passing parameters When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners. For example: -``` +```javascript +const { EventEmitter } = require("events"); + +// create an emitter and bind some events to it +const eventEmitter = new EventEmitter(); + const foo = function foo(bar) { - console.log(`foo has been passed ${bar}`) -} + console.log(`foo has been passed ${bar}`); +}; // Bind the connection event with the listner1 function -eventEmitter.on('foo', foo) +eventEmitter.on("foo", foo); // fire the event -eventEmitter.emit('foo', 'bar') +eventEmitter.emit("foo", "bar"); ``` ## Summary diff --git a/js/main.js b/js/main.js index 401d2b1..b1f2b89 100644 --- a/js/main.js +++ b/js/main.js @@ -1,8 +1,29 @@ +/* Add an edit in repl link to code blocks */ + +function checkForReplLinks() { + const replCode = document.querySelectorAll('pre'); + [...replCode].forEach(pre => { + console.log(pre); + const text = encodeURI(pre.innerText); + const link = document.createElement('a'); + link.title = 'Run this code in the REPL'; + link.innerText = 'Run this code in the REPL'; + link.href = '/repl/?code=' + text; + const paragraph = document.createElement('p'); + paragraph.appendChild(link); + const wrapper = document.createElement('div'); + pre.parentNode.insertBefore(wrapper, pre); + wrapper.appendChild(pre); + wrapper.appendChild(paragraph); + }); +} + window.addEventListener('DOMContentLoaded', event => { if (window.localStorage) { window.topicsCompleted = getTopicsFromLocalStorage(); updateUI(); } + checkForReplLinks(); }); function updateUI() { From bd8f47169a72ae73d660ea2fdc0e58ff025aa5e7 Mon Sep 17 00:00:00 2001 From: Donovan Hutchinson Date: Wed, 13 Nov 2019 19:01:58 +0000 Subject: [PATCH 3/3] Making repl link optional --- _includes/default.njk | 71 +------------------------------------------ events/index.md | 14 +++++++-- js/main.js | 29 ++++++++++++------ repl/index.md | 2 +- 4 files changed, 33 insertions(+), 83 deletions(-) diff --git a/_includes/default.njk b/_includes/default.njk index 0c5a97a..dbafc68 100644 --- a/_includes/default.njk +++ b/_includes/default.njk @@ -1,76 +1,7 @@ --- layout: base.njk --- -<<<<<<< HEAD

{{ title }}

-{{ content | safe }} -======= - - - - - - {{ title }} - - - - - - - - - - -
-
- - -
-
-
-
- -
-

{{ title }}

- {{ content | safe }} -
-
-
- - - ->>>>>>> Adapting repl code to apply to all code blocks automatically +{{ content | safe }} \ No newline at end of file diff --git a/events/index.md b/events/index.md index 9b2c2f2..b3f477d 100644 --- a/events/index.md +++ b/events/index.md @@ -16,6 +16,8 @@ When the EventEmitter object emits an event, all of the functions attached to th This example creates an event listener for `foo` events, and an event emitter to fire these events. +
+ ```javascript const { EventEmitter } = require('events'); @@ -35,14 +37,18 @@ eventEmitter.on('foo', foo); eventEmitter.emit('foo'); ``` +
+ ## Passing parameters When an event is emitted using the `emit` method, the subsequent arguments are passed through to the listeners. For example: +
+ ```javascript -const { EventEmitter } = require("events"); +const { EventEmitter } = require('events'); // create an emitter and bind some events to it const eventEmitter = new EventEmitter(); @@ -52,12 +58,14 @@ const foo = function foo(bar) { }; // Bind the connection event with the listner1 function -eventEmitter.on("foo", foo); +eventEmitter.on('foo', foo); // fire the event -eventEmitter.emit("foo", "bar"); +eventEmitter.emit('foo', 'bar'); ``` +
+ ## Summary Listeners _listen_ for events, that are _emitted_ from emitters. diff --git a/js/main.js b/js/main.js index b1f2b89..ce3028d 100644 --- a/js/main.js +++ b/js/main.js @@ -1,20 +1,31 @@ /* Add an edit in repl link to code blocks */ function checkForReplLinks() { - const replCode = document.querySelectorAll('pre'); - [...replCode].forEach(pre => { - console.log(pre); - const text = encodeURI(pre.innerText); + // const replCode = document.querySelectorAll('pre'); + // [...replCode].forEach(pre => { + // console.log(pre); + // const text = encodeURI(pre.innerText); + // const link = document.createElement('a'); + // link.title = 'Run this code in the REPL'; + // link.innerText = 'Run this code in the REPL'; + // link.href = '/repl/?code=' + text; + // const paragraph = document.createElement('p'); + // paragraph.appendChild(link); + // const wrapper = document.createElement('div'); + // pre.parentNode.insertBefore(wrapper, pre); + // wrapper.appendChild(pre); + // wrapper.appendChild(paragraph); + // }); + const replCode = document.querySelectorAll('.repl-code'); + [...replCode].forEach(code => { + const codeText = encodeURI(code.innerText); const link = document.createElement('a'); link.title = 'Run this code in the REPL'; link.innerText = 'Run this code in the REPL'; - link.href = '/repl/?code=' + text; + link.href = '/repl/?code=' + codeText; const paragraph = document.createElement('p'); paragraph.appendChild(link); - const wrapper = document.createElement('div'); - pre.parentNode.insertBefore(wrapper, pre); - wrapper.appendChild(pre); - wrapper.appendChild(paragraph); + code.appendChild(paragraph); }); } diff --git a/repl/index.md b/repl/index.md index f301d80..54f1fa4 100644 --- a/repl/index.md +++ b/repl/index.md @@ -1,5 +1,5 @@ --- -layout: default.njk +layout: default title: Node.js REPL ---