packdir-cli
Version:
Packdir CLI
101 lines • 4.77 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAndNormalizeChapter = exports.validateAndNormalizeChapters = exports.validateAndNormalizeOptions = exports.normName = exports.chapterDefaults = exports.optionsDefaults = exports.chapterPredicate = exports.optionsPredicate = void 0;
const tslib_1 = require("tslib");
const diacritics_1 = require("diacritics");
const mime_1 = require("mime");
const ow_1 = tslib_1.__importDefault(require("ow"));
const chapter_xhtml_ejs_1 = tslib_1.__importDefault(require("../templates/epub2/chapter.xhtml.ejs"));
const content_opf_ejs_1 = tslib_1.__importDefault(require("../templates/epub2/content.opf.ejs"));
const toc_xhtml_ejs_1 = tslib_1.__importDefault(require("../templates/epub2/toc.xhtml.ejs"));
const chapter_xhtml_ejs_2 = tslib_1.__importDefault(require("../templates/epub3/chapter.xhtml.ejs"));
const content_opf_ejs_2 = tslib_1.__importDefault(require("../templates/epub3/content.opf.ejs"));
const toc_xhtml_ejs_2 = tslib_1.__importDefault(require("../templates/epub3/toc.xhtml.ejs"));
const template_css_1 = tslib_1.__importDefault(require("../templates/template.css"));
const toc_ncx_ejs_1 = tslib_1.__importDefault(require("../templates/toc.ncx.ejs"));
const uslug_1 = tslib_1.__importDefault(require("uslug"));
const html_1 = require("./html");
const validate_1 = require("./validate");
Object.defineProperty(exports, "chapterPredicate", { enumerable: true, get: function () { return validate_1.chapterPredicate; } });
Object.defineProperty(exports, "optionsPredicate", { enumerable: true, get: function () { return validate_1.optionsPredicate; } });
tslib_1.__exportStar(require("./html"), exports);
tslib_1.__exportStar(require("./other"), exports);
const optionsDefaults = (version = 3) => ({
description: '',
author: ['anonymous'],
publisher: 'anonymous',
tocTitle: 'Table of Contents',
tocInTOC: true,
numberChaptersInTOC: true,
prependChapterTitles: true,
date: new Date().toISOString(),
lang: "en",
css: template_css_1.default,
chapterXHTML: version === 2 ? chapter_xhtml_ejs_1.default : chapter_xhtml_ejs_2.default,
contentOPF: version === 2 ? content_opf_ejs_1.default : content_opf_ejs_2.default,
tocNCX: toc_ncx_ejs_1.default,
tocXHTML: version === 2 ? toc_xhtml_ejs_1.default : toc_xhtml_ejs_2.default,
fonts: [],
version,
fetchTimeout: 20000,
retryTimes: 3,
batchSize: 100,
ignoreFailedDownloads: false,
verbose: false,
});
exports.optionsDefaults = optionsDefaults;
const chapterDefaults = (index) => ({
title: `Chapter ${index + 1}`,
id: `item_${index}`,
url: '',
excludeFromToc: false,
beforeToc: false,
});
exports.chapterDefaults = chapterDefaults;
const normName = (name) => ow_1.default.isValid(name, ow_1.default.string) ? [name] : (name || []);
exports.normName = normName;
const validateAndNormalizeOptions = (options) => {
(0, ow_1.default)(options, 'options', validate_1.optionsPredicate);
// put defaults
const opt = {
...(0, exports.optionsDefaults)(options.version || 3),
...options,
};
opt.author = (0, exports.normName)(opt.author);
opt.fonts = opt.fonts.map(font => ({ ...font, mediaType: (0, mime_1.getType)(font.filename) }));
opt.date = new Date(opt.date).toISOString();
opt.lang = (0, diacritics_1.remove)(opt.lang);
return opt;
};
exports.validateAndNormalizeOptions = validateAndNormalizeOptions;
function validateAndNormalizeChapters(chapters) {
(0, ow_1.default)(chapters, 'content', ow_1.default.array.ofType(validate_1.chapterPredicate));
let afterTOC = false;
return chapters.map((chapter, index) => {
const ch = (0, exports.validateAndNormalizeChapter)(chapter, index);
ch.content = html_1.normalizeHTML.call(this, index, chapter.content);
if (afterTOC && ch.beforeToc)
this.warn(`Warning (content[${index}]): Got \`beforeToc=true\` after at least one \`beforeToc=false\`. Chapters will be out of order.`);
if (!ch.beforeToc)
afterTOC = true;
return ch;
});
}
exports.validateAndNormalizeChapters = validateAndNormalizeChapters;
const validateAndNormalizeChapter = (chapter, index) => {
const ch = {
...(0, exports.chapterDefaults)(index),
...chapter,
};
const slug = (0, uslug_1.default)((0, diacritics_1.remove)(ch.title));
if (!ch.filename) {
ch.filename = `${index}_${slug}.xhtml`;
}
else if (!ch.filename.endsWith('.xhtml')) {
ch.filename = `${ch.filename}.xhtml`;
}
ch.author = (0, exports.normName)(ch.author);
return ch;
};
exports.validateAndNormalizeChapter = validateAndNormalizeChapter;
//# sourceMappingURL=index.js.map
;