Browse Source

Merge pull request #255 from bvaughn/gatsby-remark-babel-repl-link

Added gatsby-remark-babel-repl-link plug-in
main
Brian Vaughn 7 years ago
committed by GitHub
parent
commit
bc9a412a4e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      content/docs/hello-world.md
  2. 2
      content/docs/jsx-in-depth.md
  3. 2
      content/docs/react-without-jsx.md
  4. 2
      content/home/examples/a-simple-component.md
  5. 4
      content/tutorial/tutorial.md
  6. 3
      examples/es5-syntax-example.js
  7. 3
      examples/jsx-simple-example.js
  8. 8
      examples/tutorial-expanded-version.js
  9. 6
      gatsby-config.js
  10. 1
      package.json
  11. 68
      plugins/gatsby-remark-babel-repl-link/index.js
  12. 4
      plugins/gatsby-remark-babel-repl-link/package.json
  13. 3
      plugins/gatsby-remark-codepen-examples/index.js
  14. 4
      yarn.lock

2
content/docs/hello-world.md

@ -31,4 +31,4 @@ The next few sections will gradually introduce you to using React. We will exami
React is a JavaScript library, and so it assumes you have a basic understanding of the JavaScript language. If you don't feel very confident, we recommend [refreshing your JavaScript knowledge](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) so you can follow along more easily.
We also use some of the ES6 syntax in the examples. We try to use it sparingly because it's still relatively new, but we encourage you to get familiar with [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [template literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals), [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), and [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) statements. You can use <a href="http://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact&experimental=false&loose=false&spec=false&code=const%20element%20%3D%20%3Ch1%3EHello%2C%20world!%3C%2Fh1%3E%3B%0Aconst%20container%20%3D%20document.getElementById('root')%3B%0AReactDOM.render(element%2C%20container)%3B%0A">Babel REPL</a> to check what ES6 code compiles to.
We also use some of the ES6 syntax in the examples. We try to use it sparingly because it's still relatively new, but we encourage you to get familiar with [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [template literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals), [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), and [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) statements. You can use the [Babel REPL](babel-repl://es5-syntax-example) to check what ES6 code compiles to.

2
content/docs/jsx-in-depth.md

@ -47,7 +47,7 @@ React.createElement(
)
```
If you want to test out how some specific JSX is converted into JavaScript, you can try out [the online Babel compiler](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-0&code=function%20hello()%20%7B%0A%20%20return%20%3Cdiv%3EHello%20world!%3C%2Fdiv%3E%3B%0A%7D).
If you want to test out how some specific JSX is converted into JavaScript, you can try out [the online Babel compiler](babel-repl://jsx-simple-example).
## Specifying The React Element Type

2
content/docs/react-without-jsx.md

@ -38,7 +38,7 @@ ReactDOM.render(
);
```
If you're curious to see more examples of how JSX is converted to JavaScript, you can try out [the online Babel compiler](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-0&code=function%20hello()%20%7B%0A%20%20return%20%3Cdiv%3EHello%20world!%3C%2Fdiv%3E%3B%0A%7D).
If you're curious to see more examples of how JSX is converted to JavaScript, you can try out [the online Babel compiler](babel-repl://jsx-simple-example).
The component can either be provided as a string, or as a subclass of `React.Component`, or a plain function for stateless components.

2
content/home/examples/a-simple-component.md

