blog/app.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-05-21 09:42:57 +00:00
"use strict";
const fs = require('fs');
const https = require('https');
const express = require('express');
2016-05-27 05:04:05 +00:00
const compress = require('compression');
2016-05-21 09:42:57 +00:00
const app = express();
const Metalsmith = require('metalsmith');
const inplace = require('metalsmith-in-place');
const layouts = require('metalsmith-layouts');
const markdown = require('metalsmith-markdownit');
const permalinks = require('metalsmith-permalinks');
const collections = require('metalsmith-collections');
const pagination = require('metalsmith-pagination');
const define = require('metalsmith-define');
const feed = require('metalsmith-feed');
const sass = require('metalsmith-sass');
2016-05-27 06:02:17 +00:00
const date = require('metalsmith-build-date');
2016-05-21 09:42:57 +00:00
const Handlebars = require('handlebars');
const emoji = require('markdown-it-emoji');
const moment = require('moment');
const port = process.env.PORT || 8000;
2016-05-27 05:10:01 +00:00
const oneDay = 86400000;
2016-05-21 09:42:57 +00:00
2016-05-27 05:04:05 +00:00
app.use(compress());
2016-05-27 06:02:17 +00:00
app.use(express.static(__dirname + '/build', {maxAge: oneDay * 365}));
2016-05-21 09:42:57 +00:00
var md = markdown('commonmark', {html: true});
md.parser.use(emoji);
Handlebars.registerHelper('markdown', function(text) {
return md.parser.render(text);
});
Handlebars.registerHelper('moment', function(date, format) {
return new moment(date).format(format);
});
Metalsmith(__dirname)
.use(define({
site: {
title: 'Marcus Noble',
description: 'Awesomeness with a side of geek',
2016-07-26 09:11:56 +00:00
url: 'https://blog.marcusnoble.co.uk'
2016-05-21 09:42:57 +00:00
}
}))
2016-05-27 06:02:17 +00:00
.use(date())
2016-05-21 09:42:57 +00:00
.use(collections({
posts: {
pattern: 'posts/*',
sortBy: 'date',
reverse: true,
},
2016-07-26 09:08:49 +00:00
drafts: {
pattern: 'drafts/*',
sortBy: 'date',
reverse: true,
},
2016-05-21 09:42:57 +00:00
projects: {
pattern: 'projects/*'
},
pages: {
pattern: 'pages/*'
2016-05-27 05:04:05 +00:00
},
experience: {
pattern: 'experience/*',
sortBy: 'start',
reverse: true
2016-05-21 09:42:57 +00:00
}
}))
.use(inplace({
engine: 'handlebars',
directory: 'templates',
partials: 'templates/partials'
}))
.use(md)
.use(permalinks({
pattern: ':date-:title',
date: 'YYYY-MM-DD',
linksets: [
{
match: { collection: 'pages' },
pattern: ':title'
2016-07-26 09:08:49 +00:00
},
{
match: { collection: 'drafts' },
pattern: 'drafts/:title'
2016-05-21 09:42:57 +00:00
}
]
}))
.use(feed({
collection: 'posts',
destination: 'feed.xml'
}))
.use(pagination({
'collections.posts': {
perPage: 5,
layout: 'index.html',
first: 'index.html',
noPageOne: true,
path: 'page:num/index.html',
pageMetadata: {
title: 'Posts'
}
}
}))
.use(layouts({
engine: 'handlebars',
directory: 'templates',
partials: 'templates/partials'
}))
.use(sass())
.build(function(err) {
if (err) throw err;
app.listen(port, function () {
console.log(`App listening on port ${port}`);
});
2016-07-26 09:08:49 +00:00
});