const path = require('path'); const fs = require('fs'); const snarkdown = require('snarkdown'); const fecha = require('fecha'); module.exports = function (app) { app.server.get('/', (req, res) => { const files = fs.readdirSync(app.fileLocation).filter(fileName => fileName.includes('.json')) .map(fileName => { // Cache the file data so sorting doesn't need to re-check each file return { name: fileName, time: fs.statSync(path.resolve(app.fileLocation, fileName)).mtime.getTime() }; }).sort((a, b) => a.time - b.time).map(v => v.name); // Sort from oldest to newest. let books = files.map(fileName => { const bookData = JSON.parse(fs.readFileSync(path.resolve(app.fileLocation, fileName), 'utf8')); if (bookData.hasOwnProperty('fileName')) return ''; bookData.author = bookData.author ? bookData.author : 'author not provided'; bookData.contributor = bookData.contributor ? bookData.contributor : 'Anonymous'; const id = fileName.replace('.json', ''); const confirmId = 'confirm_' + id; const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const modal = app.templater.fill('./templates/elements/modalCard.html', { id, header: '