1

I have lots of markdown source files, which are automatically generated by some data export step.

I'm using metalsmith-layouts and have e.g. these lines on top of my handcrafted (not automatically generated) markdown files in order to layout them using my layout.html:

---
layout: layout.html
title:  Downloads
---

How can I add the layout info dynamically to all files that match for example a file pattern tutorials/*.md, so that those files are also rendered with my layout? Adding this info to the files before conversion wouldn't be elegant.

benjist
  • 2,740
  • 3
  • 31
  • 58

2 Answers2

0

I found it out meanwhile. It's possible to define a default layout to a file pattern this way:

// Apply the default layout to all .html files
.use(layouts({
    engine: 'handlebars',
    default: 'layout.html',
    directory: 'layouts',
    pattern: '**/*.html'
}))
benjist
  • 2,740
  • 3
  • 31
  • 58
0

I very recently just wrote my first few Metalsmith plugins, one of which, keymaster, can do exactly what you originally wanted. To set the layout info to "foobar.html" for all the tutorials/*.md files, you would

use(keymaster(function() { return "foobar.html; },   // set it to "foobar.html"
              "layout",                              // in the layout field
              /tutorials.*md/);                      // for files matching this regex

(Note: my Regex isn't perfect, so that final line may be off.

Hmm, the fact that the first line is so clunky has me thinking of changin my API a bit.

Note, API has recently slightly changed, please read the docs.

user949300
  • 15,364
  • 7
  • 35
  • 66