Skip to content

Commit 79c6235

Browse files
committed
feat(options): override embedding options via url query
You can now override the plug-in wide embedding options on a per-sandbox basis by specifying them in the link url query: ```js // gatsby-config.js { resolve: ‘gatsby-remark-embedded-codesandbox’, options: { embedOptions: { view: 'preview', } } } ``` These embedOptions are applied to every sandbox, but you may want to override them in a single sandbox. To do this simply add a url query in the generating link: ```md [code example](embedded-codesandbox://some/example?view=split) ``` The `urlQuery` options will be merged with the `gatsby-config` one.
1 parent ba72d1f commit 79c6235

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/__tests__/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,24 @@ describe('gatsby-remark-embedded-codesandbox', () => {
8080
);
8181
expect(getNodeContent(transformed)).toMatchSnapshot();
8282
});
83+
84+
it('generates an embedded sandbox using the overridable url-query options', () => {
85+
const markdownAST = remark.parse(
86+
`[](embedded-codesandbox://example?view=split&hidenavigation=0)`
87+
);
88+
const transformed = plugin({ markdownAST }, { directory: 'examples' });
89+
expect(getNodeContent(transformed).value).toEqual(
90+
expect.stringContaining('query=hidenavigation%3D0%26view%3Dsplit')
91+
);
92+
});
93+
94+
it('overrides url-query options correctly', () => {
95+
const markdownAST = remark.parse(
96+
`[](embedded-codesandbox://example?hidenavigation=0&foo=bar)`
97+
);
98+
const transformed = plugin({ markdownAST }, { directory: 'examples' });
99+
expect(getNodeContent(transformed).value).toEqual(
100+
expect.stringContaining('foo%3Dbar%26hidenavigation%3D0%26view%3Dpreview')
101+
);
102+
});
83103
});

src/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,24 @@ module.exports = (
7373
return compress(JSON.stringify(params));
7474
};
7575

76-
const convertNodeToEmbedded = (node, params) => {
76+
const getUrlParts = url => {
77+
const splitUrl = url.split('?');
78+
return {
79+
base: splitUrl[0],
80+
query: queryString.parse(splitUrl[1]),
81+
};
82+
};
83+
84+
const convertNodeToEmbedded = (node, params, options = {}) => {
7785
delete node.children;
7886
delete node.position;
7987
delete node.title;
8088
delete node.url;
8189

90+
// merge the overriding options with the plugin one
91+
const mergedOptions = { ...embedOptions, ...options };
8292
const encodedEmbedOptions = encodeURIComponent(
83-
queryString.stringify(embedOptions)
93+
queryString.stringify(mergedOptions)
8494
);
8595
const sandboxUrl = `https://codesandbox.io/api/v1/sandboxes/define?embed=1&parameters=${params}&query=${encodedEmbedOptions}`;
8696
const embedded = getIframe(sandboxUrl);
@@ -91,10 +101,15 @@ module.exports = (
91101

92102
map(markdownAST, (node, index, parent) => {
93103
if (node.type === 'link' && node.url.startsWith(protocol)) {
94-
const dir = getDirectoryPath(node.url);
104+
// split the url in base and query to allow user
105+
// to customise embedding options on a per-node basis
106+
const url = getUrlParts(node.url);
107+
// get all files in the folder and generate
108+
// the embeddeing parameters
109+
const dir = getDirectoryPath(url.base);
95110
const files = getFilesList(dir);
96111
const params = createParams(files);
97-
convertNodeToEmbedded(node, params);
112+
convertNodeToEmbedded(node, params, url.query);
98113
}
99114

100115
return node;

0 commit comments

Comments
 (0)