impress.me
Version:
Create impress.js presentations from markdown documents with style
90 lines (89 loc) • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImpressMe = exports.defaultConfig = void 0;
const fs_1 = require("fs");
const helpers_1 = require("./helpers");
const markdown_1 = require("./markdown");
const position_1 = require("./position");
const strategy_1 = require("./strategy");
const shape_1 = require("./shape");
const theme_1 = require("./theme");
const path_1 = require("path");
exports.defaultConfig = {
basePath: '.',
template: 'templates/slides.pug',
cssFiles: [
'css/impress.me.scss',
'highlight.js/styles/monokai.css',
],
jsFiles: [
'impress.js/js/impress.js',
'js/navigation-ui-icons.js',
],
primary: 'default',
secondary: 'default',
theme: 'planet',
shape: shape_1.Shape.Circle,
strategy: strategy_1.Strategy.Planet,
transitionDuration: 0,
positionStrategyFactory: new position_1.PositionStrategyFactory(),
width: 1920,
height: 1080,
shapeSize: 1680,
shapeOffset: 400,
stepDistance: 1920 * 0.6,
hasInlineConfig: false,
slide: {
layout: 'default',
primary: 'default',
secondary: 'default',
},
};
function extractTheme(themeName) {
if (!(themeName in theme_1.themeMap)) {
throw new Error(`Theme "${themeName}" not found`);
}
return theme_1.themeMap[themeName];
}
class ImpressMe {
// eslint-disable-next-line no-useless-constructor
constructor(overrides = {}) {
this.overrides = overrides;
this.basePath = '.';
}
mergeConfigs(documentConfig) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, exports.defaultConfig), documentConfig), extractTheme((_b = (_a = this.overrides.theme) !== null && _a !== void 0 ? _a : documentConfig.theme) !== null && _b !== void 0 ? _b : exports.defaultConfig.theme)), this.overrides), { cssFiles: [
...exports.defaultConfig.cssFiles,
...(_c = documentConfig === null || documentConfig === void 0 ? void 0 : documentConfig.cssFiles) !== null && _c !== void 0 ? _c : [],
...(_e = (_d = this.overrides) === null || _d === void 0 ? void 0 : _d.cssFiles) !== null && _e !== void 0 ? _e : [],
].map(path => helpers_1.resolvePath(path, [this.basePath])), jsFiles: [
...exports.defaultConfig.jsFiles,
...(_f = documentConfig === null || documentConfig === void 0 ? void 0 : documentConfig.jsFiles) !== null && _f !== void 0 ? _f : [],
...(_h = (_g = this.overrides) === null || _g === void 0 ? void 0 : _g.jsFiles) !== null && _h !== void 0 ? _h : [],
].map(path => helpers_1.resolvePath(path, [this.basePath])), slide: Object.assign(Object.assign(Object.assign({}, exports.defaultConfig.slide), (_j = documentConfig === null || documentConfig === void 0 ? void 0 : documentConfig.slide) !== null && _j !== void 0 ? _j : {}), (_l = (_k = this.overrides) === null || _k === void 0 ? void 0 : _k.slide) !== null && _l !== void 0 ? _l : {}), basePath: this.basePath });
}
async convert(input, output) {
helpers_1.logInit();
const inputFile = [input, `${input}.md`].find(fs_1.existsSync);
if (inputFile === undefined) {
throw new Error('Input file not found: ' + input);
}
this.basePath = path_1.dirname(inputFile);
const outFile = output === undefined ? helpers_1.toOutputFilename(input) : output;
helpers_1.log('Input/output prepared');
const [data, documentConfig] = await helpers_1.parseInput(inputFile);
const config = this.mergeConfigs(documentConfig !== null && documentConfig !== void 0 ? documentConfig : {});
const [html, js, css] = await Promise.all([
markdown_1.markdownToHtml(data, config).then(helpers_1.logStep('Markdown converted')),
helpers_1.mergeJs(config.jsFiles).then(helpers_1.logStep('JavaScript files merged')),
helpers_1.mergeCss(config.cssFiles, helpers_1.insertCssVars(config)).then(helpers_1.logStep('CSS files merged')),
]);
const rendered = helpers_1.renderTemplate(config.template, html, js, css, config);
helpers_1.log('Template rendered');
await fs_1.promises.writeFile(outFile, rendered);
helpers_1.log(`Created "${outFile}" from "${inputFile}"`);
return outFile;
}
}
exports.ImpressMe = ImpressMe;