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

Using {{relative}} helper with pretty permalink preset #64

Open andymcfee opened 8 years ago

andymcfee commented 8 years ago

I've googled for hours and I can't seem to find any issues, answers, documentation to answer the particular issue I'm having so here it goes... Also, I'm not sure if this belongs here or in another repo's issues, but I'll start here.

My main goal is to generate a set of link for navigation that will use the pretty permalink structure instead of pointing to the index.html file of each respective page.

Assemble packages:

"grunt-assemble": "^0.4.0",
"grunt-assemble-permalinks": "^0.1.0",

Relevant assemble config

assemble: {
  options: {
    plugins: [
      'grunt-assemble-permalinks'
    ],
    permalinks: {
      preset: 'pretty'
    },
    collections: [{
      name: 'navMain',
      sortby: 'navOrder',
      sortorder: 'ascending'
    }],
    ...
},

Handlebars nav partial

<ul>
  {{#withSort navMain 'data.navOrder'}}
    {{#each this.pages}}
      <li class="{{#is ../../page.dest this.dest}}active{{/is}}">
        <a href="{{relative ../../page.dest this.dest}}">
          {{data.title}}
        </a>
      </li>
    {{/each}}
  {{/withSort}}
</ul>

Assemble HTML for index.html

<ul>
  <li>
    <a href="index.html">
      Home
    </a>
  </li>
  <li>
    <a href="about/index.html">
      About
    </a>
  </li>
  ...
</ul>

Expected HTML

<ul>
  <li>
    <a href="/">
      Home
    </a>
  </li>
  <li>
    <a href="about/">
      About
    </a>
  </li>
  ...
</ul>

Am I missing something? Is this not possible? If my generated nav is pointing to the page/index.html file, this kills the whole point of using pretty URLs, no? Thanks in advance for any assistance. And if I need to open the issue somewhere else or provide additional info, just let me know.

andymcfee commented 8 years ago

I solved this with a custom helper.

helpers.js

module.exports.register = function (Handlebars, options) {
  'use strict';

  Handlebars.registerHelper('relativePretty', function (from, to) {
    var url = Handlebars.helpers.relative(from, to);
    return url.substring(0, url.indexOf('index.html'));
  });

};

nav.hbs:

<a href="{{relativePretty ../../page.dest this.dest}}">
  {{title}}
</a>

I will leave this open in case my usecase is actually a bug. But if it is intended to be solved with a custom helper, feel free to close.