-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support multiple files for codesandbox import #5619
Merged
KyleAMathews
merged 7 commits into
gatsbyjs:master
from
cyan33:support-multiple-files-codesandbox
Jul 25, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
819f419
support multiple files for codesandbox import
cyan33 5596540
add new test cases and refactor code
cyan33 7197cd8
fix the nits
cyan33 87be5f1
update document about supporting multiple files
cyan33 96b0449
replace indexOf with includes
cyan33 62702c2
ran prettier
cyan33 4d1a232
CodesandBox -> CodeSandbox
cyan33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,30 +42,59 @@ to HTML links that open the embedded code examples in a REPL. For example: | |
|
||
```html | ||
<!-- before --> | ||
[See it in Babel](babel://hello-world) | ||
[See it in Babel](babel://hello-world.js) | ||
|
||
<!-- after --> | ||
<a href="https://babeljs.io/repl/#?presets=react&code_lz=..."> | ||
See it in Babel | ||
</a> | ||
|
||
<!-- before --> | ||
[Try it on CodePen](codepen://components-and-props/rendering-a-component) | ||
[Try it on CodePen](codepen://components-and-props/rendering-a-component.js) | ||
|
||
<!-- after --> | ||
<a href="/redirect-to-codepen/components-and-props/rendering-a-component"> | ||
Try it on CodePen | ||
</a> | ||
|
||
<!-- before --> | ||
[Try it on CodeSandbox](codesandbox://components-and-props/rendering-a-component) | ||
[Try it on CodeSandbox](codesandbox://components-and-props/rendering-a-component.js) | ||
|
||
<!-- after --> | ||
<a href="https://codesandbox.io/api/v1/sandboxes/define?parameters=..."> | ||
Try it on CodeSandbox | ||
</a> | ||
``` | ||
|
||
### Creating CodeSandbox Example With Multiple Files | ||
|
||
Sometimes a larger code example would require more than a single file, with various types. For example, you might have an example folder like this: | ||
|
||
``` | ||
├── my-example | ||
│ ├── index.js | ||
│ ├── util.js | ||
│ └── index.css | ||
``` | ||
|
||
CodeSandbox supports code example with multiple files. With this plugin, you can do: | ||
|
||
```html | ||
[Try it on CodeSandbox](codesandbox://my-example/index.js,my-example/util.js,my-example/index.css) | ||
``` | ||
|
||
> Caveat | ||
> | ||
> The first file path you passed to `codesandbox://` will be the entry of your example, that is, the `main` field specified in your `package.json`. | ||
|
||
And in `index.js`, you could import other files using the ES6 modules syntax: | ||
|
||
```js | ||
import { foo } from "./utils" | ||
|
||
import "./index.css" | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice overview. |
||
### How does it work? | ||
|
||
Codepen links point to Gatsby pages (also created by this plug-in) that redirect | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,25 +57,52 @@ module.exports = ( | |
|
||
const getFilePath = (url, protocol, directory) => { | ||
let filePath = url.replace(protocol, ``) | ||
if (!filePath.endsWith(`.js`)) { | ||
if (!filePath.includes(`.`)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for making this change! It was nagging me 😁 |
||
filePath += `.js` | ||
} | ||
filePath = normalizePath(join(directory, filePath)) | ||
return filePath | ||
} | ||
|
||
const verifyFile = path => { | ||
const getMultipleFilesPaths = (urls, protocol, directory) => | ||
urls | ||
.replace(protocol, ``) | ||
.split(`,`) | ||
.map(url => { | ||
if (!url.includes(`.`)) { | ||
url += `.js` | ||
} | ||
|
||
return { | ||
url, // filename itself | ||
filePath: normalizePath(join(directory, url)), // absolute path | ||
} | ||
}) | ||
|
||
const verifyFile = (path, protocol) => { | ||
if (protocol !== PROTOCOL_CODE_SANDBOX && path.split(`,`).length > 1) { | ||
throw Error( | ||
`Code example path should only contain a single file, but found more than one: ${path.replace( | ||
directory, | ||
`` | ||
)}. ` + | ||
`Only CodeSandbox REPL supports multiple files entries, the protocol prefix of which starts with ${PROTOCOL_CODE_SANDBOX}` | ||
) | ||
} | ||
if (!fs.existsSync(path)) { | ||
throw Error(`Invalid REPL link specified; no such file "${path}"`) | ||
} | ||
} | ||
|
||
const verifyMultipleFiles = (paths, protocol) => | ||
paths.forEach(path => verifyFile(path.filePath, protocol)) | ||
|
||
map(markdownAST, (node, index, parent) => { | ||
if (node.type === `link`) { | ||
if (node.url.startsWith(PROTOCOL_BABEL)) { | ||
const filePath = getFilePath(node.url, PROTOCOL_BABEL, directory) | ||
|
||
verifyFile(filePath) | ||
verifyFile(filePath, PROTOCOL_BABEL) | ||
|
||
const code = compress(fs.readFileSync(filePath, `utf8`)) | ||
const href = `https://babeljs.io/repl/#?presets=react&code_lz=${code}` | ||
|
@@ -86,19 +113,20 @@ module.exports = ( | |
} else if (node.url.startsWith(PROTOCOL_CODEPEN)) { | ||
const filePath = getFilePath(node.url, PROTOCOL_CODEPEN, directory) | ||
|
||
verifyFile(filePath) | ||
verifyFile(filePath, PROTOCOL_CODEPEN) | ||
|
||
const href = node.url.replace(PROTOCOL_CODEPEN, `/redirect-to-codepen/`) | ||
const text = | ||
node.children.length === 0 ? defaultText : node.children[0].value | ||
|
||
convertNodeToLink(node, text, href, target) | ||
} else if (node.url.startsWith(PROTOCOL_CODE_SANDBOX)) { | ||
const filePath = getFilePath(node.url, PROTOCOL_CODE_SANDBOX, directory) | ||
|
||
verifyFile(filePath) | ||
|
||
const code = fs.readFileSync(filePath, `utf8`) | ||
const filesPaths = getMultipleFilesPaths( | ||
node.url, | ||
PROTOCOL_CODE_SANDBOX, | ||
directory | ||
) | ||
verifyMultipleFiles(filesPaths, PROTOCOL_CODE_SANDBOX) | ||
|
||
// CodeSandbox GET API requires a list of "files" keyed by name | ||
let parameters = { | ||
|
@@ -114,17 +142,22 @@ module.exports = ( | |
} | ||
return map | ||
}, {}), | ||
main: filesPaths[0].url, | ||
}, | ||
}, | ||
"index.js": { | ||
content: code, | ||
}, | ||
"index.html": { | ||
content: html, | ||
}, | ||
}, | ||
} | ||
|
||
filesPaths.forEach((path, i) => { | ||
const code = fs.readFileSync(path.filePath, `utf8`) | ||
parameters.files[path.url] = { | ||
content: code, | ||
} | ||
}) | ||
|
||
// This config JSON must then be lz-string compressed | ||
parameters = compress(JSON.stringify(parameters)) | ||
|
||
|
@@ -136,7 +169,7 @@ module.exports = ( | |
} else if (node.url.startsWith(PROTOCOL_RAMDA)) { | ||
const filePath = getFilePath(node.url, PROTOCOL_RAMDA, directory) | ||
|
||
verifyFile(filePath) | ||
verifyFile(filePath, PROTOCOL_RAMDA) | ||
|
||
// Don't use `compress()` as the Ramda REPL won't understand the output. | ||
// It uses URI to encode the code for its urls, so we do the same. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's tell people to add explicit file extensions now that we support multiple types of files, even if we have a fallback mechanism for JavaScript files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good suggestion.