├── .gitignore ├── README.md ├── lib └── index.js ├── package.json └── test ├── fixtures └── basic │ ├── build │ ├── test-manual.md │ ├── test-noproperty.md │ ├── test-simple.md │ ├── test-symbols.md │ └── test-unicode.md │ └── src │ ├── test-manual.md │ ├── test-noproperty.md │ ├── test-simple.md │ ├── test-symbols.md │ └── test-unicode.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # metalsmith slug 2 | 3 | Add a `slug` property to the metadata for targeted files in your 4 | [Metalsmith](http://www.metalsmith.io/), based on a particular property. 5 | Useful for generating links to pages based on, say, their `title`. 6 | 7 | ## Installation 8 | 9 | npm install metalsmith-slug --save 10 | 11 | ## Usage 12 | 13 | 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, 14 | project scaffolding and more. It can be interacted with via a CLI or 15 | JavaScript API. 16 | 17 | ### Metalsmith CLI 18 | 19 | Add `metalsmith-slug` to your `metalsmith.json`. 20 | 21 | ```json 22 | { 23 | "plugins": { 24 | "metalsmith-slug": {} 25 | } 26 | } 27 | ``` 28 | 29 | ### Metalsmith JavaScript API 30 | 31 | Add `metalsmith-slug` to your project and `.use()` it. 32 | 33 | ```js 34 | var slug = require('metalsmith-slug'); 35 | 36 | metalsmith.use(slug()); 37 | ``` 38 | 39 | ## Options 40 | 41 | **patterns** `Array`: Glob patterns of files to match. Uses 42 | [minimatch](https://github.com/isaacs/minimatch). Default: `[]`. 43 | 44 | ```js 45 | metalsmith.use(slug({ 46 | patterns: ['*.md', '*.rst'] // Defaults to all files 47 | })); 48 | ``` 49 | 50 | **property** `String`: Property to generate the slug from. Default: `title`. 51 | 52 | ```js 53 | metalsmith.use(slug({ 54 | property: 'name' 55 | })); 56 | ``` 57 | 58 | **renameFiles** `Boolean`: When set to `true`, will rename the files passed to 59 | metalsmith-slug to the file's new slug property. Default: `false`. 60 | 61 | ```js 62 | metalsmith.use(slug({ 63 | renameFiles: true 64 | })); 65 | ``` 66 | 67 | **slug options**: You can additionally use any of the options available for [node-slug][node-slug-options]. 68 | 69 | ```js 70 | // This are the defaults for node-slug 'pretty' mode 71 | metalsmith.use(slug({ 72 | replacement: '-', 73 | symbols: true, 74 | remove: /[.]/g, 75 | lower: false, 76 | charmap: [object Object], // slug.defaults.charmap 77 | multicharmap: [object Object], // slug.defaults.multicharmap 78 | })); 79 | ``` 80 | 81 | Alternatively you can change the default node-slug mode: 82 | 83 | ``` 84 | metalsmith.use(slug({ 85 | mode: 'rfc3986' 86 | })); 87 | ``` 88 | 89 | ## Manual override 90 | 91 | Also, it's possible to override the `slug` property in a single file. 92 | If you define a `slug` in the front matter, the plugin won't replace it with a new one. 93 | However, it will apply the same [slug options][node-slug-options], if any. 94 | 95 | ```yaml 96 | --- 97 | title: Something 98 | slug: Something-Else 99 | --- 100 | contents... 101 | ``` 102 | 103 | ```js 104 | metalsmith.use(slug()); // => {slug: 'Something-Else', ...} 105 | metalsmith.use(slug({lower: true})); // => {slug: 'something-else', ...} 106 | ``` 107 | 108 | ## Tests 109 | 110 | `npm test` to run the tests. 111 | 112 | ## License 113 | 114 | The MIT License (MIT) 115 | 116 | Copyright (c) 2014 Nikhil Sonnad 117 | 118 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 119 | 120 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 121 | 122 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 123 | 124 | 125 | [node-slug-options]: https://github.com/dodo/node-slug#options 126 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var slug = require('slug'); 3 | var minimatch = require('minimatch'); 4 | 5 | module.exports = plugin; 6 | 7 | function matchGlobArray (file, patterns) { 8 | return patterns.some(function (pattern) { 9 | return minimatch(file, pattern, { matchBase: true }); 10 | }); 11 | } 12 | 13 | function saveSlug (files, file, opts, slugOpts) { 14 | var currFile = files[file]; 15 | var str = currFile.slug || currFile[opts.property]; 16 | if (str === undefined || str === null) return; 17 | 18 | var newSlug = slug(str, slugOpts); 19 | 20 | if (opts.renameFiles) { 21 | var currPath = path.dirname(file); 22 | var currExt = path.extname(file); 23 | var slugFilename = path.join(currPath, newSlug + currExt); 24 | 25 | if (slugFilename !== file) { 26 | files[slugFilename] = currFile; 27 | delete files[file]; 28 | } 29 | } 30 | 31 | currFile.slug = newSlug; 32 | } 33 | 34 | function plugin (opts) { 35 | // Plugin options 36 | opts = opts || {}; 37 | opts.lower = opts.lower || opts.lowercase; // 'lowercase' for backward compatibility 38 | opts.mode = opts.mode || slug.defaults.mode; 39 | opts.property = opts.property || 'title'; 40 | opts.renameFiles = opts.renameFiles || false; 41 | 42 | // Slug options 43 | var slugOpts = {}; 44 | var slugDefaults = slug.defaults.modes[opts.mode]; 45 | 46 | Object.keys(slugDefaults).forEach(function (slugOpt) { 47 | slugOpts[slugOpt] = opts[slugOpt] || slugDefaults[slugOpt]; 48 | }); 49 | 50 | return function (files, metalsmith, done) { 51 | opts.patterns = opts.patterns || ['**']; 52 | 53 | var matchedFiles = Object.keys(files).filter(function (file) { 54 | return matchGlobArray(file, opts.patterns); 55 | }); 56 | 57 | matchedFiles.forEach(function (file) { 58 | saveSlug(files, file, opts, slugOpts); 59 | }); 60 | 61 | done(); 62 | }; 63 | } 64 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "metalsmith-slug", 3 | "version": "0.2.1", 4 | "description": "slugify a property in metalsmith", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "node ./test/index.js | tap-dot" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/nsonnad/metalsmith-slug" 12 | }, 13 | "keywords": [ 14 | "metalsmith" 15 | ], 16 | "author": "Nikhil Sonnad", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/nsonnad/metalsmith-slug/issues" 20 | }, 21 | "homepage": "https://github.com/nsonnad/metalsmith-slug", 22 | "dependencies": { 23 | "minimatch": "2.x", 24 | "slug": "0.9.x" 25 | }, 26 | "devDependencies": { 27 | "metalsmith": "1.x", 28 | "tap": "^1.3.1", 29 | "tap-dot": "^1.0.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/fixtures/basic/build/test-manual.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stephanmax/metalsmith-slug/1250bb9c420c96966abb62c0ce62485cff06d1ad/test/fixtures/basic/build/test-manual.md -------------------------------------------------------------------------------- /test/fixtures/basic/build/test-noproperty.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stephanmax/metalsmith-slug/1250bb9c420c96966abb62c0ce62485cff06d1ad/test/fixtures/basic/build/test-noproperty.md -------------------------------------------------------------------------------- /test/fixtures/basic/build/test-simple.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stephanmax/metalsmith-slug/1250bb9c420c96966abb62c0ce62485cff06d1ad/test/fixtures/basic/build/test-simple.md -------------------------------------------------------------------------------- /test/fixtures/basic/build/test-symbols.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/fixtures/basic/build/test-unicode.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stephanmax/metalsmith-slug/1250bb9c420c96966abb62c0ce62485cff06d1ad/test/fixtures/basic/build/test-unicode.md -------------------------------------------------------------------------------- /test/fixtures/basic/src/test-manual.md: -------------------------------------------------------------------------------- 1 | --- 2 | testName: manual 3 | title: Something in the way she moves 4 | slug: Something-From-George 5 | --- 6 | -------------------------------------------------------------------------------- /test/fixtures/basic/src/test-noproperty.md: -------------------------------------------------------------------------------- 1 | --- 2 | testName: file with no property 3 | --- 4 | -------------------------------------------------------------------------------- /test/fixtures/basic/src/test-simple.md: -------------------------------------------------------------------------------- 1 | --- 2 | testName: simple 3 | title: A simple string of characters! 4 | --- 5 | -------------------------------------------------------------------------------- /test/fixtures/basic/src/test-symbols.md: -------------------------------------------------------------------------------- 1 | --- 2 | testName: symbols 3 | title: ©cœŒ∑®∂ƒ™…˚ºª∆∞♥&|<> 4 | --- 5 | 6 | -------------------------------------------------------------------------------- /test/fixtures/basic/src/test-unicode.md: -------------------------------------------------------------------------------- 1 | --- 2 | testName: unicode 3 | title: ☢☠☤☣☭☯☮☏☔'☎ ☀★☂☃✈✉✊' 4 | --- 5 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var test = require('tap').test; 2 | var Metalsmith = require('metalsmith'); 3 | var metalsmithSlug = require('..'); 4 | var slug = require('slug'); 5 | 6 | test('it should work with default options', function (t) { 7 | Metalsmith('test/fixtures/basic') 8 | .use(metalsmithSlug()) 9 | .use(testFiles(t, { 10 | 'test-noproperty.md': undefined, 11 | 'test-simple.md': 'A-simple-string-of-characters', 12 | 'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', 13 | 'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', 14 | })) 15 | .build(testDone(t)); 16 | }); 17 | 18 | test('it should sluggify the specified property', function (t) { 19 | Metalsmith('test/fixtures/basic') 20 | .use(function (files, ms, done) { 21 | for (var file in files) { 22 | files[file].name = files[file].title; 23 | } 24 | 25 | done(); 26 | }) 27 | .use(metalsmithSlug({ 28 | property: 'name' 29 | })) 30 | .use(testFiles(t, { 31 | 'test-noproperty.md': undefined, 32 | 'test-simple.md': 'A-simple-string-of-characters', 33 | 'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', 34 | 'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', 35 | })) 36 | .build(testDone(t)); 37 | }); 38 | 39 | test('it should match the given patterns', function (t) { 40 | Metalsmith('test/fixtures/basic') 41 | .use(metalsmithSlug({ 42 | patterns: ['test-s*.md'] 43 | })) 44 | .use(testFiles(t, { 45 | 'test-noproperty.md': undefined, 46 | 'test-simple.md': 'A-simple-string-of-characters', 47 | 'test-symbols.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', 48 | 'test-unicode.md': undefined, 49 | })) 50 | .build(testDone(t)); 51 | }); 52 | 53 | test('it should rename the files', function (t) { 54 | Metalsmith('test/fixtures/basic') 55 | .use(metalsmithSlug({ renameFiles: true })) 56 | .use(testFiles(t, { 57 | 'test-noproperty.md': undefined, 58 | 'A-simple-string-of-characters.md': 'A-simple-string-of-characters', 59 | 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater.md': 'ccoeOEsumrdftmooadeltainfinityloveandorlessgreater', 60 | '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', 61 | })) 62 | .build(testDone(t)); 63 | }); 64 | 65 | test('it should work with "lowercase" option (backward compatibility)', function (t) { 66 | Metalsmith('test/fixtures/basic') 67 | .use(metalsmithSlug({ lowercase: true })) 68 | .use(testFiles(t, { 69 | 'test-noproperty.md': undefined, 70 | 'test-simple.md': 'a-simple-string-of-characters', 71 | 'test-symbols.md': 'ccoeoesumrdftmooadeltainfinityloveandorlessgreater', 72 | 'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', 73 | })) 74 | .build(testDone(t)); 75 | }); 76 | 77 | test('it should take slug options', function (t) { 78 | Metalsmith('test/fixtures/basic') 79 | .use(metalsmithSlug({ lower: true })) 80 | .use(testFiles(t, { 81 | 'test-noproperty.md': undefined, 82 | 'test-simple.md': 'a-simple-string-of-characters', 83 | 'test-symbols.md': 'ccoeoesumrdftmooadeltainfinityloveandorlessgreater', 84 | 'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', 85 | })) 86 | .build(testDone(t)); 87 | }); 88 | 89 | test('it should change the slug mode', function (t) { 90 | Metalsmith('test/fixtures/basic') 91 | .use(metalsmithSlug({ mode: 'rfc3986' })) 92 | .use(testFiles(t, { 93 | 'test-noproperty.md': undefined, 94 | 'test-simple.md': 'a-simple-string-of-characters', 95 | 'test-symbols.md': 'ccoeoesumrdftm...ooadeltainfinityloveandorlessgreater', 96 | 'test-unicode.md': 'radioactiveskull-and-bonescaduceusbiohazardhammer-and-sickleyin-yangpeacetelephoneumbrella-with-rain-dropstelephone-sun-with-raysstarumbrellasnowmanairplaneenveloperaised-fist', 97 | })) 98 | .build(testDone(t)); 99 | }); 100 | 101 | test('it should not replace an existing slug property', function (t) { 102 | Metalsmith('test/fixtures/basic') 103 | .use(metalsmithSlug()) 104 | .use(testFiles(t, { 105 | 'test-manual.md': 'Something-From-George' 106 | })) 107 | .build(testDone(t)); 108 | }); 109 | 110 | test('it should sluggify an existing slug property with options', function (t) { 111 | Metalsmith('test/fixtures/basic') 112 | .use(metalsmithSlug({ lower: true })) 113 | .use(testFiles(t, { 114 | 'test-manual.md': 'something-from-george' 115 | })) 116 | .build(testDone(t)); 117 | }); 118 | 119 | function testFiles(t, tests) { 120 | return function (files, ms, done) { 121 | Object.keys(files).forEach(function (file) { 122 | var currFile = files[file]; 123 | 124 | if (file in tests) { 125 | t.equal(currFile.slug, tests[file], currFile.testName); 126 | } 127 | }); 128 | 129 | done(); 130 | }; 131 | } 132 | 133 | function testDone(t) { 134 | return function (err) { 135 | if (err) { console.error(err); } 136 | t.end(); 137 | }; 138 | } 139 | --------------------------------------------------------------------------------