@ -5,4 +5,4 @@ order: 0
React components implement a `render()` method that takes input data and returns what to display. This example uses an XML-like syntax called JSX. Input data that is passed into the component can be accessed by `render()` via `this.props`.
**JSX is optional and not required to use React.** Try the [Babel REPL](http://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&code_lz=MYGwhgzhAEASCmIQHsCy8pgOb2vAHgC7wB2AJjAErxjCEB0AwsgLYAOyJph0A3gFDRoAJ1Jl4wgBQBKPoKEj4hAK7CS0SfIXQAPGQCWANwB8W7XEQo-hABb6I9NsORsHJMC3gBfM0J0B6AxMzaQBueR8ffmpaQgARAHlUelFyCU0_BCQ0DAhsXHdPAF4AIgAVMABPFGES6H9jABp5FmRlEkIAOWRxfjCgA&debug=false&circleciRepo=&evaluate=false&lineWrap=false&presets=react&targets=&version=6.26.0) to see the raw JavaScript code produced by the JSX compilation step.
**JSX is optional and not required to use React.** Try the [Babel REPL](babel-repl://es5-syntax-example) to see the raw JavaScript code produced by the JSX compilation step.

4
content/tutorial/tutorial.md

@ -28,7 +28,7 @@ Once you get a little familiar with the game, feel free to close that tab, as we
We'll assume some familiarity with HTML and JavaScript, but you should be able to follow along even if you haven't used them before.
If you need a refresher on JavaScript, we recommend reading [this guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript). Note that we're also using some features from ES6, a recent version of JavaScript. In this tutorial, we're using [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), and [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) statements. You can use <a href="http://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact&experimental=false&loose=false&spec=false&code=const%20element%20%3D%20%3Ch1%3EHello%2C%20world!%3C%2Fh1%3E%3B%0Aconst%20container%20%3D%20document.getElementById('root')%3B%0AReactDOM.render(element%2C%20container)%3B%0A">Babel REPL</a> to check what ES6 code compiles to.
If you need a refresher on JavaScript, we recommend reading [this guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript). Note that we're also using some features from ES6, a recent version of JavaScript. In this tutorial, we're using [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), and [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) statements. You can use the [Babel REPL](babel-repl://es5-syntax-example) to check what ES6 code compiles to.
### How to Follow Along
@ -129,7 +129,7 @@ return React.createElement('div', {className: 'shopping-list'},
);
```
[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)
[See full expanded version.](babel-repl://tutorial-expanded-version)
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.

3
examples/es5-syntax-example.js

@ -0,0 +1,3 @@
const element = <h1>Hello, world!</h1>;
const container = document.getElementById('root');
ReactDOM.render(element, container);

3
examples/jsx-simple-example.js

@ -0,0 +1,3 @@
function hello() {
return <div>Hello world!</div>;
}

8
examples/tutorial-expanded-version.js

@ -0,0 +1,8 @@
<div className="shopping-list">
<h1>Shopping List for {props.name}</h1>
<ul>
<li>Instagram</li>
<li>WhatsApp</li>
<li>Oculus</li>
</ul>
</div>

6
gatsby-config.js

@ -62,6 +62,12 @@ module.exports = {
directory: 'examples',
},
},
{
resolve: 'gatsby-remark-babel-repl-link',
options: {
directory: 'examples',
},
},
'gatsby-remark-use-jsx',
{
resolve: 'gatsby-remark-prismjs',

1
package.json

@ -85,6 +85,7 @@
},
"devDependencies": {
"eslint-config-prettier": "^2.6.0",
"lz-string": "^1.4.4",
"recursive-readdir": "^2.2.1",
"unist-util-map": "^1.0.3"
}

68
plugins/gatsby-remark-babel-repl-link/index.js

@ -0,0 +1,68 @@
const {existsSync, readFileSync} = require('fs');
const LZString = require('lz-string');
const {join} = require('path');
const map = require('unist-util-map');
const PROTOCOL = 'babel-repl://';
// Matches compression used in Babel REPL
// https://github.com/babel/website/blob/master/js/repl/UriUtils.js
const compress = string =>
LZString.compressToBase64(string)
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
module.exports = ({markdownAST}, {directory}) => {
map(markdownAST, (node, index, parent) => {
if (!directory.endsWith('/')) {
directory += '/';
}
if (node.type === 'link' && node.url.startsWith(PROTOCOL)) {
let filePath = node.url.replace(PROTOCOL, directory);
if (!filePath.endsWith('.js')) {
filePath += '.js';
}
filePath = join(__dirname, '../..', filePath);
// Verify that the specified example file exists.
if (!existsSync(filePath)) {
console.error(
`Invalid Babel REPL link specified; no such file "${filePath}"`,
);
process.exit(1);
}
const code = compress(readFileSync(filePath, 'utf8'));
const href = `https://babeljs.io/repl/#?presets=react&code_lz=${code}`;
const text = node.children[0].value;
const anchorOpenNode = {
type: 'html',
value: `<a href="${href}" target="_blank">`,
};
const textNode = {
type: 'text',
value: text,
};
const anchorCloseNode = {
type: 'html',
value: '</a>',
};
parent.children.splice(
index,
1,
anchorOpenNode,
textNode,
anchorCloseNode,
);
}
// No change
return node;
});
};

4
plugins/gatsby-remark-babel-repl-link/package.json

@ -0,0 +1,4 @@
{
"name": "gatsby-remark-babel-repl-link",
"version": "0.0.1"
}

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

@ -5,7 +5,6 @@ const map = require('unist-util-map');
const CODEPEN_PROTOCOL = 'codepen://';
const DEFAULT_LINK_TEXT = 'Try it on CodePen';
// TODO target="_blank"
module.exports = ({markdownAST}, {directory}) => {
map(markdownAST, (node, index, parent) => {
if (!directory.startsWith('/')) {
@ -22,7 +21,7 @@ module.exports = ({markdownAST}, {directory}) => {
// from: [Try the Hello World example on CodePen](codepen:hello-world)
// to: <a href="/<directory>/hello-world" target="_blank">Try the Hello World example on CodePen</a>
if (node.type === 'link' && node.url.startsWith(CODEPEN_PROTOCOL)) {
const href = node.url.replace(CODEPEN_PROTOCOL, `${directory}`);
const href = node.url.replace(CODEPEN_PROTOCOL, directory);
const text =
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;

4
yarn.lock

@ -6086,6 +6086,10 @@ ltcdr@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ltcdr/-/ltcdr-2.2.1.tgz#5ab87ad1d4c1dab8e8c08bbf037ee0c1902287cf"
lz-string@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
macaddress@^0.2.8:
version "0.2.8"
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"

Loading…
Cancel
Save