blog/app.js

131 lines
3.1 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');
2016-10-17 18:53:00 +00:00
const striptags = require('striptags');
2016-05-21 09:42:57 +00:00
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());
2017-10-15 06:48:59 +00:00
app.use(express.static(__dirname + '/build'));
2016-05-21 09:42:57 +00:00
2018-08-30 19:20:50 +00:00
var md = markdown({html: true});
2016-05-21 09:42:57 +00:00
md.parser.use(emoji);
Handlebars.registerHelper('markdown', function(text) {
2016-07-26 12:41:47 +00:00
if(!text) return;
2016-05-21 09:42:57 +00:00
return md.parser.render(text);
});
Handlebars.registerHelper('moment', function(date, format) {
return new moment(date).format(format);
});
2016-10-17 18:53:00 +00:00
Handlebars.registerHelper("striptags", function(text){
return striptags(text);
});
2017-10-15 16:36:59 +00:00
Handlebars.registerHelper("buildTitle", function(title, siteTitle){
if (title.indexOf(siteTitle) < 0) {
title = `'${title}' by ${siteTitle}`;
}
return title;
});
2016-05-21 09:42:57 +00:00
Metalsmith(__dirname)
.use(define({
site: {
title: 'Marcus Noble',
description: 'Awesomeness with a side of geek',
2017-06-07 05:55:21 +00:00
url: 'https://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 () {
2017-10-08 14:28:37 +00:00
console.log(`App listening at http://localhost:${port}`);
2016-05-21 09:42:57 +00:00
});
2016-07-26 09:08:49 +00:00
});