2020-02-22 20:32:51 -08:00
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const log = require('fancy-log');
|
2020-02-27 18:57:39 -08:00
|
|
|
const File = require('./file');
|
|
|
|
const actions = require('./actions');
|
2020-02-22 20:32:51 -08:00
|
|
|
const { URL } = require('url');
|
2020-02-27 18:57:39 -08:00
|
|
|
const { resolve, readFile, isCleanUrl, ENGINE } = require('./resolve');
|
2020-03-05 19:41:32 -08:00
|
|
|
const { isObject, isString } = require('./lib/util');
|
2020-02-22 20:32:51 -08:00
|
|
|
|
|
|
|
const pkg = require(resolve('package.json'));
|
2020-02-27 18:57:39 -08:00
|
|
|
const frontmatter = require('front-matter');
|
2020-02-22 20:32:51 -08:00
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
module.exports = exports = class Page extends File {
|
2020-02-22 20:32:51 -08:00
|
|
|
|
|
|
|
constructor (filepath) {
|
2020-02-27 18:57:39 -08:00
|
|
|
super(filepath);
|
2020-02-23 18:36:33 -08:00
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
this.serializable.push(
|
|
|
|
'fullurl',
|
|
|
|
'engine',
|
|
|
|
'source',
|
|
|
|
'meta',
|
|
|
|
'images',
|
|
|
|
'titlecard',
|
|
|
|
'tweets',
|
|
|
|
'dateCreated',
|
|
|
|
'dateModified',
|
|
|
|
'classes',
|
|
|
|
'flags',
|
|
|
|
);
|
2020-02-22 20:32:51 -08:00
|
|
|
|
2020-02-29 16:27:55 -08:00
|
|
|
this.engine = ENGINE[this.type] || ENGINE.COPY;
|
|
|
|
}
|
|
|
|
|
|
|
|
_out () {
|
2020-02-27 18:57:39 -08:00
|
|
|
var isIndexPage = (this.name === 'index');
|
|
|
|
var isClean = isCleanUrl(this.ext);
|
2020-02-22 20:32:51 -08:00
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
if (isClean && isIndexPage) {
|
2020-02-29 16:27:55 -08:00
|
|
|
this.out = path.join(this.base, 'index.html');
|
2020-02-25 19:37:10 -08:00
|
|
|
this.json = path.join(this.base, 'index.json');
|
|
|
|
this.url = this.dir;
|
2020-02-27 18:57:39 -08:00
|
|
|
} else if (isClean) {
|
2020-02-29 16:27:55 -08:00
|
|
|
this.out = path.join(this.base, this.name, 'index.html');
|
2020-02-25 19:37:10 -08:00
|
|
|
this.json = path.join(this.base, this.name + '.json');
|
|
|
|
this.url = path.join(this.dir, this.name);
|
2020-02-22 20:32:51 -08:00
|
|
|
} else if (isIndexPage) {
|
2020-02-29 16:27:55 -08:00
|
|
|
this.out = path.join(this.base, 'index.html');
|
2020-02-25 19:37:10 -08:00
|
|
|
this.json = path.join(this.base, this.name + '.json');
|
|
|
|
this.url = this.dir;
|
2020-02-22 20:32:51 -08:00
|
|
|
} else {
|
2020-02-29 16:27:55 -08:00
|
|
|
this.out = path.join(this.base, this.basename);
|
2020-02-25 19:37:10 -08:00
|
|
|
this.json = path.join(this.base, this.basename + '.json');
|
|
|
|
this.url = path.join(this.dir, this.basename);
|
2020-02-22 20:32:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(pkg.siteInfo.siteUrl);
|
|
|
|
url.pathname = this.url;
|
|
|
|
this.fullurl = url.href;
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
async load (PublicFiles) {
|
2020-02-23 18:36:33 -08:00
|
|
|
const [ raw, { ctime, mtime } ] = await Promise.all([
|
2020-02-25 19:37:10 -08:00
|
|
|
readFile(this.input).catch(() => null),
|
|
|
|
fs.stat(this.input).catch(() => ({})),
|
2020-02-22 20:32:51 -08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// empty file
|
|
|
|
if (!raw || !ctime) {
|
|
|
|
log.error('Could not load page: ' + this.filepath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var { attributes: meta, body } = frontmatter(raw.toString('utf8'));
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Error while parsing frontmatter for ' + this.filepath, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.source = body;
|
|
|
|
this.meta = meta;
|
|
|
|
this.dateCreated = meta.date && new Date(meta.date) || ctime;
|
|
|
|
this.dateModified = mtime;
|
|
|
|
|
2020-02-29 16:27:55 -08:00
|
|
|
this._parse(PublicFiles);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_parse (PublicFiles) {
|
|
|
|
const { titlecard, webready } = PublicFiles.for(this.dir);
|
|
|
|
|
2020-03-05 19:40:31 -08:00
|
|
|
this.ignore = this.meta.ignore;
|
2020-02-29 16:27:55 -08:00
|
|
|
this.images = webready;
|
|
|
|
this.titlecard = titlecard;
|
2020-03-05 19:41:32 -08:00
|
|
|
if (this.meta.tweets && isString(this.meta.tweets)) this.meta.tweets = this.meta.tweets.split(/\s/);
|
2020-02-29 16:27:55 -08:00
|
|
|
this.tweets = (this.meta.tweets || []).map(parseTweetId);
|
|
|
|
|
|
|
|
this.classes = Array.from(new Set(this.meta.classes || []));
|
2020-02-22 20:32:51 -08:00
|
|
|
this.flags = this.classes.reduce((res, item) => {
|
|
|
|
var camelCased = item.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
|
|
|
|
res[camelCased] = true;
|
|
|
|
return res;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
tasks () {
|
|
|
|
if (!isObject(this.tweets)) return [];
|
2020-02-22 20:32:51 -08:00
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
return Object.values(this.tweets)
|
|
|
|
.map((t) => t.media)
|
|
|
|
.flat()
|
|
|
|
.map((m) => ({ ...m, action: actions.fetch, output: m.output }));
|
2020-02-22 20:32:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2020-02-27 18:57:39 -08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|