Search code examples
handlebars.jsmetalsmith

How to insert layout meta info into all files matching a file pattern?


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.


Solution

  • 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'
    }))