metalsmith-slug
Advanced tools
Comparing version 0.0.6 to 0.1.0
@@ -9,3 +9,3 @@ var path = require('path'); | ||
return patterns.some(function (pattern) { | ||
return minimatch(file, pattern, { matchBase: true}); | ||
return minimatch(file, pattern, { matchBase: true }); | ||
}); | ||
@@ -20,15 +20,11 @@ } | ||
if (opts.lowercase) { | ||
newSlug = newSlug.toLowerCase(); | ||
} | ||
if (opts.renameFiles) { | ||
var currPath = path.dirname(file); | ||
var currExt = path.extname(file); | ||
var slugFilename = path.join(currPath, currFile.slug + currExt); | ||
var slugFilename = path.join(currPath, newSlug + currExt); | ||
if (slugFilename === file) return; | ||
files[slugFilename] = currFile; | ||
delete files[file]; | ||
if (slugFilename !== file) { | ||
files[slugFilename] = currFile; | ||
delete files[file]; | ||
} | ||
} | ||
@@ -40,16 +36,19 @@ | ||
function plugin (opts) { | ||
// Plugin options | ||
opts = opts || {}; | ||
opts.lower = opts.lower || opts.lowercase; // 'lowercase' for backward compatibility | ||
opts.mode = opts.mode || slug.defaults.mode; | ||
opts.property = opts.property || 'title'; | ||
opts.renameFiles = opts.renameFiles || false; | ||
opts.lowercase = opts.lowercase || false; | ||
var slugOpts = {}; | ||
// Slug options | ||
var slugOpts = {}; | ||
var slugDefaults = slug.defaults.modes[opts.mode]; | ||
Object.keys(slug.defaults).forEach(function (slugOpt) { | ||
slugOpts[slugOpt] = opts[slugOpt] || slug.defaults[slugOpt]; | ||
Object.keys(slugDefaults).forEach(function (slugOpt) { | ||
slugOpts[slugOpt] = opts[slugOpt] || slugDefaults[slugOpt]; | ||
}); | ||
return function (files, metalsmith, done) { | ||
var src = metalsmith.source(); | ||
opts.patterns = opts.patterns || [src + '/**']; | ||
opts.patterns = opts.patterns || ['**']; | ||
@@ -56,0 +55,0 @@ var matchedFiles = Object.keys(files).filter(function (file) { |
{ | ||
"name": "metalsmith-slug", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "slugify a property in metalsmith", | ||
@@ -23,10 +23,10 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"minimatch": "^0.3.0", | ||
"slug": "^0.5.0" | ||
"minimatch": "2.x", | ||
"slug": "0.9.x" | ||
}, | ||
"devDependencies": { | ||
"metalsmith": "^0.10.0", | ||
"tap": "^0.4.12", | ||
"tap-dot": "^0.2.3" | ||
"metalsmith": "1.x", | ||
"tap": "^1.3.1", | ||
"tap-dot": "^1.0.0" | ||
} | ||
} |
# metalsmith slug | ||
Add a slug property to the metadata for targeted files in your | ||
Add a `slug` property to the metadata for targeted files in your | ||
[Metalsmith](http://www.metalsmith.io/), based on a particular property. | ||
Useful for generating links to pages based on, say, their title. | ||
Useful for generating links to pages based on, say, their `title`. | ||
## Installation | ||
npm install metalsmith-slug | ||
npm install metalsmith-slug --save | ||
## Usage | ||
If you're not familiar with Metalsmith, check it out. It's a versatile file | ||
processor that can be used for static site generation, project scaffolding and | ||
more. It can be interacted with via a CLI or JavaScript API. | ||
If you're not familiar with Metalsmith, [check it out](http://metalsmith.io). It's a versatile file processor that can be used for static site generation, | ||
project scaffolding and more. It can be interacted with via a CLI or | ||
JavaScript API. | ||
@@ -41,21 +41,21 @@ ### Metalsmith CLI | ||
**patterns** _[array]_: Glob patterns of files to match. Uses | ||
[minimatch](https://github.com/isaacs/minimatch). | ||
**patterns** `Array`: Glob patterns of files to match. Uses | ||
[minimatch](https://github.com/isaacs/minimatch). Default: `[]`. | ||
```js | ||
metalsmith.use(slug({ | ||
patterns: ['*.md', '*.rst'] // Deafults to metalsmith.source() directory | ||
patterns: ['*.md', '*.rst'] // Deafults to all files | ||
})); | ||
``` | ||
**property** _'string'_: Property to generate the slug from. | ||
**property** `String`: Property to generate the slug from. Default: `title`. | ||
```js | ||
metalsmith.use(slug({ | ||
property: 'name' // Defaults to 'title' | ||
property: 'name' | ||
})); | ||
``` | ||
**renameFiles** _boolean_: When set to `true`, will rename the files passed to | ||
metalsmith-slug to the file's new slug property. | ||
**renameFiles** `Boolean`: When set to `true`, will rename the files passed to | ||
metalsmith-slug to the file's new slug property. Default: `false`. | ||
@@ -68,11 +68,24 @@ ```js | ||
**slug options**: You can additionally use any of the options available to [node-slug](https://github.com/dodo/node-slug#options) | ||
**slug options**: You can additionally use any of the options available for [node-slug](https://github.com/dodo/node-slug#options). | ||
```js | ||
// This are the defaults for node-slug 'pretty' mode | ||
metalsmith.use(slug({ | ||
replacement: '_', // Defaults to node-slug defaults | ||
symbols: false | ||
replacement: '-', | ||
symbols: true, | ||
remove: /[.]/g, | ||
lower: false, | ||
charmap: [object Object], // slug.defaults.charmap | ||
multicharmap: [object Object], // slug.defaults.multicharmap | ||
})); | ||
``` | ||
Alternatively you can change the default node-slug mode: | ||
``` | ||
metalsmith.use(slug({ | ||
mode: 'rfc3986' | ||
})); | ||
``` | ||
## Tests | ||
@@ -79,0 +92,0 @@ |
@@ -6,31 +6,116 @@ var test = require('tap').test; | ||
var testProperty = 'title'; | ||
test('it should work with default options', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug()) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'A-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
test('it should specified property of metalsmith files to a slug', function (t) { | ||
t.plan(4); | ||
test('it should sluggify the specified property', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(function (files, ms, done) { | ||
for (var file in files) { | ||
files[file].name = files[file].title; | ||
} | ||
var metalsmith = Metalsmith('test/fixtures/basic'); | ||
done(); | ||
}) | ||
.use(metalsmithSlug({ | ||
property: 'name' | ||
})) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'A-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
metalsmith | ||
test('it should match the given patterns', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug({ | ||
patterns: ['*.md'], | ||
property: testProperty | ||
patterns: ['test-s*.md'] | ||
})) | ||
.build(function (err, files) { | ||
if (err) { | ||
console.error(err); t.end(); | ||
} | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'A-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': undefined, | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
Object.keys(files).forEach(function (file) { | ||
var currFile = files[file]; | ||
if (currFile[testProperty]) { | ||
t.equal(currFile.slug, slug(currFile.title), currFile.testName); | ||
} else { | ||
t.equal(currFile.slug, undefined, currFile.testName); | ||
} | ||
}); | ||
test('it should rename the files', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug({ renameFiles: true })) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'A-simple-string-of-characters.md': 'A-simple-string-of-characters', | ||
'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', | ||
'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
t.end(); | ||
}); | ||
test('it should work with "lowercase" option (backward compatibility)', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug({ lowercase: true })) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'a-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeoesumrdftmooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
test('it should take slug options', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug({ lower: true })) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'a-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeoesumrdftmooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
test('it should change the slug mode', function (t) { | ||
Metalsmith('test/fixtures/basic') | ||
.use(metalsmithSlug({ mode: 'rfc3986' })) | ||
.use(testFiles(t, { | ||
'test-noproperty.md': undefined, | ||
'test-simple.md': 'a-simple-string-of-characters', | ||
'test-symbols.md': 'ccoeoesumrdftm...ooadeltainfinityloveandorlessgreater', | ||
'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', | ||
})) | ||
.build(testDone(t)); | ||
}); | ||
function testFiles(t, tests) { | ||
return function (files, ms, done) { | ||
Object.keys(files).forEach(function (file) { | ||
var currFile = files[file]; | ||
if (file in tests) { | ||
t.equal(currFile.slug, tests[file], currFile.testName); | ||
} | ||
}); | ||
done(); | ||
}; | ||
} | ||
function testDone(t) { | ||
return function (err) { | ||
if (err) { console.error(err); } | ||
t.end(); | ||
}; | ||
} |
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
10560
157
105
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedminimatch@2.0.10(transitive)
+ Addedslug@0.9.4(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedslug@0.5.0(transitive)
Updatedminimatch@2.x
Updatedslug@0.9.x