Skip to content

Instantly share code, notes, and snippets.

@bzerangue
Created March 12, 2018 12:26
Show Gist options
  • Save bzerangue/b4a8ac30e5b4e8fc5a3ca3291938208d to your computer and use it in GitHub Desktop.
Save bzerangue/b4a8ac30e5b4e8fc5a3ca3291938208d to your computer and use it in GitHub Desktop.
contentful metalsmith example with Handlebars, Handlebars-Layouts (for template inheritance)
const glob = require('glob')
const Metalsmith = require('metalsmith')
const layouts = require('metalsmith-layouts')
const assets = require('metalsmith-assets')
const sass = require('metalsmith-sass')
const markdown = require('metalsmith-markdown')
const dataMarkdown = require('metalsmith-data-markdown')
const contentful = require('contentful-metalsmith')
const handlebars = require('handlebars')
const handlebarsLayouts = require('handlebars-layouts')
// add custom helpers to handlebars
// https://github.com/superwolff/metalsmith-layouts/issues/63
//
// using the global handlebars instance
glob.sync('helpers/*.js').forEach((fileName) => {
const helper = fileName.split('/').pop().replace('.js', '')
handlebars.registerHelper(
helper,
require(`./${fileName}`)
),
handlebars.registerHelper(
handlebarsLayouts(handlebars)
)
})
handlebarsLayouts.register(handlebars);
Metalsmith(__dirname)
.source('src')
.destination('build')
.use(contentful({
space_id: 'w7sdyslol3fu',
access_token: 'baa905fc9cbfab17b1bc0b556a7e17a3e783a2068c9fd6ccf74ba09331357182',
common: {
featured_author: {
limit: 1,
filter: {
'sys.id[in]': '5JQ715oDQW68k8EiEuKOk8'
}
}
},
}))
.use(layouts({
engine: 'handlebars',
partials: 'partials'
}))
.use(assets({
source: 'assets/',
destination: 'assets/'
}))
.use(sass({
outputStyle: 'compressed'
}))
.use(markdown())
.use(dataMarkdown({
removeAttributeAfterwards: true
}))
.build(function (err) {
if (err) throw err
console.log('Successfully build metalsmith')
})
{
"name": "contentful-metalsmith-example",
"version": "1.0.0",
"description": "A example app that uses the contentful-metalsmith plugin",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/contentful-labs/contentful-metalsmith-example"
},
"scripts": {
"dev": "run-p watch serve",
"serve": "live-server ./build",
"start": "node build.js",
"lint": "standard \"helpers/**/*.js\" \"build.js\"",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "gazer --pattern '{src,layouts,helpers,partials}/**/*' node build.js"
},
"license": "MIT",
"dependencies": {
"bootstrap-sass": "^3.3.7",
"contentful-metalsmith": "^0.9.0",
"gazer": "^0.0.3",
"glob": "^7.0.6",
"handlebars": "^4.0.0",
"handlebars-layouts": "^3.1.4",
"metalsmith": "^2.1.0",
"metalsmith-assets": "^0.1.0",
"metalsmith-clean": "^0.0.0",
"metalsmith-data-markdown": "0.0.3",
"metalsmith-drafts": "^0.0.1",
"metalsmith-layouts": "^1.6.5",
"metalsmith-markdown": "^0.2.1",
"metalsmith-sass": "^1.3.0"
},
"devDependencies": {
"cz-conventional-changelog": "^1.2.0",
"live-server": "^1.1.0",
"npm-run-all": "^3.0.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment