140 lines
3.8 KiB
JavaScript
Raw Normal View History

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-03-07 18:04:37 -08:00
const { resolve, readFile, isCleanUrl, TYPE, ENGINE } = require('./resolve');
const { isObject, isString } = require('./lib/util');
2020-04-07 10:34:21 -07:00
const { parseTweetId } = require('./page-tweets');
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-27 18:57:39 -08:00
this.serializable.push(
'fullurl',
'engine',
'source',
'meta',
'images',
'titlecard',
'tweets',
'dateCreated',
'dateModified',
'classes',
'flags',
'siblings'
2020-02-27 18:57:39 -08:00
);
2020-02-22 20:32:51 -08:00
2020-03-07 18:04:37 -08:00
this.engine = this._engine();
}
_engine () {
switch (this.type) {
2020-05-13 11:45:26 -07:00
case TYPE.HANDYBARS:
return TYPE.HANDYBARS;
2020-03-07 18:04:37 -08:00
case TYPE.MARKDOWN:
return ENGINE.PAGE;
default:
return ENGINE.OTHER;
}
2020-02-29 16:27:55 -08:00
}
_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) {
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.input + '\n', e);
2020-02-22 20:32:51 -08:00
return false;
}
this.source = body;
2020-04-07 10:36:09 -07:00
this.meta = meta || {};
2020-02-22 20:32:51 -08:00
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) {
2020-04-07 10:36:09 -07:00
const { titlecard, webready } = this.files = PublicFiles.for(this.dir);
this.ignore = this.meta.ignore;
this.draft = this.meta.draft;
this.lang = this.lang || this.meta.lang || 'en';
2020-03-15 16:49:03 -07:00
this.siblings = this.meta.siblings;
2020-02-29 16:27:55 -08:00
this.images = webready;
this.titlecard = titlecard;
2020-04-07 10:36:09 -07:00
if (this.meta.tweets && isString(this.meta.tweets)) this.meta.tweets = this.meta.tweets.split(/\s/).filter(Boolean);
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 () {
2020-04-07 10:36:22 -07:00
const tasks = [];
if (isObject(this.tweets)) {
tasks.push(...(
Object.values(this.tweets)
.map((t) => t.media)
.flat()
.map((m) => ({ ...m, action: actions.fetch, output: m.output }))
));
}
if (this._tasks) tasks.push(...this._tasks);
return tasks;
2020-02-22 20:32:51 -08:00
}
};