mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-25 23:05:23 +00:00
Added custom remark transform for Codepen example links
This commit is contained in:
45
plugins/gatsby-remark-codepen-examples/index.js
Normal file
45
plugins/gatsby-remark-codepen-examples/index.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const map = require('unist-util-map');
|
||||
|
||||
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:')) {
|
||||
const href = node.url.replace('codepen:', '/codepen/');
|
||||
const text =
|
||||
node.children.length === 0 ? DEFAULT_LINK_TEXT : node.children[0].value;
|
||||
|
||||
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
plugins/gatsby-remark-codepen-examples/package.json
Normal file
4
plugins/gatsby-remark-codepen-examples/package.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "gatsby-remark-codepen-examples",
|
||||
"version": "0.0.1"
|
||||
}
|
||||
Reference in New Issue
Block a user