1

Metalsmith portrays itself as a ridiculously simple static site generator. I'm trying to write the most basic of sites just to get myself familiar with the basics of the software, but I can't seem to get even that to build. Here's my folder structure:

|- build/
|- index.js
|- src/
    |-index.html
|- templates
    |-index.hbt

My index.js file:

var Metalsmith = require('metalsmith');

Metalsmith(__dirname)
    .destination('./build')
    .build();

My index.html file:

---
title: Home
template: index.hbt
---

And my index.hbt template:

<!doctype html>
<html>
    <head>
        <title>FOO</title>
    </head>
    <body>
        something
    </body>
</html>

My understanding is that the build command should look through the src directory and parse any file it finds with that YAML stuff at the top. So it should look at index.html, see that it renders using the templates/index.hbt template, and basically just move the file into build/index.html. But when I run node index.js I get absolutely nothing. No progress indicator, no "Done building your stuff!" message, just a blinking command prompt line. My build directory is empty. Obviously something is breaking, but there are no logs to check, and no status messages to google. What am I doing wrong? Shouldn't there be at least one page created in the build directory?

tmountjr
  • 1,423
  • 2
  • 22
  • 38

2 Answers2

2

Found the answer, in a comment on a tutorial, of all places: https://blog.robinthrift.com/2014/04/14/metalsmith-part-1-setting-up-the-forge/. It's also on the github examples: https://github.com/segmentio/metalsmith

According to those links, you need to include an error callback on the .build() function:

Metalsmith(__dirname)
    .build(function(err) {
        if (err) throw err;
    });
tmountjr
  • 1,423
  • 2
  • 22
  • 38
1

In addition to the error callback issue, which you've already identified, I think you're missing a few things in your files. True, Metalsmith is very simple, but its simplicity means that a lot of the functionality (like support for templates) is brought by modules that you need to install and include explicitly.

You say the content of your index.js file is:

var Metalsmith = require('metalsmith');

Metalsmith(__dirname)
    .destination('./build')
    .build();

Is that everything you have in you index.js? If you want to use Handlebars templates, you need to explicitly add the metalsmith plugin that handles templates, and instruct it to use handlebars:

var Metalsmith = require('metalsmith');
var templates = require('metalsmith-templates');

Metalsmith(__dirname)
    .destination('./build')
    .use(templates('handlebars'))
    .build();

And make sure that you install the metalsmith-templates and handlebars modules from npm.

Also, you probably know this, but in your index.hbt, you'll need to change

    <title>FOO</title>

to

    <title>{{ title }}</title>

in order to load the title metadata from index.html.

Let me know if this gets you going, or if you need more help.

  • 5
    btw, the `metalsmith-template` plugin has been deprecated, and split in to 2 different plugins `metalsmith-layout` and `metalsmith-in-place` – Bchavez.gd Aug 06 '15 at 16:47