Skip to content

Added custom remark transform for Codepen example links #251

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

Merged
merged 5 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions content/docs/components-and-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ReactDOM.render(
);
```

<a href="/codepen/components-and-props/rendering-a-component" target="_blank">Try it on CodePen</a>.
[](codepen://components-and-props/rendering-a-component).

Let's recap what happens in this example:

Expand Down Expand Up @@ -118,7 +118,7 @@ ReactDOM.render(
);
```

<a href="/codepen/components-and-props/composing-components" target="_blank">Try it on CodePen</a>.
[](codepen://components-and-props/composing-components).

Typically, new React apps have a single `App` component at the very top. However, if you integrate React into an existing app, you might start bottom-up with a small component like `Button` and gradually work your way to the top of the view hierarchy.

Expand Down Expand Up @@ -152,7 +152,7 @@ function Comment(props) {
}
```

<a href="/codepen/components-and-props/extracting-components" target="_blank">Try it on CodePen</a>.
[](codepen://components-and-props/extracting-components).

It accepts `author` (an object), `text` (a string), and `date` (a date) as props, and describes a comment on a social media website.

Expand Down Expand Up @@ -231,7 +231,7 @@ function Comment(props) {
}
```

<a href="/codepen/components-and-props/extracting-components-continued" target="_blank">Try it on CodePen</a>.
[](codepen://components-and-props/extracting-components-continued).

Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. A good rule of thumb is that if a part of your UI is used several times (`Button`, `Panel`, `Avatar`), or is complex enough on its own (`App`, `FeedStory`, `Comment`), it is a good candidate to be a reusable component.

Expand Down
2 changes: 1 addition & 1 deletion content/docs/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ redirect_from:
- "docs/getting-started-zh-CN.html"
---

The easiest way to get started with React is to use <a href="/codepen/hello-world" target="_blank">this Hello World example code on CodePen</a>. You don't need to install anything; you can just open it in another tab and follow along as we go through examples. If you'd rather use a local development environment, check out the [Installation](/docs/installation.html) page.
The easiest way to get started with React is to use [this Hello World example code on CodePen](codepen://hello-world). You don't need to install anything; you can just open it in another tab and follow along as we go through examples. If you'd rather use a local development environment, check out the [Installation](/docs/installation.html) page.

The smallest React example looks like this:

Expand Down
2 changes: 1 addition & 1 deletion content/docs/introducing-jsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ReactDOM.render(
);
```

<a href="/codepen/introducing-jsx" target="_blank">Try it on CodePen.</a>
[](codepen://introducing-jsx).

We split JSX over multiple lines for readability. While it isn't required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of [automatic semicolon insertion](http://stackoverflow.com/q/2846283).

Expand Down
1 change: 1 addition & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = {
},
},
'gatsby-remark-autolink-headers',
'gatsby-remark-codepen-examples',
'gatsby-remark-use-jsx',
{
resolve: 'gatsby-remark-prismjs',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
},
"devDependencies": {
"eslint-config-prettier": "^2.6.0",
"recursive-readdir": "^2.2.1"
"recursive-readdir": "^2.2.1",
"unist-util-map": "^1.0.3"
}
}
57 changes: 57 additions & 0 deletions plugins/gatsby-remark-codepen-examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {existsSync} = require('fs');
const {join} = require('path');
const map = require('unist-util-map');

const CODEPEN_PROTOCOL = 'codepen://';
const DEFAULT_LINK_TEXT = 'Try it on CodePen';

// TODO target="_blank"
module.exports = ({markdownAST}) => {
map(markdownAST, (node, index, parent) => {
// eg convert
// from: [](codepen:introducing-jsx)
// to: <a href="/codepen/introducing-jsx" target="_blank">Try it on CodePen</a>
// from: [Try the Hello World example on CodePen](codepen:hello-world)
// to: <a href="/codepen/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, '/codepen/');
const text =
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;

// Verify that the specified example file exists.
const filePath = join(__dirname, `../../${href}.js`);
if (!existsSync(filePath)) {
console.error(
`Invalid Codepen link specified; no such file "${filePath}"`,
);
process.exit(1);
}

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 changes: 4 additions & 0 deletions plugins/gatsby-remark-codepen-examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "gatsby-remark-codepen-examples",
"version": "0.0.1"
}
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9795,6 +9795,12 @@ unist-util-is@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-2.1.1.tgz#0c312629e3f960c66e931e812d3d80e77010947b"

unist-util-map@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/unist-util-map/-/unist-util-map-1.0.3.tgz#26a913d7cddb3cd3e9a886d135d37a3d1f54e514"
dependencies:
object-assign "^4.0.1"

unist-util-modify-children@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/unist-util-modify-children/-/unist-util-modify-children-1.1.1.tgz#66d7e6a449e6f67220b976ab3cb8b5ebac39e51d"
Expand Down