acetate-folder
Advanced tools
Comparing version 0.0.1 to 0.0.2
22
index.js
@@ -1,3 +0,4 @@ | ||
var fs = require('fs'); | ||
var nunjucks = require('nunjucks'); | ||
var glob = require('glob'); | ||
var path = require('path'); | ||
@@ -22,2 +23,3 @@ function NunjucksFolder() { | ||
return new nodes.CallExtension(this, 'run', args, [body]); | ||
}; | ||
@@ -29,12 +31,14 @@ | ||
var files = fs.readdirSync(folder).filter(function (file) { | ||
return fs.statSync(file).isFile() && file.charAt(0) !== '.'; | ||
glob(folder, { | ||
dot: false, | ||
sync: true | ||
}, function(err, files) { | ||
if (err) { throw err } | ||
files.forEach( function(file) { | ||
file = path.basename(file).split('.')[0]; | ||
var sample = html.replace(/FILENAME/g, file); | ||
response += sample; | ||
}); | ||
}); | ||
files.forEach( function(file) { | ||
var filename = file.split('.')[0]; | ||
var sample = html.replace(/FILENAME/g, filename); | ||
response += sample; | ||
}); | ||
return response; | ||
@@ -41,0 +45,0 @@ }; |
{ | ||
"name": "acetate-folder", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Custom nunjucks tag that iterates over all the files in a folder", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,6 +25,6 @@ # Acetate Folder | ||
Now, in your templates, you can use the folder tag: | ||
Now, in your templates, you can use the folder tag, passing it a glob pattern: | ||
``` | ||
{% folder '.' %} | ||
{% folder './*.svg' %} | ||
<div class="FILENAME">FILENAME</div> | ||
@@ -34,7 +34,7 @@ {% endfolder %} | ||
This will iterate over every file from your current folder (relative to where the acetate config is), skipping directories and files that begin with a `.`. | ||
This will iterate over every file from your current folder (relative to where the acetate config is) with an extension of `.svg`. By default the tag doesn't show files that begin with `.`. | ||
The code inside the `{% folder %}` tag will be repeated for each file in the folder. Every occurrence of `FILENAME` will be replaced by the filename of each file. | ||
For example, if you had the following directory located at .`/fruit-pics`: | ||
For example, if you had the following directory located at `/fruit-pics`: | ||
@@ -52,3 +52,3 @@ ``` | ||
``` | ||
{% folder './fruit-pics' %} | ||
{% folder './fruit-pics/*.svg' %} | ||
<div class="FILENAME">FILENAME</div> | ||
@@ -61,20 +61,17 @@ {% endfolder %} | ||
``` | ||
<div class="apples">apples</div> | ||
<div class="bananas">bananas</div> | ||
<div class="apples">apples</div> | ||
<div class="fruit">fruit</div> | ||
``` | ||
These are returned in alphabetical order with filenames that start with a capital letter being before lowercase filenames. | ||
## TODO | ||
I think there are a number of things you could make configurable inside the tag itself, like: | ||
I think there are a number of options you could make configurable inside the tag itself, like: | ||
- include directories | ||
- include files beginning with `.` | ||
- recursive | ||
- specify a sort order for results | ||
Also, there is some work that needs to be done on the script itself to make it more production ready: | ||
- error handling for non-existent file paths | ||
- error handling for problems reading files from filesystem | ||
## Contributing | ||
@@ -81,0 +78,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
4436
40
0
81