assemble / grunt-assemble-permalinks

Permalinks middleware for Assemble, the static site generator for Grunt.js and Yeoman. This plugin enables powerful and configurable URI replacement patterns, presets, uses Moment.js for parsing dates, and much more.
MIT License
43 stars 11 forks source link

middleware #50

Closed jonschlinkert closed 9 years ago

jonschlinkert commented 10 years ago

@doowb, I'd like to add a middleware option that essentially takes an array of middlewares. Each middleware would just be replacement patterns. So instead of this:

options: {
  permalinks: {
    structure: ':year/:month/:day/:post/index.html',
    patterns: [
      {
        pattern: ':year',
        replacement: function(str) {
          var path = require('path');
          var name = path.basename(str, path.extname(str));
          var re = /(\d{4})-(\d{2})-(\d{2})-(.+)/;
          return name.replace(re, '$');
        }
      },
      {
        pattern: ':month',
        replacement: function(str) {
          var path = require('path');
          var name = path.basename(str, path.extname(str));
          var re = /(\d{4})-(\d{2})-(\d{2})-(.+)/;
          return name.replace(re, '$2');
        }
      },
      {
        pattern: ':day',
        replacement: function(str) {
          var path = require('path');
          var name = path.basename(str, path.extname(str));
          var re = /(\d{4})-(\d{2})-(\d{2})-(.+)/;
          return name.replace(re, '$3');
        }
      },
      {
        pattern: ':post',
        replacement: function(str) {
          var path = require('path');
          var name = path.basename(str, path.extname(str));
          var re = /(\d{4})-(\d{2})-(\d{2})-(.+)/;
          return name.replace(re, '$4');
        }
      }
    ]
  }
}

you could do this:

options: {
  permalinks: {
    structure: ':year/:month/:day/:post/index.html',
    middleware: [require('posts')]
  }
}

Or ideally middlewares would be able to define all options, so if you can use the middleware's defaults, you could just do:

options: {
  permalinks: {
    middleware: [require('posts')]
  }
}

But the latter isn't a have-to-have. I'm not as familiar with the Strings lib as I'd like to be, so I'm not sure what needs to be done to implement this. @doowb can you take a crack at it?

jonschlinkert commented 10 years ago

In the latter, it seems we would just be extending the options with each middleware, similar to how .assemblerc.yml works. so I expect they would have to be called in the order specified in the array.

hariadi commented 10 years ago

:+1:

doowb commented 10 years ago

This should be simple enough to do the first one. I'm not sure if we could do the first and second unless I'm assuming each middleware object will always look like the options:

middleware: [
  {
    structure: ':year/:month/:day/:post/index.html',
    permalinks: {
      ...
    }
  }
]
jonschlinkert commented 10 years ago

I think we need to have a spec for getting/setting values with the context. if we spend some time explaining how/when certain values can be set during the build, then at least the logic and conventions for this kind of thing will be easier to standardize and generalize. (for anyone reading, this won't be easy.)

jonschlinkert commented 9 years ago

this is possible with the 0.6 release of assemble