metalsmith-slug
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var slug = require('slug'); | ||
@@ -12,5 +13,21 @@ var minimatch = require('minimatch'); | ||
function saveSlug (files, file, property, slugOpts) { | ||
var currFile = files[file]; | ||
currFile.slug = slug(currFile[property], slugOpts); | ||
} | ||
function renameKey (files, file) { | ||
var currFile = files[file]; | ||
var currPath = path.dirname(file); | ||
var currExt = path.extname(file); | ||
var slugFilename = path.join(currPath, currFile.slug + currExt); | ||
files[slugFilename] = currFile; | ||
delete files[file]; | ||
} | ||
function plugin (opts) { | ||
opts = opts || {}; | ||
opts.property = opts.property || 'title'; | ||
opts.renameFiles = opts.renameFiles || false; | ||
@@ -30,6 +47,12 @@ var slugOpts = {}; | ||
matchedFiles.forEach(function (file) { | ||
var currFile = files[file]; | ||
currFile.slug = slug(currFile[opts.property], slugOpts); | ||
}); | ||
if (opts.renameFiles) { | ||
matchedFiles.forEach(function (file) { | ||
saveSlug(files, file, opts.property, slugOpts); | ||
renameKey(files, file); | ||
}); | ||
} else { | ||
matchedFiles.forEach(function (file) { | ||
saveSlug(files, file, opts.property, slugOpts); | ||
}); | ||
} | ||
@@ -36,0 +59,0 @@ done(); |
{ | ||
"name": "metalsmith-slug", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "slugify a property in metalsmith", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -58,2 +58,11 @@ # metalsmith slug | ||
**renameFiles** _boolean_: When set to `true`, will rename the files passed to | ||
metalsmith-slug to the file's new slug property. | ||
```js | ||
metalsmith.use(slug({ | ||
renameFiles: true | ||
})); | ||
``` | ||
**slug options**: You can additionally use any of the options available to [node-slug](https://github.com/dodo/node-slug#options) | ||
@@ -67,1 +76,2 @@ | ||
``` | ||
3730
48
76