Skip to content

Commit d743b1c

Browse files
committed
gatsby-remark-codepen-examples supports 'directory' option
1 parent aa04afb commit d743b1c

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

gatsby-config.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ module.exports = {
5656
},
5757
},
5858
'gatsby-remark-autolink-headers',
59-
'gatsby-remark-codepen-examples',
59+
{
60+
resolve: 'gatsby-remark-codepen-examples',
61+
options: {
62+
directory: 'examples',
63+
},
64+
},
6065
'gatsby-remark-use-jsx',
6166
{
6267
resolve: 'gatsby-remark-prismjs',

gatsby-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
171171
// Create Codepen redirects.
172172
// These use the Codepen prefill API to JIT-create Pens.
173173
// https://blog.codepen.io/documentation/api/prefill/
174-
const files = await recursiveReaddir('./codepen');
174+
const files = await recursiveReaddir('./examples');
175175
files.forEach(file => {
176176
const slug = file.substring(0, file.length - 3); // Trim extension
177177
const code = readFileSync(file, 'utf8');

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ const CODEPEN_PROTOCOL = 'codepen://';
66
const DEFAULT_LINK_TEXT = 'Try it on CodePen';
77

88
// TODO target="_blank"
9-
module.exports = ({markdownAST}) => {
9+
module.exports = ({markdownAST}, {directory}) => {
1010
map(markdownAST, (node, index, parent) => {
11+
if (!directory.startsWith('/')) {
12+
directory = `/${directory}`;
13+
}
14+
15+
if (!directory.endsWith('/')) {
16+
directory = `${directory}/`;
17+
}
18+
1119
// eg convert
1220
// from: [](codepen:introducing-jsx)
13-
// to: <a href="/codepen/introducing-jsx" target="_blank">Try it on CodePen</a>
21+
// to: <a href="/<directory>/introducing-jsx" target="_blank">Try it on CodePen</a>
1422
// from: [Try the Hello World example on CodePen](codepen:hello-world)
15-
// to: <a href="/codepen/hello-world" target="_blank">Try the Hello World example on CodePen</a>
23+
// to: <a href="/<directory>/hello-world" target="_blank">Try the Hello World example on CodePen</a>
1624
if (node.type === 'link' && node.url.startsWith(CODEPEN_PROTOCOL)) {
17-
const href = node.url.replace(CODEPEN_PROTOCOL, '/codepen/');
25+
const href = node.url.replace(CODEPEN_PROTOCOL, `${directory}`);
1826
const text =
1927
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;
2028

0 commit comments

Comments
 (0)