90 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-02-29 16:27:55 -08:00
const path = require('path');
const { without } = require('lodash');
2020-03-07 18:04:37 -08:00
const { resolve, isCleanUrl, TYPE, ENGINE } = require('./resolve');
2020-02-29 16:27:55 -08:00
const Page = require('./page');
2020-04-07 10:32:23 -07:00
const slugify = require('./lib/slugify');
2020-02-29 16:27:55 -08:00
const pkg = require(resolve('package.json'));
2020-04-07 10:32:32 -07:00
const POSTMATCH = /(\d{4}-\d\d-\d\d)\.\d{4}\.(\w+)/;
2020-02-29 16:27:55 -08:00
function arrayify (input) {
if (!input) return [];
if (!Array.isArray(input)) return [ input ];
return input;
}
2020-02-29 16:27:55 -08:00
module.exports = exports = class Post extends Page {
2020-03-07 18:04:37 -08:00
_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.POST;
default:
return ENGINE.OTHER;
}
}
2020-02-29 16:27:55 -08:00
_dir (dir) {
2020-04-07 10:32:32 -07:00
// if the file name matches the POSTMATCH pattern, then this needs to be /p/ file
const match = this.name.match(POSTMATCH);
2020-02-29 16:27:55 -08:00
if (match) {
2020-03-05 19:42:57 -08:00
return [ 'tweets', match[2] ];
2020-02-29 16:27:55 -08:00
}
2020-04-07 10:32:32 -07:00
dir = dir.replace(POSTMATCH, '$2').split('/');
2020-02-29 16:27:55 -08:00
dir = without(dir, 'posts', '_images');
2020-03-05 19:42:57 -08:00
dir.unshift('tweets');
2020-02-29 16:27:55 -08:00
return dir;
}
_out () {
2020-04-07 10:32:32 -07:00
var isIndexPage = (this.name === 'index' || this.name.match(POSTMATCH));
2020-02-29 16:27:55 -08:00
var isClean = isCleanUrl(this.ext);
if (isClean && isIndexPage) {
this.out = path.join(this.base, 'index.html');
this.json = path.join(this.base, 'index.json');
this.url = this.dir;
} else if (isClean) {
this.out = 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);
} else if (isIndexPage) {
this.out = path.join(this.base, 'index.html');
this.json = path.join(this.base, this.name + '.json');
this.url = this.dir;
} else {
this.out = path.join(this.base, this.basename);
this.json = path.join(this.base, this.basename + '.json');
this.url = path.join(this.dir, this.basename);
}
const url = new URL(pkg.siteInfo.siteUrl);
url.pathname = this.url;
this.fullurl = url.href;
}
2020-03-02 20:28:46 -08:00
_parse (...args) {
super._parse(...args);
this.id = this.meta.id;
2020-03-07 18:04:37 -08:00
if (!this.titlecard) this.titlecard = '/tweets/titlecard.png';
2020-03-05 19:41:48 -08:00
this.meta.tags = (this.meta.tags || []).reduce((result, tag) => {
result[slugify(tag)] = tag;
return result;
}, {});
this.meta.author = this.meta.author && arrayify(this.meta.author) || [];
2020-03-02 20:28:46 -08:00
this.classes.push('post');
}
2020-02-29 16:27:55 -08:00
};