Skip to content

Commit

Permalink
gatsby-remark-codepen-examples supports 'directory' option
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Nov 7, 2017
1 parent aa04afb commit d743b1c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 6 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ module.exports = {
},
},
'gatsby-remark-autolink-headers',
'gatsby-remark-codepen-examples',
{
resolve: 'gatsby-remark-codepen-examples',
options: {
directory: 'examples',
},
},
'gatsby-remark-use-jsx',
{
resolve: 'gatsby-remark-prismjs',
Expand Down
2 changes: 1 addition & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
// Create Codepen redirects.
// These use the Codepen prefill API to JIT-create Pens.
// https://blog.codepen.io/documentation/api/prefill/
const files = await recursiveReaddir('./codepen');
const files = await recursiveReaddir('./examples');
files.forEach(file => {
const slug = file.substring(0, file.length - 3); // Trim extension
const code = readFileSync(file, 'utf8');
Expand Down
16 changes: 12 additions & 4 deletions plugins/gatsby-remark-codepen-examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ const CODEPEN_PROTOCOL = 'codepen://';
const DEFAULT_LINK_TEXT = 'Try it on CodePen';

// TODO target="_blank"
module.exports = ({markdownAST}) => {
module.exports = ({markdownAST}, {directory}) => {
map(markdownAST, (node, index, parent) => {
if (!directory.startsWith('/')) {
directory = `/${directory}`;
}

if (!directory.endsWith('/')) {
directory = `${directory}/`;
}

// eg convert
// from: [](codepen:introducing-jsx)
// to: <a href="/codepen/introducing-jsx" target="_blank">Try it on CodePen</a>
// to: <a href="/<directory>/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>
// to: <a href="/<directory>/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 href = node.url.replace(CODEPEN_PROTOCOL, `${directory}`);
const text =
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;

Expand Down

0 comments on commit d743b1c

Please sign in to comment.