263 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-02-21 20:05:52 -08:00
const path = require('path');
const fs = require('fs-extra');
const log = require('fancy-log');
2020-03-07 18:04:37 -08:00
const { resolve, readFile, ENGINE, TYPE } = require('./resolve');
2020-02-21 20:05:52 -08:00
2020-05-13 11:45:26 -07:00
const handybars = require('handybars');
const Kit = require('handybars/kit');
2020-02-21 20:05:52 -08:00
2020-04-07 10:32:23 -07:00
const slugify = require('./lib/slugify');
2021-02-27 20:43:09 -08:00
const { stripHtml } = require('string-strip-html');
2020-02-21 20:05:52 -08:00
const markdownIt = require('markdown-it');
const markdownEngines = {
full: markdownIt({
html: true,
linkify: true,
typographer: true,
})
.enable('image')
.use(require('markdown-it-anchor'), {
permalink: true,
permalinkClass: 'header-link',
permalinkSymbol: '<img src="/images/svg/paragraph.svg">',
slugify,
})
.use(require('./lib/markdown-raw-html'), { debug: false }),
2020-02-21 20:05:52 -08:00
preview: markdownIt({
html: false,
linkify: false,
typographer: true,
})
2020-02-28 10:31:13 -08:00
.use(require('./lib/markdown-token-filter')),
2020-02-21 20:05:52 -08:00
};
2020-05-13 11:45:26 -07:00
function markdown (mode, input, data, hbs) {
2020-02-21 20:05:52 -08:00
if (mode === 'preview') {
2021-02-27 20:43:09 -08:00
input = stripHtml(input
2020-02-21 20:05:52 -08:00
.replace(/<!--\[[\s\S]*?\]-->/g, '')
.replace(/æææ[\s\S]*?æææ/gi, '')
.replace(/\{!\{([\s\S]*?)\}!\}/mg, ''),
2020-10-12 13:30:50 -07:00
).result.trim();
2020-02-21 20:05:52 -08:00
if (input.length > 1000) input = input.slice(0, 1000) + '…';
2020-02-21 20:05:52 -08:00
} else {
input = input.replace(/\{!\{([\s\S]*?)\}!\}/mg, (match, contents) => {
2020-05-13 12:29:39 -07:00
const result = hbs(contents, data);
return 'æææ' + result + 'æææ';
});
2020-02-21 20:05:52 -08:00
input = input.replace(/<!--[[\]]-->/g, '');
}
2020-03-15 16:49:03 -07:00
try {
2020-05-13 11:45:26 -07:00
return input ? markdownEngines[mode].render(input, data) : '';
2020-03-15 16:49:03 -07:00
} catch (e) {
log(input);
throw e;
}
2020-02-21 20:05:52 -08:00
}
function stripIndent (input) {
const match = input.match(/^[^\S\n]*(?=\S)/gm);
const indent = match && Math.min(...match.map((el) => el.length));
if (indent) {
const regexp = new RegExp(`^.{${indent}}`, 'gm');
input = input.replace(regexp, '');
}
return input;
}
2020-05-13 11:45:26 -07:00
const HANDYBARS_PARTIALS = {
2020-02-21 20:05:52 -08:00
layout: 'templates/layout.hbs',
2020-05-13 11:45:26 -07:00
};
const HANDYBARS_TEMPLATES = {
2020-03-07 18:04:37 -08:00
page: 'templates/page.hbs',
post: 'templates/post.hbs',
2020-02-21 20:05:52 -08:00
};
module.exports = exports = async function (prod) {
2020-05-13 11:45:26 -07:00
const revManifest = prod && await fs.readJson(resolve('rev-manifest.json')).catch(() => {}).then((r) => r || {});
const injectables = new Injectables(prod, revManifest);
const env = { ...Kit, ...injectables.helpers() };
for (const [ name, file ] of Object.entries(HANDYBARS_PARTIALS)) {
2020-02-21 20:05:52 -08:00
try {
2020-02-25 19:37:10 -08:00
const contents = await readFile(file);
2020-05-13 11:45:26 -07:00
env[name] = handybars.partial(contents.toString('utf8'));
2020-02-21 20:05:52 -08:00
} catch (e) {
2020-05-13 11:45:26 -07:00
log.error('Could not load partial ' + file, e);
2020-02-21 20:05:52 -08:00
}
}
2020-05-13 11:45:26 -07:00
const templates = {};
for (const [ name, file ] of Object.entries(HANDYBARS_TEMPLATES)) {
try {
const contents = await readFile(file);
templates[name] = handybars(contents.toString('utf8'), env);
} catch (e) {
2020-05-13 12:29:39 -07:00
log.error('Could not load template ' + file, e);
2020-05-13 11:45:26 -07:00
}
}
2020-02-21 20:05:52 -08:00
2020-05-13 11:45:26 -07:00
const hbs = (source, data) => handybars(source, env)(data);
2020-02-21 20:05:52 -08:00
const result = {
2020-05-13 12:29:39 -07:00
[TYPE.HANDYBARS]: hbs,
2020-05-13 11:45:26 -07:00
[TYPE.MARKDOWN]: (source, data) => markdown('full', source, data, hbs),
2020-03-07 18:04:37 -08:00
[TYPE.OTHER]: (source) => source,
2020-05-13 11:45:26 -07:00
[ENGINE.PAGE]: (source, data) => templates.page({ ...data, contents: markdown('full', source, data, hbs) }),
[ENGINE.POST]: (source, data) => templates.post({ ...data, contents: markdown('full', source, data, hbs) }),
[ENGINE.HTML]: (source) => source,
2020-03-07 18:04:37 -08:00
[ENGINE.OTHER]: (source) => source,
2020-05-13 11:45:26 -07:00
preview: (source, data) => markdown('preview', source, data, hbs),
2020-02-21 20:05:52 -08:00
};
return result;
};
class Injectables {
constructor (prod, revManifest) {
this.prod = prod;
this.revManifest = revManifest;
this.injections = {};
}
_parsePath (tpath, local, type) {
2020-02-25 19:37:10 -08:00
if (tpath[0] === '/') tpath = resolve(tpath.slice(1));
else if (tpath[0] === '~') tpath = resolve('templates', tpath.slice(2));
2020-02-21 20:05:52 -08:00
else tpath = path.resolve(local.cwd, tpath);
if (type && !tpath.endsWith(type)) tpath += '.' + type;
return tpath;
}
_template (tpath, make) {
2020-02-27 18:57:39 -08:00
if (!tpath) throw new Error('Received an empty template path: ' + tpath);
2020-02-21 20:05:52 -08:00
if (this.injections[tpath]) return this.injections[tpath];
if (!fs.existsSync(tpath)) {
2020-02-27 18:57:39 -08:00
throw new Error('Injectable does not exist: ' + tpath);
2020-02-21 20:05:52 -08:00
}
let contents;
try {
contents = fs.readFileSync(tpath).toString('utf8');
if (make) contents = make(contents);
this.injections[tpath] = contents;
return contents;
} catch (e) {
2020-02-25 19:37:10 -08:00
log.error(e, 'An error occured while loading the injectable: ' + tpath);
2020-02-21 20:05:52 -08:00
}
return '';
}
2020-05-13 11:45:26 -07:00
helpers () {
return {
import: this.import(),
markdown: this.markdown(),
icon: this.icon(),
prod: this.production(),
rev: this.rev(),
};
}
2020-02-21 20:05:52 -08:00
rev () {
const self = this;
return function (url) {
if (!url) return '';
if (url[0] === '/') url = url.substr(1);
if (self.prod && self.revManifest[url]) return '/' + self.revManifest[url];
return '/' + url;
};
}
production () {
const self = this;
2020-05-13 11:45:26 -07:00
return function ({ fn, inverse }) {
if (!fn) return self.prod;
return self.prod ? fn(this) : inverse && inverse(this);
2020-02-21 20:05:52 -08:00
};
}
markdown () {
const self = this;
return function (...args) {
2020-05-13 11:45:26 -07:00
const { fn, data, resolve: rval } = args.pop();
const local = rval('@root.this.local');
2020-02-21 20:05:52 -08:00
let contents;
if (fn) {
2020-03-05 19:40:18 -08:00
contents = stripIndent(fn(data.root));
2020-02-21 20:05:52 -08:00
} else {
let tpath = args.shift();
2020-05-13 11:45:26 -07:00
tpath = self._parsePath(tpath, local, 'md');
2020-02-21 20:05:52 -08:00
contents = self._template(tpath);
}
2020-05-13 11:45:26 -07:00
contents = markdown('full', contents, data, () => { throw new Error('You went too deep!'); });
2020-02-21 20:05:52 -08:00
2020-05-13 11:45:26 -07:00
return { value: contents };
2020-02-21 20:05:52 -08:00
};
}
import () {
const self = this;
return function (tpath, ...args) {
2020-05-13 11:45:26 -07:00
const { hash, env, resolve: rval } = args.pop();
const value = args.shift() || this;
2020-05-13 11:45:26 -07:00
const frame = handybars.makeContext(value, env, { hash });
const local = rval('@root.this.local');
2020-02-21 20:05:52 -08:00
2020-05-13 11:45:26 -07:00
tpath = self._parsePath(tpath, local, 'hbs');
2020-02-21 20:05:52 -08:00
try {
2020-05-13 11:45:26 -07:00
const contents = self._template(tpath, handybars.parse).evaluate(value, frame);
return handybars.safe(contents);
2020-02-21 20:05:52 -08:00
} catch (e) {
2020-02-27 18:57:39 -08:00
log.error('Could not execute import template ' + tpath, e);
2020-02-21 20:05:52 -08:00
return '';
}
};
}
icon () {
const self = this;
return function (name, ...args) {
2020-05-13 11:45:26 -07:00
const { hash, env, resolve: rval } = args.pop();
const local = rval('@root.this.local');
const tpath = path.join(local.root, 'svg', name + '.svg');
const frame = handybars.makeContext(hash, env);
2020-02-21 20:05:52 -08:00
try {
const contents = self._template(tpath, (s) =>
2020-05-13 11:45:26 -07:00
handybars(`<span class="svg-icon" {{#if size}}style="width:{{size}}px;height:{{size}}px"{{/if}}>${s}</span>`),
)(frame);
2020-02-21 20:05:52 -08:00
2020-05-13 11:45:26 -07:00
return handybars.safe(contents);
2020-02-21 20:05:52 -08:00
} catch (e) {
2020-02-27 18:57:39 -08:00
log.error('Could not execute import template ' + tpath, e);
2020-02-21 20:05:52 -08:00
return '';
}
};
}
}