gitbook-plugin-include-codeblock
Version:
GitBook plugin for including file
63 lines (53 loc) • 1.35 kB
JavaScript
// LICENSE : MIT
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTemplateContent = getTemplateContent;
exports.readFileFromPath = readFileFromPath;
var _options = require("./options.js");
var fs = require("fs");
/**
* Sunc file read with path check
* @param {string} path
* @return {string}
*/
function readFileFromPath(path) {
var content;
try {
content = fs.readFileSync(path, "utf8");
} catch (err) {
if (err.code === "ENOENT") {
console.warn("Error: file not found: " + path);
return "Error: file not found: " + path;
} else {
throw err;
}
}
return content;
}
/**
* Load template from template label
* @param {object} kvMap
* @return {string}
*/
function getTemplateContent(kvMap) {
var t = kvMap.template;
var dt = _options.defaultBookOptionsMap.template;
var tPath = _options.defaultTemplateMap[t];
var dtPath = _options.defaultTemplateMap[dt];
var isTemplateDefault = t === dt;
var isTemplatePath = tPath === undefined;
var p; // No template option.
if (isTemplateDefault) {
p = dtPath;
} else if (isTemplatePath) {
// Template option is a path.
p = t;
} else {
// Template option one of template/ directory.
p = tPath || dtPath;
}
return readFileFromPath(p);
}
//# sourceMappingURL=template.js.map