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

Is it possible to create the permalink structure from the filename structure? #57

Closed saviomd closed 6 years ago

saviomd commented 10 years ago

My permalink config is

permalinks: {
    structure: ':path/index.html'
}

I set the path in the YFM of every file and it's working fine, the files are compiled like '/jogos/lista/index.html', '/jogos/buraco/index.html', etc But I wanted to have something more automatic, I don't want to do this in every single html file.

I was looking for a way to have something like that

permalinks: {
    structure: ':basename/index.html'
}

So the name of the files would be something like "jogoslista.html", "jogosburaco.html" and I would have a way to intercept the basename and replace all "__" with "/". Is this currently possible? (I'm using assemble 0.4.41 and permalinks 0.3.6)

doowb commented 10 years ago

I think a permalinks configuration like this will work for you:

  permalinks: {
    structure: ':custom/:basename/index.html',
    patterns: [
      {
        pattern: ':custom',
        replacement: function () {
          var root = path.join(grunt.config.get('config.src'), 'templates/pages');
          var ext = path.extname(this.src);
          return this.src
            .replace(root, '')
            .replace(ext, '')
            .replace(this.basename, '')
            .replace(/^[\/\\]/, '');
        }
      }
    ]
  }

grunt.config.get('config.src') is a property set in my grunt.initConfig. It might be something different for you.

saviomd commented 10 years ago

Ok, I'll try that out. Thank you!

LaurentGoderre commented 9 years ago

@doowb this is a nice workaround but perhaps this should be built in