Skip to content

Commit a3e7c8b

Browse files
committed
Added gatsby-remark-babel-repl-link plug-in
1 parent 8c6ef38 commit a3e7c8b

8 files changed

Lines changed: 93 additions & 3 deletions

File tree

content/tutorial/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ return React.createElement('div', {className: 'shopping-list'},
129129
);
130130
```
131131

132-
[See full expanded version.](https://babeljs.io/repl/#?babili=false&evaluate=false&lineWrap=false&presets=react&targets=&browsers=&builtIns=false&debug=false&experimental=false&loose=false&spec=false&playground=true&code=%3Cdiv%20className%3D%22shopping-list%22%3E%0A%20%20%3Ch1%3EShopping%20List%20for%20%7Bprops.name%7D%3C%2Fh1%3E%0A%20%20%3Cul%3E%0A%20%20%20%20%3Cli%3EInstagram%3C%2Fli%3E%0A%20%20%20%20%3Cli%3EWhatsApp%3C%2Fli%3E%0A%20%20%20%20%3Cli%3EOculus%3C%2Fli%3E%0A%20%20%3C%2Ful%3E%0A%3C%2Fdiv%3E)
132+
[See full expanded version.](babel-repl://tutorial-expanded-version)
133133

134134
If you're curious, `createElement()` is described in more detail in the [API reference](/docs/react-api.html#createelement), but we won't be using it directly in this tutorial. Instead, we will keep using JSX.
135135

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div className="shopping-list">
2+
<h1>Shopping List for {props.name}</h1>
3+
<ul>
4+
<li>Instagram</li>
5+
<li>WhatsApp</li>
6+
<li>Oculus</li>
7+
</ul>
8+
</div>

gatsby-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ module.exports = {
6262
directory: 'examples',
6363
},
6464
},
65+
{
66+
resolve: 'gatsby-remark-babel-repl-link',
67+
options: {
68+
directory: 'examples',
69+
},
70+
},
6571
'gatsby-remark-use-jsx',
6672
{
6773
resolve: 'gatsby-remark-prismjs',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
},
8686
"devDependencies": {
8787
"eslint-config-prettier": "^2.6.0",
88+
"lz-string": "^1.4.4",
8889
"recursive-readdir": "^2.2.1",
8990
"unist-util-map": "^1.0.3"
9091
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const {existsSync, readFileSync} = require('fs');
2+
const LZString = require('lz-string');
3+
const {join} = require('path');
4+
const map = require('unist-util-map');
5+
6+
const PROTOCOL = 'babel-repl://';
7+
8+
// Matches compression used in Babel REPL
9+
// https://github.com/babel/website/blob/master/js/repl/UriUtils.js
10+
const compress = string =>
11+
LZString.compressToBase64(string)
12+
.replace(/\+/g, '-') // Convert '+' to '-'
13+
.replace(/\//g, '_') // Convert '/' to '_'
14+
.replace(/=+$/, ''); // Remove ending '='
15+
16+
module.exports = ({markdownAST}, {directory}) => {
17+
map(markdownAST, (node, index, parent) => {
18+
if (!directory.endsWith('/')) {
19+
directory += '/';
20+
}
21+
22+
if (node.type === 'link' && node.url.startsWith(PROTOCOL)) {
23+
let filePath = node.url.replace(PROTOCOL, directory);
24+
if (!filePath.endsWith('.js')) {
25+
filePath += '.js';
26+
}
27+
filePath = join(__dirname, '../..', filePath);
28+
29+
// Verify that the specified example file exists.
30+
if (!existsSync(filePath)) {
31+
console.error(
32+
`Invalid Babel REPL link specified; no such file "${filePath}"`,
33+
);
34+
process.exit(1);
35+
}
36+
37+
const code = compress(readFileSync(filePath, 'utf8'));
38+
const href = `https://babeljs.io/repl/#?presets=react&code_lz=${code}`;
39+
const text = node.children[0].value;
40+
41+
const anchorOpenNode = {
42+
type: 'html',
43+
value: `<a href="${href}" target="_blank">`,
44+
};
45+
46+
const textNode = {
47+
type: 'text',
48+
value: text,
49+
};
50+
51+
const anchorCloseNode = {
52+
type: 'html',
53+
value: '</a>',
54+
};
55+
56+
parent.children.splice(
57+
index,
58+
1,
59+
anchorOpenNode,
60+
textNode,
61+
anchorCloseNode,
62+
);
63+
}
64+
65+
// No change
66+
return node;
67+
});
68+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "gatsby-remark-babel-repl-link",
3+
"version": "0.0.1"
4+
}

plugins/gatsby-remark-codepen-examples/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const map = require('unist-util-map');
55
const CODEPEN_PROTOCOL = 'codepen://';
66
const DEFAULT_LINK_TEXT = 'Try it on CodePen';
77

8-
// TODO target="_blank"
98
module.exports = ({markdownAST}, {directory}) => {
109
map(markdownAST, (node, index, parent) => {
1110
if (!directory.startsWith('/')) {
@@ -22,7 +21,7 @@ module.exports = ({markdownAST}, {directory}) => {
2221
// from: [Try the Hello World example on CodePen](codepen:hello-world)
2322
// to: <a href="/<directory>/hello-world" target="_blank">Try the Hello World example on CodePen</a>
2423
if (node.type === 'link' && node.url.startsWith(CODEPEN_PROTOCOL)) {
25-
const href = node.url.replace(CODEPEN_PROTOCOL, `${directory}`);
24+
const href = node.url.replace(CODEPEN_PROTOCOL, directory);
2625
const text =
2726
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;
2827

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6086,6 +6086,10 @@ ltcdr@^2.2.1:
60866086
version "2.2.1"
60876087
resolved "https://registry.yarnpkg.com/ltcdr/-/ltcdr-2.2.1.tgz#5ab87ad1d4c1dab8e8c08bbf037ee0c1902287cf"
60886088

6089+
lz-string@^1.4.4:
6090+
version "1.4.4"
6091+
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
6092+
60896093
macaddress@^0.2.8:
60906094
version "0.2.8"
60916095
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"

0 commit comments

Comments
 (0)