More burndown.

This commit is contained in:
Jocelyn Badgley (Twipped)
2020-02-27 18:57:39 -08:00
parent 2df7574697
commit e95f2cf3db
17 changed files with 736 additions and 528 deletions

View File

@@ -3,74 +3,42 @@ const path = require('path');
const Promise = require('bluebird');
const fs = require('fs-extra');
const log = require('fancy-log');
const frontmatter = require('front-matter');
const File = require('./file');
const actions = require('./actions');
const { URL } = require('url');
const { pick, omit } = require('lodash');
const { resolve, readFile } = require('./resolve');
const { resolve, readFile, isCleanUrl, ENGINE } = require('./resolve');
const { isObject } = require('../lib/util');
const pkg = require(resolve('package.json'));
const frontmatter = require('front-matter');
/* Utility Functions **************************************************/
const MD = '.md';
const HBS = '.hbs';
const HTML = '.html';
const XML = '.xml';
const tweeturl = /https?:\/\/twitter\.com\/(?:#!\/)?(?:\w+)\/status(?:es)?\/(\d+)/i;
const tweetidcheck = /^\d+$/;
function parseTweetId (tweetid) {
// we can't trust an id that isn't a string
if (typeof tweetid !== 'string') return false;
const match = tweetid.match(tweeturl);
if (match) return match[1];
if (tweetid.match(tweetidcheck)) return tweetid;
return false;
}
module.exports = exports = class Page {
module.exports = exports = class Page extends File {
constructor (filepath) {
if (filepath && typeof filepath === 'object') {
// we've been passed a json object, treat as serialized Page
Object.assign(this, filepath);
return this;
}
super(filepath);
const file = path.parse(filepath);
const { base: basename, name, ext } = file;
this.serializable.push(
'fullurl',
'engine',
'source',
'meta',
'images',
'titlecard',
'tweets',
'dateCreated',
'dateModified',
'classes',
'flags',
);
// this file is an include, skip it.
if (name[0] === '_') return false;
var isIndexPage = (this.name === 'index');
var isClean = isCleanUrl(this.ext);
// this is not a page file
if (![ MD, HBS, HTML, XML ].includes(ext)) return false;
// remove the pages root and any _images segment from the dir
const dir = file.dir.split('/');
if (dir[0] === 'pages') dir.shift();
const i = dir.indexOf('_images');
if (i > -1) dir.splice(i, 1);
this.input = filepath; // /local/path/to/pages/file.ext
this.cwd = file.dir; // /local/path/to/pages/, pages/folder, pages/folder/subfolder
this.base = path.join(...dir); // '', 'folder', 'folder/subfolder'
this.dir = path.join('/', ...dir); // /, /folder, /folder/subfolder
this.name = name; // index, fileA, fileB
this.basename = basename; // index.ext, fileA.ext, fileB.ext
this.ext = file.ext;
var isIndexPage = (name === 'index');
var isCleanUrl = [ HBS, MD ].includes(ext);
if (isCleanUrl && isIndexPage) {
if (isClean && isIndexPage) {
this.output = path.join(this.base, 'index.html');
this.json = path.join(this.base, 'index.json');
this.url = this.dir;
} else if (isCleanUrl) {
} else if (isClean) {
this.output = path.join(this.base, this.name, 'index.html');
this.json = path.join(this.base, this.name + '.json');
this.url = path.join(this.dir, this.name);
@@ -88,23 +56,16 @@ module.exports = exports = class Page {
url.pathname = this.url;
this.fullurl = url.href;
if ([ HBS, HTML, XML ].includes(ext)) {
this.engine = 'hbs';
} else if (ext === MD) {
this.engine = 'md';
} else {
this.engine = 'raw';
}
this.engine = ENGINE[this.type] || ENGINE.COPY;
}
async load ({ Assets }) {
async load (PublicFiles) {
const [ raw, { ctime, mtime } ] = await Promise.all([
readFile(this.input).catch(() => null),
fs.stat(this.input).catch(() => ({})),
]);
const { titlecard, assets } = Assets.for(this.dir);
const { titlecard, assets } = PublicFiles.for(this.dir);
// empty file
if (!raw || !ctime) {
@@ -137,36 +98,27 @@ module.exports = exports = class Page {
return this;
}
toJson () {
const j = pick(this, [
'input',
'output',
'json',
'dateCreated',
'dateModified',
'cwd',
'base',
'dir',
'name',
'ext',
'basename',
'dest',
'out',
'url',
'fullurl',
'engine',
'source',
'images',
'assets',
'titlecard',
'tweets',
'classes',
'flags',
]);
tasks () {
if (!isObject(this.tweets)) return [];
j.meta = omit(this.meta, [ 'date', 'classes', 'tweets' ]);
return j;
return Object.values(this.tweets)
.map((t) => t.media)
.flat()
.map((m) => ({ ...m, action: actions.fetch, output: m.output }));
}
};
/* Utility Functions **************************************************/
const tweeturl = /https?:\/\/twitter\.com\/(?:#!\/)?(?:\w+)\/status(?:es)?\/(\d+)/i;
const tweetidcheck = /^\d+$/;
function parseTweetId (tweetid) {
// we can't trust an id that isn't a string
if (typeof tweetid !== 'string') return false;
const match = tweetid.match(tweeturl);
if (match) return match[1];
if (tweetid.match(tweetidcheck)) return tweetid;
return false;
}