Skip to content

Commit ce63eb0

Browse files
author
Guy Thomas
committed
allow ignoring of file / folder names
1 parent fac8a4d commit ce63eb0

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ This is than passed to the (awesome) [define api](https://codesandbox.io/docs/im
119119
// Customise the embedding iframe given the generated url
120120
// default:
121121
getIframe: url => `<iframe src="${url}" class="embedded-codesandbox" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`
122+
123+
124+
// Customise the ignored file / folder names
125+
// default:
126+
ignoredFiles: [
127+
'node_modules',
128+
'yarn.lock',
129+
'package-lock.json'
130+
]
122131
}
123132
}
124133
]

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "gatsby-remark-embedded-codesandbox",
3-
"version": "1.2.0",
4-
"description": "Gatsby Remark plugin for embedding Codesandbox given a folder of files",
2+
"name": "@guy/gatsby-remark-embedded-codesandbox",
3+
"version": "1.0.0",
4+
"description": "Gatsby Remark plugin for embedding Codesandbox given a recursive folder of files",
55
"main": "index.js",
6-
"repository": "https://github.com/elboman/gatsby-remark-embedded-codesandbox",
6+
"repository": "https://github.com/guyathomas/gatsby-remark-embedded-codesandbox",
77
"author": "Marco Botto <[email protected]>",
88
"bugs": {
9-
"url": "https://github.com/elboman/gatsby-remark-embedded-codesandbox/issues"
9+
"url": "https://github.com/guyathomas/gatsby-remark-embedded-codesandbox/issues"
1010
},
1111
"keywords": [
1212
"gatsby",

src/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const DEFAULT_EMBED_OPTIONS = {
1313
const DEFAULT_GET_IFRAME = url =>
1414
`<iframe src="${url}" class="embedded-codesandbox" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`;
1515

16+
const DEFAULT_IGNORED_FILES = [
17+
'node_modules',
18+
'package-lock.json',
19+
'yarn.lock'
20+
]
21+
1622
// Matches compression used in Babel and CodeSandbox REPLs
1723
// https://github.com/babel/website/blob/master/js/repl/UriUtils.js
1824
const compress = string =>
@@ -21,14 +27,7 @@ const compress = string =>
2127
.replace(/\//g, `_`) // Convert '/' to '_'
2228
.replace(/=+$/, ``); // Remove ending '='
2329

24-
const getAllFiles = dirPath =>
25-
fs.readdirSync(dirPath).reduce((acc, file) => {
26-
if (file === 'node_modules') return acc;
27-
const relativePath = dirPath + '/' + file;
28-
const isDirectory = fs.statSync(relativePath).isDirectory();
29-
const additions = isDirectory ? getAllFiles(relativePath) : [relativePath];
30-
return [...acc, ...additions];
31-
}, []);
30+
3231

3332
module.exports = (
3433
{ markdownAST },
@@ -37,6 +36,7 @@ module.exports = (
3736
protocol = DEFAULT_PROTOCOL,
3837
embedOptions = DEFAULT_EMBED_OPTIONS,
3938
getIframe = DEFAULT_GET_IFRAME,
39+
ignoredFiles = DEFAULT_IGNORED_FILES,
4040
}
4141
) => {
4242
if (!rootDirectory) {
@@ -47,12 +47,23 @@ module.exports = (
4747
rootDirectory += '/';
4848
}
4949

50+
const ignoredFilesSet = new Set(ignoredFiles)
51+
5052
const getDirectoryPath = url => {
5153
let directoryPath = url.replace(protocol, '');
5254
const fullPath = path.join(rootDirectory, directoryPath);
5355
return normalizePath(fullPath);
5456
};
5557

58+
const getAllFiles = (dirPath) =>
59+
fs.readdirSync(dirPath).reduce((acc, file) => {
60+
if (ignoredFilesSet.has(file)) return acc;
61+
const relativePath = dirPath + '/' + file;
62+
const isDirectory = fs.statSync(relativePath).isDirectory();
63+
const additions = isDirectory ? getAllFiles(relativePath) : [relativePath];
64+
return [...acc, ...additions];
65+
}, []);
66+
5667
const getFilesList = directory => {
5768
let packageJsonFound = false;
5869
const folderFiles = getAllFiles(directory);

0 commit comments

Comments
 (0)