-
Notifications
You must be signed in to change notification settings - Fork 6
/
paperpress.js
342 lines (279 loc) · 8.1 KB
/
paperpress.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var fs = require('fs')
var path = require('path')
var Feed = require('feed')
var sm = require('sitemap')
var Remarkable = require('remarkable')
var highlighter = require('highlight.js')
var frontMatter = require('front-matter')
var sluglify = function (str) {
str = str.replace(/^\s+|\s+$/g, '') // trim
str = str.toLowerCase()
// remove accents, swap ñ for n, and other invalid chars
var invChar = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;'
var invCharTo = 'aaaaeeeeiiiioooouuuunc------'
for (var i = 0, l = invChar.length; i < l; i++) {
str = str.replace(new RegExp(invChar.charAt(i), 'g'), invCharTo.charAt(i))
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-') // collapse dashes
return str
}
var Paperpress = function (config) {
var self = this
if (config === undefined) {
config = {}
}
this.baseDirectory = config.baseDirectory || 'static'
this.uriPrefix = config.uriPrefix
this.pathBuilder = config.pathBuilder || function (item, collectionName) {
var path = '/' + collectionName + '/' + item.slug
if (self.uriPrefix) {
path = this.uriPrefix + path
}
return path
}
this.remarkableOptions = config.remarkableOptions || {
html: true,
linkify: true,
highlight: function (code) {
return highlighter.highlightAuto(code).value
}
}
this.items = []
this.paths = []
this._marked = new Remarkable(this.remarkableOptions)
this._hooks = config.hooks || []
}
/**
**************************************
********** Private Functions *********
**************************************
**/
Paperpress.prototype._getCollections = function () {
var that = this
try {
var collections = fs.readdirSync(this.baseDirectory).filter(function (collection) {
var path = that.baseDirectory + '/' + collection
var stats = fs.statSync(path)
return stats.isDirectory()
})
return collections
} catch (e) {
console.error('[Paperpress] ERROR - Can\'t read directory:', this.baseDirectory)
return null
}
}
Paperpress.prototype._titleToSlug = function (title) {
return sluglify(title)
}
Paperpress.prototype._directoryToItem = function (directory) {
var item = JSON.parse(fs.readFileSync(directory.path + '/info.json').toString())
if (!item.title) {
console.warn('[Paperpress] item ' + directory.path + ' without title')
return
}
if (!item.date) {
console.warn('[Paperpress] item ' + directory.path + ' without date')
return
}
item.slug = this._titleToSlug((item.slug === undefined) ? item.title : item.slug)
item.path = item.path || this.pathBuilder(item, directory.collectionName)
item.date = new Date(item.date)
this.paths.push(item.path)
if (item.contentType === 'html') {
item.content = fs.readFileSync(directory.path + '/content.html').toString()
} else {
var content = fs.readFileSync(directory.path + '/content.md').toString()
item.content = this._marked.render(content)
}
return item
}
Paperpress.prototype._fileToItem = function (file) {
var fileContent = fs.readFileSync(file.path).toString()
var fileType = path.extname(file.path)
var name = file.name.replace(fileType, '')
var slug = this._titleToSlug(name)
var item
if (fileType === '.json') {
item = JSON.parse(fileContent)
item.title = item.title || name
item.slug = item.slug || slug
if (item.date) {
item.date = new Date(item.date)
}
} else {
item = {
title: name,
slug: slug,
content: fileContent,
path: null
}
}
if (frontMatter.test(fileContent)) {
var contentData = frontMatter(fileContent)
var dataAttributes = contentData.attributes
Object.keys(dataAttributes)
.filter(function (k) { // prevents attribute leaking
return dataAttributes.hasOwnProperty(k)
})
.forEach(function (k) { // merges into item object
item[k] = dataAttributes[k]
})
// Assign parsed content
item.content = contentData.body
// If date exists then parse it
if (dataAttributes.date) {
item.date = new Date(dataAttributes.date)
}
}
if (!item.date) {
var stat = fs.statSync(file.path)
item.date = new Date(stat.mtime)
}
// If path was not assigned, then use helper method
item.path = item.path ? item.path : this.pathBuilder(item, file.collectionName)
this.paths.push(item.path)
// Do not render as markdown if file is .html
if (fileType === '.md') {
item.content = this._marked.render(item.content)
}
return item
}
Paperpress.prototype._getDuplicatePaths = function () {
var paths = this.paths.slice()
var duplicatePaths = []
paths.sort().forEach(function (item, i) {
if (paths[i + 1] === item) {
duplicatePaths.push(item)
}
})
return duplicatePaths
}
Paperpress.prototype._loadCollection = function (collectionName) {
var self = this
self.items = self.items.filter(function (item) {
return item.type !== collectionName
})
fs.readdirSync(this.baseDirectory + '/' + collectionName).forEach(function (itemName) {
var path = self.baseDirectory + '/' + collectionName + '/' + itemName
var stats = fs.statSync(path)
if (itemName.indexOf('.') === 0) { return }
var item
if (stats.isDirectory()) {
item = self._directoryToItem({
path: path,
stats: stats,
collectionName: collectionName
})
} else {
item = self._fileToItem({
name: itemName,
path: path,
collectionName: collectionName
})
}
if (item) {
item.type = collectionName
self._hooks.forEach(function (fn) {
fn(item)
})
self.items.push(item)
}
})
}
Paperpress.prototype._sortByDate = function (items) {
return items.sort(function (a, b) {
return new Date(a.date).getTime() - new Date(b.date).getTime() <= 0 ? 1 : -1
})
}
/**
**************************************
********** Public Functions **********
**************************************
**/
Paperpress.prototype.getCollection = function (collectionName) {
var collection = this.items.filter(function (item) {
return item.type === collectionName
})
return this._sortByDate(collection)
}
Paperpress.prototype.getCollections = function (collectionsName) {
var collection = this.items.filter(function (item) {
return collectionsName.indexOf(item.type) >= 0
})
return this._sortByDate(collection)
}
Paperpress.prototype.load = function () {
var that = this
var collections = this._getCollections()
try {
collections.forEach(function (collection) {
that._loadCollection(collection)
})
} catch (e) {
console.error('[Paperpress] ERROR on load', e.message)
return null
}
var duplicatePaths = this._getDuplicatePaths()
if (duplicatePaths.length > 0) {
console.warn('[Paperpress] Duplicated paths:', duplicatePaths)
}
return true
}
Paperpress.prototype.addHook = function (hook) {
this._hooks.push(hook)
}
/**
**************************************
********** Helpers Functions *********
**************************************
**/
Paperpress.helpers = {}
Paperpress.helpers.createFeed = function (description, items) {
// Create the Feed instance
// Eventually it should deprecate link in favor of hostname
if (description && !description.link) {
description.link = description.hostname
}
try {
var feed = new Feed(description)
} catch (e) {
console.error('[Paperpress] ERROR - Feed. Can\'t create the feed check the description object')
return null
}
// Load the items on the feed
try {
items.forEach(function (item) {
item.link = (description.hostname || description.link) + item.path
item.date = new Date(item.date)
feed.addItem(item)
})
} catch (e) {
console.error('[Paperpress] ERROR - Feed. Undefined array for items')
return null
}
// Test if there is no error with the feed render function
try {
feed.render()
} catch (e) {
console.error('[Paperpress] ERROR - Feed', e)
return null
}
return feed
}
Paperpress.helpers.createSiteMap = function (description, items) {
// Eventually it should deprecate link in favor of hostname
if (!description.hostname) {
description.hostname = description.link
}
var sitemap = sm.createSitemap({
hostname: description.hostname,
cacheTime: 600000
})
items.forEach(function (item) {
sitemap.add({url: item.path, changefreq: 'weekly'})
})
return sitemap.toString()
}
module.exports = Paperpress