├── .gitignore ├── .travis.yml ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── assorted_posts │ ├── build │ │ ├── 2014-11-04-four.md │ │ ├── 2014-11-04-two.md │ │ ├── 20141104-five.md │ │ ├── 20141104-six.md │ │ ├── one.md │ │ └── three.md │ └── src │ │ ├── 2014-11-04-four.md │ │ ├── 2014-11-04-two.md │ │ ├── 20141104-five.md │ │ ├── 20141104-six.md │ │ ├── one.md │ │ └── three.md ├── filenames │ ├── build │ │ ├── 2014-11-04-file.md │ │ ├── 20141104-compact-format.md │ │ ├── compact-20141104.md │ │ ├── date-can-2014-11-04-go-anywhere.md │ │ └── like in parenthesis (2014-11-04).md │ └── src │ │ ├── 2014-11-04-file.md │ │ ├── 20141104-compact-format.md │ │ ├── compact-20141104.md │ │ ├── date-can-2014-11-04-go-anywhere.md │ │ └── like in parenthesis (2014-11-04).md └── single_post_with_disparate_dates │ ├── build │ └── 2014-10-02-one.md │ └── src │ └── 2014-10-02-one.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | node_modules 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.11' 4 | - '0.10' 5 | - '0.12' 6 | deploy: 7 | provider: npm 8 | email: gerardo+npm@gaderas.com 9 | api_key: 10 | secure: nBrjHt/tmz8eou0+VE8k23aSkEgr6PrraKl7Y9YGauHNMrLV8StEVcrxC46Hk2Sx6p/3Ub+rdTGOzoZD5/ja5E6KsJn/cjyyNTB9OziuzRNR4edbO3BveAXOBJmaMyasMfxz6bDa2dCty6uOZEy+p5OPbDUqRmYN6w/iyD1ItMM= 11 | on: 12 | all_branches: true 13 | tags: true 14 | repo: sanx/metalsmith-date-in-filename 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | metalsmith-date-in-filename 2 | ==== 3 | 4 | Enrich file metadata with date info present in source filenames. 5 | 6 | [![Build Status](https://travis-ci.org/sanx/metalsmith-date-in-filename.svg?branch=master)](https://travis-ci.org/sanx/metalsmith-date-in-filename) 7 | [![npm version](https://badge.fury.io/js/metalsmith-date-in-filename.svg)](http://badge.fury.io/js/metalsmith-date-in-filename) 8 | 9 | Installation 10 | ---- 11 | 12 | `npm install --save-dev metalsmith-date-in-filename` 13 | 14 | Usage 15 | ---- 16 | 17 | Set the date in the filename: 18 | 19 | 2014-11-04-file.md 20 | date-can-2014-11-04-go-anywhere.md 21 | like in parenthesis (2014-11-04).md 22 | 20141104-compact-format.md 23 | compact-20141104.md 24 | 25 | Then in your code: 26 | 27 | var Metalsmith = require('Metalsmith'); 28 | var dateInFilename = require('metalsmith-date-in-filename'); 29 | Metalsmith(__dirname) 30 | .use(dateInFilename({override: true})) 31 | .build(); 32 | 33 | ... alternatively, you can pass just a boolean to the plugin, and the `override` setting will be set to it: 34 | 35 | ... 36 | .use(dateInFilename(true)) 37 | ... 38 | 39 | 40 | CLI Usage 41 | ---- 42 | 43 | { 44 | "plugins": { 45 | "metalsmith-date-in-filename": {"override": true} 46 | } 47 | } 48 | 49 | 50 | License 51 | ---- 52 | 53 | ISC 54 | 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | 3 | module.exports = function(options){ 4 | var override = (('boolean' === typeof options) && options) || ('object' === typeof options && options.override) || false; 5 | return function drafts(files, metalsmith, done){ 6 | _.forEach(files, function (fileMeta, fileName) { 7 | if (!override && fileMeta.date) { 8 | return; 9 | } 10 | var m; 11 | if (m = fileName.match(/(\d{4}-\d{2}-\d{2})/)) { 12 | fileMeta.date = new Date(m[1]); 13 | } else if (m = fileName.match(/(\d{8})/)) { 14 | fileMeta.date = new Date( 15 | m[1].substr(0, 4) +'-'+ 16 | m[1].substr(4, 2) +'-'+ 17 | m[1].substr(6, 2) 18 | ); 19 | } 20 | }); 21 | var filesWithoutContents = _.zipObject(_.map(files, function (fileMeta, fileName) { 22 | var filteredFileMeta = _.reduce(fileMeta, function (acc, metaValue, metaName) { 23 | if ('contents' !== metaName) { 24 | acc[metaName] = metaValue; 25 | } 26 | return acc; 27 | }, {}); 28 | return [fileName, filteredFileMeta]; 29 | })); 30 | done(); 31 | }; 32 | }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "metalsmith-date-in-filename", 3 | "version": "0.0.14", 4 | "description": "Incorporate date information on filename into metadata.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/sanx/metalsmith-date-in-filename" 12 | }, 13 | "keywords": [ 14 | "date", 15 | "filename", 16 | "metalsmith", 17 | "jekyll" 18 | ], 19 | "author": "gerardo@gerardomoad.com", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/sanx/metalsmith-date-in-filename/issues" 23 | }, 24 | "homepage": "https://github.com/sanx/metalsmith-date-in-filename", 25 | "dependencies": { 26 | "lodash": "^2.4.1" 27 | }, 28 | "devDependencies": { 29 | "metalsmith": "^1.0.1", 30 | "mocha": "^2.0.1" 31 | }, 32 | "directories": { 33 | "test": "test" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/2014-11-04-four.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/2014-11-04-four.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/2014-11-04-two.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/2014-11-04-two.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/20141104-five.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/20141104-five.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/20141104-six.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/20141104-six.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/one.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/one.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/build/three.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/assorted_posts/build/three.md -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/2014-11-04-four.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: post number four 3 | --- 4 | -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/2014-11-04-two.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: second post 3 | date: 2014-11-01 4 | --- 5 | -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/20141104-five.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: fifth post 3 | date: 2014-11-01 4 | --- -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/20141104-six.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: sixth post 3 | --- -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/one.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: first post 3 | date: 2014-11-04 4 | --- 5 | -------------------------------------------------------------------------------- /test/fixtures/assorted_posts/src/three.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: post number three 3 | --- 4 | -------------------------------------------------------------------------------- /test/fixtures/filenames/build/2014-11-04-file.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/build/2014-11-04-file.md -------------------------------------------------------------------------------- /test/fixtures/filenames/build/20141104-compact-format.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/build/20141104-compact-format.md -------------------------------------------------------------------------------- /test/fixtures/filenames/build/compact-20141104.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/build/compact-20141104.md -------------------------------------------------------------------------------- /test/fixtures/filenames/build/date-can-2014-11-04-go-anywhere.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/build/date-can-2014-11-04-go-anywhere.md -------------------------------------------------------------------------------- /test/fixtures/filenames/build/like in parenthesis (2014-11-04).md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/build/like in parenthesis (2014-11-04).md -------------------------------------------------------------------------------- /test/fixtures/filenames/src/2014-11-04-file.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/src/2014-11-04-file.md -------------------------------------------------------------------------------- /test/fixtures/filenames/src/20141104-compact-format.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/src/20141104-compact-format.md -------------------------------------------------------------------------------- /test/fixtures/filenames/src/compact-20141104.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/src/compact-20141104.md -------------------------------------------------------------------------------- /test/fixtures/filenames/src/date-can-2014-11-04-go-anywhere.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/src/date-can-2014-11-04-go-anywhere.md -------------------------------------------------------------------------------- /test/fixtures/filenames/src/like in parenthesis (2014-11-04).md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/filenames/src/like in parenthesis (2014-11-04).md -------------------------------------------------------------------------------- /test/fixtures/single_post_with_disparate_dates/build/2014-10-02-one.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sanx/metalsmith-date-in-filename/485d6b7db959b762ce693cb7fc59a419a9429229/test/fixtures/single_post_with_disparate_dates/build/2014-10-02-one.md -------------------------------------------------------------------------------- /test/fixtures/single_post_with_disparate_dates/src/2014-10-02-one.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: first post 3 | date: 2014-11-04 4 | --- 5 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | var assert = require('assert'); 3 | var Metalsmith = require('metalsmith'); 4 | var dateInFilename = require('..'); 5 | 6 | function buildDone(done) { 7 | return function (err) { 8 | if (err) { 9 | return done(err); 10 | } 11 | 12 | done(); 13 | } 14 | } 15 | 16 | describe('metalsmith-date-in-filename', function () { 17 | it('should add date to meta if it\'s present in filename', function (done) { 18 | var metalsmith = Metalsmith('test/fixtures/assorted_posts'); 19 | metalsmith 20 | .use(dateInFilename({override: false})) 21 | .use(function (files, metalsmith, done) { 22 | _.forEach(files, function (fileMeta, fileName) { 23 | switch (fileName) { 24 | case '2014-11-04-four.md': 25 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 26 | break; 27 | case '20141104-six.md': 28 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 29 | break; 30 | } 31 | }); 32 | done(); 33 | }) 34 | .build(buildDone(done)); 35 | }); 36 | it('should work with different date placements', function(done) { 37 | var metalsmith = Metalsmith('test/fixtures/filenames'); 38 | metalsmith 39 | .use(dateInFilename(true)) 40 | .use(function (files, metalsmith, done) { 41 | _.forEach(files, function (fileMeta, fileName) { 42 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 43 | }); 44 | done(); 45 | }) 46 | .build(buildDone(done)); 47 | }); 48 | it('should add date to meta if it\'s present in filename, but shouldn\'t override if it\'s already present in front matter', function(done) { 49 | var metalsmith = Metalsmith('test/fixtures/assorted_posts'); 50 | metalsmith 51 | .use(dateInFilename({override: false})) 52 | .use(function (files, metalsmith, done) { 53 | _.forEach(files, function (fileMeta, fileName) { 54 | switch (fileName) { 55 | case 'one.md': 56 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 57 | break; 58 | case '2014-11-04-two.md': 59 | assert.equal((new Date('2014-11-01')).toISOString(), fileMeta.date.toISOString()); 60 | break; 61 | case 'three.md': 62 | assert.equal(undefined, fileMeta.date); 63 | break; 64 | case '2014-11-04-four.md': 65 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 66 | break; 67 | case '20141104-five.md': 68 | assert.equal((new Date('2014-11-01')).toISOString(), fileMeta.date.toISOString()); 69 | break; 70 | case '20141104-six.md': 71 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 72 | break; 73 | } 74 | }); 75 | done(); 76 | }) 77 | .build(buildDone(done)); 78 | }); 79 | it('should add date to meta if it\'s present in filename, and it should override if it\'s already present in front matter', function(done) { 80 | var metalsmith = Metalsmith('test/fixtures/assorted_posts'); 81 | metalsmith 82 | .use(dateInFilename({override: true})) 83 | .use(function (files, metalsmith, done) { 84 | _.forEach(files, function (fileMeta, fileName) { 85 | switch (fileName) { 86 | case 'one.md': 87 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 88 | break; 89 | case '2014-11-04-two.md': 90 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 91 | break; 92 | case 'three.md': 93 | assert.equal(undefined, fileMeta.date); 94 | break; 95 | case '2014-11-04-four.md': 96 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 97 | break; 98 | case '20141104-five.md': 99 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 100 | break; 101 | case '20141104-six.md': 102 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 103 | break; 104 | } 105 | }); 106 | done(); 107 | }) 108 | .build(buildDone(done)); 109 | }); 110 | it('passing `false` is shorthand for setting `override` option to `false`', function(done) { 111 | var metalsmith = Metalsmith('test/fixtures/single_post_with_disparate_dates'); 112 | metalsmith 113 | .use(dateInFilename(false)) 114 | .use(function (files, metalsmith, done) { 115 | _.forEach(files, function (fileMeta, fileName) { 116 | switch (fileName) { 117 | case '2014-10-02-one.md': 118 | assert.equal((new Date('2014-11-04')).toISOString(), fileMeta.date.toISOString()); 119 | break; 120 | } 121 | }); 122 | done(); 123 | }) 124 | .build(buildDone(done)); 125 | }); 126 | it('passing `true` is shorthand for setting `override` option to `true`', function(done) { 127 | var metalsmith = Metalsmith('test/fixtures/single_post_with_disparate_dates'); 128 | metalsmith 129 | .use(dateInFilename(true)) 130 | .use(function (files, metalsmith, done) { 131 | _.forEach(files, function (fileMeta, fileName) { 132 | switch (fileName) { 133 | case '2014-10-02-one.md': 134 | assert.equal((new Date('2014-10-02')).toISOString(), fileMeta.date.toISOString()); 135 | break; 136 | } 137 | }); 138 | done(); 139 | }) 140 | .build(buildDone(done)); 141 | }); 142 | }); 143 | --------------------------------------------------------------------------------