var gulp = require('gulp');
var Metalsmith = require('metalsmith');

var collections = require('metalsmith-collections');
var ignore = require('metalsmith-ignore');
var permalinks = require('metalsmith-permalinks');

// Content
var fileMetadata = require('metalsmith-filemetadata');
var metadata = require('metalsmith-metadata');
var markdown = require('metalsmith-markdown');
var templates = require('metalsmith-templates');

// CSS
var less = require('metalsmith-less');
var fingerprint = require('metalsmith-fingerprint');
var autoprefixer = require('metalsmith-autoprefixer');

var Handlebars = require('handlebars');
var moment = require('handlebars-helper-moment')();

Handlebars.registerHelper('moment', moment.moment);

Handlebars.registerHelper('log', function(content) {
  console.log(content);
});

gulp.task('build', function(callback) {
  Metalsmith('./')
    // CSS
    .use(less({
      pattern: '**/main.less'
    }))
    .use(autoprefixer())
    .use(fingerprint({
      pattern: '**/*.css'
    }))
    .use(ignore([
      '**/*.less',
      '**/main.css',
      '**/.DS_Store'
    ]))
    // Content
    .use(metadata({
      site: 'site.yaml'
    }))
    .use(fileMetadata([
      {
        pattern: '**/*.md',
        metadata: {
          template: 'default.html'
        },
        preserve: true
      }
    ]))
    .use(collections({
      pages: {
        pattern: '*.md',
        sortBy: 'order'
      },
      articles: {
        pattern: 'articles/*.md',
        sortBy: 'date',
        reverse: true
      }
    }))
    .use(markdown({
      smartypants: true
    }))
    .use(permalinks({
      relative: false
    }))
    .use(templates('handlebars'))
    .build(function(err, files) {
      if (err) {
        return callback(err);
      }
      callback();
    });
});

@@ -1661,28 +1661,246 @@
 mplates(
-'handlebars'
+%7B%0A      engine: 'handlebars',%0A      inPlace: false%0A    %7D))%0A    // Have to run templates a second time,%0A    // in order to render Handlbars within markdown.%0A    .use(templates(%7B%0A      engine: 'handlebars',%0A      inPlace: true%0A    %7D
 ))%0A    .

Make Handlebars render from within markdown.