From bbf0b2feedd53ea86a7dd80856d502ab8c11cd1d Mon Sep 17 00:00:00 2001 From: "Jocelyn Badgley (Twipped)" Date: Fri, 6 Mar 2020 19:36:09 -0800 Subject: [PATCH] Write out a post index into /tweets/index.json. Write out assets for debugging. --- .gitignore | 1 + build/index.js | 18 ++++++++++++++---- build/page-writer.js | 25 +++++++++++++++++++------ build/page.js | 2 +- build/post.js | 8 ++++++++ gulp/clean.js | 4 ++-- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 449ebca..1d8cd50 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ node_modules /terraform/.terraform /terraform/files/*.zip +/assets.json diff --git a/build/index.js b/build/index.js index 80a8c3c..f118963 100644 --- a/build/index.js +++ b/build/index.js @@ -18,6 +18,10 @@ const scss = require('./scss'); const svg = require('./svg'); const scripts = require('./scripts'); +function writeIndex (destination, files, compressed) { + files = files.map((p) => !p.draft && (p.toJson ? p.toJson() : p)); + return fs.writeFile(resolve(destination), compressed ? JSON.stringify(files) : JSON.stringify(files, null, 2)); +} exports.everything = function (prod = false) { async function fn () { @@ -43,6 +47,8 @@ exports.everything = function (prod = false) { posts = sortBy(posts, 'date'); posts.reverse(); + const assets = [ ...PostFiles.assets, ...PublicFiles.assets ]; + // compile all tasks to be completed const tasks = await Promise.all([ PublicFiles.tasks, @@ -53,16 +59,20 @@ exports.everything = function (prod = false) { favicon(prod), ]); - await fs.writeFile(resolve('pages.json'), JSON.stringify(pages.map((p) => p.toJson()), null, 2)); - await fs.writeFile(resolve('posts.json'), JSON.stringify(posts.map((p) => p.toJson()), null, 2)); + await Promise.all([ + fs.ensureDir(resolve('dist')), + writeIndex('pages.json', pages), + writeIndex('posts.json', posts), + writeIndex('assets.json', assets), + ]); - await fs.ensureDir(resolve('dist')); const cache = new Cache({ prod }); await cache.load(); await evaluate(tasks.flat(), cache); await cache.save(); - await pageWriter(pages, posts, prod); + posts = await pageWriter(pages, posts, prod); + await writeIndex('dist/tweets/index.json', posts.filter(Boolean), true); } fn.displayName = prod ? 'buildForProd' : 'build'; diff --git a/build/page-writer.js b/build/page-writer.js index d71f384..a538391 100644 --- a/build/page-writer.js +++ b/build/page-writer.js @@ -7,8 +7,9 @@ const { siteInfo } = require(resolve('package.json')); module.exports = exports = async function writePageContent (pages, posts, prod) { const engines = await getEngines(prod); - await processPages(engines, posts, null, prod); + const postIndex = await processPages(engines, posts, null, prod); await processPages(engines, pages, posts, prod); + return postIndex; }; function processPages (engines, pages, posts, prod) { @@ -35,21 +36,22 @@ function processPages (engines, pages, posts, prod) { }; const json = { - url: page.fullurl, + url: page.url, + fullurl: page.fullurl, title: page.meta.title, subtitle: page.meta.subtitle, description: page.meta.description, - tweets: page.tweets, - images: page.images, - dateCreated: page.dateCreated, - dateModified: page.dateModified, + date: page.dateCreated, titlecard: page.titlecard, + tags: page.meta.tags, + author: page.meta.author, }; const html = String(engines[page.engine](data.source, data)); if (page.engine === ENGINE.MARKDOWN) { json.preview = String(engines.MARKDOWN_PREVIEW(data.source, data)); page.content = String(engines.MARKDOWN_CONTENT(data.source, data)); + json.content = page.content; } const output = resolve('dist', page.out); @@ -60,5 +62,16 @@ function processPages (engines, pages, posts, prod) { prod ? JSON.stringify(json) : JSON.stringify(json, null, 2), )), ]); + + return !page.draft && { + url: page.url, + json: page.json, + title: page.meta.title, + subtitle: page.meta.subtitle, + description: page.meta.description, + date: page.dateCreated, + tags: page.meta.tags, + author: page.meta.author, + }; }); } diff --git a/build/page.js b/build/page.js index cafbd0d..51cb671 100644 --- a/build/page.js +++ b/build/page.js @@ -92,8 +92,8 @@ module.exports = exports = class Page extends File { _parse (PublicFiles) { const { titlecard, webready } = PublicFiles.for(this.dir); - this.ignore = this.meta.ignore; + this.draft = this.meta.draft; this.images = webready; this.titlecard = titlecard; if (this.meta.tweets && isString(this.meta.tweets)) this.meta.tweets = this.meta.tweets.split(/\s/); diff --git a/build/post.js b/build/post.js index 667a0ad..50cbb80 100644 --- a/build/post.js +++ b/build/post.js @@ -9,6 +9,12 @@ const pkg = require(resolve('package.json')); const postmatch = /(\d{4}-\d\d-\d\d)\.\d{4}\.(\w+)/; +function arrayify (input) { + if (!input) return []; + if (!Array.isArray(input)) return [ input ]; + return input; +} + module.exports = exports = class Post extends Page { _dir (dir) { @@ -60,6 +66,8 @@ module.exports = exports = class Post extends Page { return result; }, {}); + this.meta.author = this.meta.author && arrayify(this.meta.author) || []; + this.classes.push('post'); } diff --git a/gulp/clean.js b/gulp/clean.js index efed2e2..f7581d7 100644 --- a/gulp/clean.js +++ b/gulp/clean.js @@ -3,11 +3,11 @@ const { src } = require('gulp'); const clean = require('gulp-clean'); module.exports = exports = function cleanDistribution () { - return src([ 'dist', 'rev-manifest.json', 'pages.json' ], { read: false, allowEmpty: true }) + return src([ 'dist', 'rev-manifest.json', 'pages.json', 'assets.json' ], { read: false, allowEmpty: true }) .pipe(clean()); }; exports.dev = function cleanDistributionForDev () { - return src([ 'dist/**.{js|json|jsx}', 'rev-manifest.json', 'pages.json' ], { read: false, allowEmpty: true }) + return src([ 'dist/**.{js|json|jsx}', 'rev-manifest.json', 'pages.json', 'assets.json' ], { read: false, allowEmpty: true }) .pipe(clean()); };