blog/app.js

147 lines
3.5 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');
2021-01-29 22:45:22 +00:00
const sitemap = require('metalsmith-sitemap');
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: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
// Lets try and slow down some of those exploit crawlers
2021-06-15 03:57:09 +00:00
app.use("/", require('./filterRoutes'));
2021-06-15 03:57:09 +00:00
app.get("/robots.txt", function(req, res) {
res.send("User-agent: * Disallow: ");
2020-11-18 12:14:51 +00:00
})
2021-06-15 03:57:09 +00:00
app.get(/\/(feed|rss)\/?$/, function(req, res) {
2020-11-21 20:21:59 +00:00
res.redirect('/feed.xml');
})
2021-06-15 03:57:09 +00:00
app.get("/healthz", function(req, res) {
res.sendStatus(200);
})
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;
});
2021-05-12 08:27:51 +00:00
Handlebars.registerHelper("jointags", function(tags){
return (tags || '').split(' ').join(',');
});
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/*'
}
}))
.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'
}))
2021-01-29 22:45:22 +00:00
.use(sitemap({
hostname: 'https://marcusnoble.co.uk',
modifiedProperty: 'date'
}))
2016-05-21 09:42:57 +00:00
.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
});