Domanda

I want the path of my blog posts to use the fancy date format e.g. /blog/2013/09/17 so the links from my old octopress blog are not broken.

In the YAML front matter on each markdown page I have added the date.

---
date: 2013-09-13
---

So in the Gruntfile.js I want to do something like this:

dest: './blog/<%= date.getFullYear() %>/<%= date.getMonth() %>/<%= date.getDate() %>'

Though can't find a way to get hold of that date property from the YFM. Is this possible, and it is the right way to achieve what I want?

È stato utile?

Soluzione

EDIT: Since this answer was initially posted, a permalinks plugin was published that makes this super easy: https://github.com/assemble/assemble-contrib-permalinks


I think a better approach would be to create a feature request at http://github.com/assemble/assemble/issues, however, if you prefer to find a way to use custom logic in lodash templates inside the gruntfile, then my suggestion would be to create mixins that can then be used in your templates.

To add mixins, just do something like this outside the initConfig object (there are other ways, but this is easiest):

module.exports = function (grunt) {

  grunt.util._.mixin({
    year: function(foo) {
      return date.getFullYear(foo);
    },
    month: function(foo) {
      return date.getMonth(foo);
    },
    day: function(foo) {
      return date.getDay(foo);
    }
  });

  grunt.initConfig({
    foo: {
      src: 'path/to/my/files/**`
      // Now we can use the mixins like this:
      dest: <%= _.year() %>/<%= _.month() %>/<%= _.day() %>'
  });
  grunt.registerTask(...);
};

The challenge with this approach is getting the context from the YAML front matter of the src files and then returning the dates to be used in the dest paths. Alternatively, this should't be difficult to implement as a native feature in assemble, and I think others would benefit from it as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top