totocaster / metalsmith-tags

A metalsmith plugin to create dedicated pages for tags in posts or pages.
49 stars 43 forks source link

this plugin should use metalsmith-layouts instead of the deprecated metalsmith-templates plugin #20

Closed oupala closed 9 years ago

oupala commented 9 years ago

I'm a happy user of metalsmith-tags and I have recently encountered a problem with this metalsmith plugin. The reason is that metalsmith-templates is a declared dependancy of metalsmith-tags. The problem is that metalsmith-templates is now deprecated (as you can read on the README file and has been replaced by metalsmith-layouts.

Do you think it could be possible to replace metalsmith-templates by metalsmith-layout ?

When I use metalsmith-tags 0.10.1, I get empty generated files (the file exist but contains no content at all) for tag pages.

oupala commented 9 years ago

While debugging with metalsmith debug, I can see the following debug statements :

  'tags/html/index.html':
   { template: 'tag.hbt',
     contents: '',
     tag: 'html',
     pagination:
      { num: 1,
        pages: [Object],
        tag: 'html',
        files: [Object] } },

I think that the empty value in the content field might explain the empty generated file.

brianbrewer commented 9 years ago

I've also encountered this same problem, as you said it is because metalsmith-templates is a dependency of metalsmith-tags. This plugin uses the metadata template whereas metalsmith-layouts requires the use of layout instead to provide the template/layout for the file. Below are a couple of solutions that I hope will help

1: Editing metalsmith-tags locally

Changing its reference at L138 from template to layout has made it work perfectly fine.

        // Generate a new file based on the filename with correct metadata.
        var page = {
          layout: opts.template,
          contents: '',
          tag: tag,
          pagination: {
            num: i + 1,
            pages: pages,
            tag: tag,
            files: pageFiles
          }
        };
2: Writing a plugin to create a layout property using the template property.

This solution requires no messing with the plugin internals and only add a new layout field if there is not one already and a template field available.

    .use(function (files, metalsmith, done) {
        Object.keys(files).forEach(function (file) {
            var data = files[file];

            // Only messes with metadata if there is no layout and a template
            if (!data.layout && data.template) {
                data.layout = data.template;
            }
        });
        done();
    })
3: Updating metalsmith-tags

At the same time I also forked the plugin and made the changes to make it so that metalsmith-layouts is used instead of metalsmith-templates brianbrewer/metalsmith-tags

oupala commented 9 years ago

Thanks @brianbrewer for all these suggestions.

I'll go with solution no 2 for the moment.

Since the issue is fixed in another fork, it might be the right moment to merge brianbrewer/metalsmith-tags fork to master... If @hswolff could do this and publish to npmjs.org, it would be great !

oupala commented 9 years ago

Surprisingly, @brianbrewer solutions 1 and 2 do not work for me. Fortunately, solution 3 works prefectly.

Could you please open a pull request so that your fork is merged back to the official plugin, and published on npmjs.org ?

hswolff commented 9 years ago

Awesome work @oupala and @brianbrewer, it's much much much appreciated!

Merged your changes @brianbrewer. I also added back deprecated usage of metalsmith-templates so this plugin will support both for the time being.

Published changes as 0.11.0.