An open API service indexing awesome lists of open source software.

https://github.com/featurist/codesandbox-example-links

Generates "run this example" links for your readme.md
https://github.com/featurist/codesandbox-example-links

codesandbox javascript

Last synced: 5 months ago
JSON representation

Generates "run this example" links for your readme.md

Awesome Lists containing this project

README

        

[Codesandbox](https://codesandbox.io) Example Links [![Codeship Status for featurist/codesandbox-example-links](https://app.codeship.com/projects/6176c9c0-20f5-0137-6e2c-62b911cd023e/status?branch=master)](https://app.codeship.com/projects/329497)
-----------

A CLI/API to replace magic placeholders in a markdown files with:

- `iframe` with a codesandbox project
- "Run this example" link to a codesandbox project
- code snippet extracted from a file on a file system

## Example

Given the following project structure:
```
.
├── demo-project
│   ├── index.html
│   ├── package.json
│   └── src
│   ├── app.jsx
│   └── index.js
├── docs
│   └── README.md
```

Where `docs/README.md` looks like this:

```markdown
## An Example

[view code](/demo-project/src/index.js)

[codesandbox](/demo-project)

```

Then running:

```sh
npx codesandbox-example-links --output-dir=. ./docs/README.md
```

Will produce a `./README.md` with the following contents:

## An Example

```js
import * as hyperdom from "hyperdom";
import App from "./app";

hyperdom.append(document.body, new App());

```

Run this example

### Real world example

https://hyperdom.org

## Usage

- manually create sandbox for your example
- download it into your project
- insert magic links in markdown files
- repeat for all examples
- `npx codesandbox-example-links --output-dir=./dist docs/*.md`

The above command renders links. Add `--iframe` option to generate iframes instead.

### Magic links

`[codesandbox](/demo-project)` - turns into either link or an iframe, depending on `--iframe` option.

`[codesandbox](/demo-project?module=/path/to/file)` - as above, but opens the project on a specific file. Other [codesanbdox embed options](https://codesandbox.io/docs/embedding#embed-options) can be specified too.

`[view code](/path/to/file.js)` - turns into a code snippet containing file.js.

`[view code](/path/to/file.jsL#3)` - turns into a code snippet containing a fragment of file.js starting at line 3 onwards.

`[view code](/path/to/file.jsL#3-L10)` - turns into a code snippet containing a fragment of file.js, lines 3 to 10.

### API

```js
const generateLinks = require('codesandbox-example-links')

const input = fs.readFileSync('readme.md', {encoding: 'utf-8'})
const output = generateLinks(input, {iframe: true})
```

### Debug

As of this writing, codesandbox api is sort of short on validation errors. So if generated links don't work, you might find some additional debug info useful. Set `DEBUG=codesandbox-example-links` to get some.