gitbook-plugin-include-codeblock
Version:
GitBook plugin for including file
53 lines (48 loc) • 1.28 kB
JavaScript
// LICENSE : MIT
;
const fs = require("fs");
import { defaultBookOptionsMap, defaultTemplateMap } from "./options.js";
/**
* Sunc file read with path check
* @param {string} path
* @return {string}
*/
export function readFileFromPath(path) {
let 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}
*/
export function getTemplateContent(kvMap) {
const t = kvMap.template;
const dt = defaultBookOptionsMap.template;
const tPath = defaultTemplateMap[t];
const dtPath = defaultTemplateMap[dt];
const isTemplateDefault = t === dt;
const isTemplatePath = tPath === undefined;
let 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);
}