diff --git a/content/docs/hello-world.md b/content/docs/hello-world.md index adf4db48..465eb981 100644 --- a/content/docs/hello-world.md +++ b/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 Babel REPL 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. diff --git a/content/docs/jsx-in-depth.md b/content/docs/jsx-in-depth.md index 9837958d..3ba88e39 100644 --- a/content/docs/jsx-in-depth.md +++ b/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 diff --git a/content/docs/react-without-jsx.md b/content/docs/react-without-jsx.md index e1439009..2c1f0d14 100644 --- a/content/docs/react-without-jsx.md +++ b/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. diff --git a/content/home/examples/a-simple-component.md b/content/home/examples/a-simple-component.md index 84897192..1157ef2b 100644 --- a/content/home/examples/a-simple-component.md +++ b/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. diff --git a/content/tutorial/tutorial.md b/content/tutorial/tutorial.md index 4fcf1a93..5dcd5b0e 100644 --- a/content/tutorial/tutorial.md +++ b/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 Babel REPL 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. diff --git a/examples/es5-syntax-example.js b/examples/es5-syntax-example.js new file mode 100644 index 00000000..6d1e2033 --- /dev/null +++ b/examples/es5-syntax-example.js @@ -0,0 +1,3 @@ +const element =

Hello, world!

; +const container = document.getElementById('root'); +ReactDOM.render(element, container); \ No newline at end of file diff --git a/examples/jsx-simple-example.js b/examples/jsx-simple-example.js new file mode 100644 index 00000000..af770ebc --- /dev/null +++ b/examples/jsx-simple-example.js @@ -0,0 +1,3 @@ +function hello() { + return
Hello world!
; +} \ No newline at end of file diff --git a/examples/tutorial-expanded-version.js b/examples/tutorial-expanded-version.js new file mode 100644 index 00000000..767a233f --- /dev/null +++ b/examples/tutorial-expanded-version.js @@ -0,0 +1,8 @@ +
+

Shopping List for {props.name}

+ +
\ No newline at end of file diff --git a/gatsby-config.js b/gatsby-config.js index a645f86a..35a5bf44 100644 --- a/gatsby-config.js +++ b/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', diff --git a/package.json b/package.json index 8501d1de..e888cb43 100644 --- a/package.json +++ b/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" } diff --git a/plugins/gatsby-remark-babel-repl-link/index.js b/plugins/gatsby-remark-babel-repl-link/index.js new file mode 100644 index 00000000..e3f13e20 --- /dev/null +++ b/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: ``, + }; + + const textNode = { + type: 'text', + value: text, + }; + + const anchorCloseNode = { + type: 'html', + value: '', + }; + + parent.children.splice( + index, + 1, + anchorOpenNode, + textNode, + anchorCloseNode, + ); + } + + // No change + return node; + }); +}; diff --git a/plugins/gatsby-remark-babel-repl-link/package.json b/plugins/gatsby-remark-babel-repl-link/package.json new file mode 100644 index 00000000..a0318cc5 --- /dev/null +++ b/plugins/gatsby-remark-babel-repl-link/package.json @@ -0,0 +1,4 @@ +{ + "name": "gatsby-remark-babel-repl-link", + "version": "0.0.1" +} \ No newline at end of file diff --git a/plugins/gatsby-remark-codepen-examples/index.js b/plugins/gatsby-remark-codepen-examples/index.js index f1da51e4..7e4ff332 100644 --- a/plugins/gatsby-remark-codepen-examples/index.js +++ b/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: Try the Hello World example on CodePen 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; diff --git a/yarn.lock b/yarn.lock index 9640d346..abe90f6b 100644 --- a/yarn.lock +++ b/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"