From d743b1c6ed2cb2a2ebaece5080c65590a4d8bb67 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 7 Nov 2017 11:03:49 +0000 Subject: [PATCH] gatsby-remark-codepen-examples supports 'directory' option --- gatsby-config.js | 7 ++++++- gatsby-node.js | 2 +- plugins/gatsby-remark-codepen-examples/index.js | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index 60be836c..a645f86a 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -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', diff --git a/gatsby-node.js b/gatsby-node.js index 359be801..ca812c78 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -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'); diff --git a/plugins/gatsby-remark-codepen-examples/index.js b/plugins/gatsby-remark-codepen-examples/index.js index d3872849..f1da51e4 100644 --- a/plugins/gatsby-remark-codepen-examples/index.js +++ b/plugins/gatsby-remark-codepen-examples/index.js @@ -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: Try it on CodePen + // to: Try it on CodePen // from: [Try the Hello World example on CodePen](codepen:hello-world) - // to: Try the Hello World example on CodePen + // to: Try the Hello World example on CodePen 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;