-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
174 lines (160 loc) · 4.61 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use strict";
const fs = require('fs');
const https = require('https');
const express = require('express');
const compress = require('compression');
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');
const date = require('metalsmith-build-date');
const sitemap = require('metalsmith-sitemap');
const Handlebars = require('handlebars');
const emoji = require('markdown-it-emoji');
const moment = require('moment');
const striptags = require('striptags');
const port = process.env.PORT || 8000;
app.disable('x-powered-by');
app.use(compress());
app.use(express.static(__dirname + '/build'));
// Lets try and slow down some of those exploit crawlers
app.use("/", require('./filterRoutes'));
// Redirects
app.use("/", require('./redirects'));
// Handle some iOS icon 404s
app.get("/apple-touch-icon*", function(req, res) {
res.sendFile(__dirname + '/build/images/favico/' + req.url, () => {
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
});
});
app.get("/images/favico/*.png", function(req, res) {
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
});
app.get("/favicon.png", function(req, res) {
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
});
app.get("/robots.txt", function(req, res) {
res.send("User-agent: * Disallow: ");
})
app.get(/(\/(feeds?|rss|atom)\/?|feed.xml|rss.xml|index.rss|feed.rss)$/, function(req, res) {
res.redirect(301, '/feed.xml');
});
app.get(/.+\/manifest.json$/, function(req, res) {
res.redirect(301, '/manifest.json');
});
app.get("/healthz", function(req, res) {
res.sendStatus(200);
});
var md = markdown({html: true});
md.parser.use(emoji);
const proxy = (tokens, idx, options, env, self) => self.renderToken(tokens, idx, options);
const defaultTableOpenRenderer = md.parser.renderer.rules.table_open || proxy;
md.parser.renderer.rules.table_open = function(tokens, idx, options, env, self) {
tokens[idx].attrJoin("role", "grid");
return defaultTableOpenRenderer(tokens, idx, options, env, self)
};
Handlebars.registerHelper('markdown', function(text) {
if(!text) return;
return md.parser.render(text);
});
Handlebars.registerHelper('moment', function(date, format) {
return new moment(date).format(format);
});
Handlebars.registerHelper("striptags", function(text){
return striptags(text);
});
Handlebars.registerHelper("buildTitle", function(title, siteTitle){
if (title.indexOf(siteTitle) < 0) {
title = `'${title}' by ${siteTitle}`;
}
return title;
});
Handlebars.registerHelper("jointags", function(tags){
return (tags || '').split(' ').join(',');
});
Metalsmith(__dirname)
.use(define({
site: {
title: 'Marcus Noble',
description: 'The blog of Marcus Noble, self-described tinkerer, platform engineer and all round average guy!',
url: 'https://marcusnoble.co.uk'
}
}))
.use(date())
.use(collections({
posts: {
pattern: 'posts/*',
sortBy: 'date',
reverse: true,
},
drafts: {
pattern: 'drafts/*',
sortBy: 'date',
reverse: true,
},
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'
},
{
match: { collection: 'drafts' },
pattern: 'drafts/:title'
}
]
}))
.use(feed({
collection: 'posts',
destination: 'feed.xml'
}))
.use(sitemap({
hostname: 'https://marcusnoble.co.uk',
modifiedProperty: 'date'
}))
.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 at http://localhost:${port}`);
});
});