UNPKG

@argdown/node

Version:

Async Argdown application for node.js

99 lines 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IncludePlugin = void 0; const fs = __importStar(require("fs")); const util_1 = require("util"); const readFileAsync = (0, util_1.promisify)(fs.readFile); let path = require("path"); const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep")); const lodash_includes_1 = __importDefault(require("lodash.includes")); const core_1 = require("@argdown/core"); class IncludePlugin { constructor(config) { this.name = "IncludePlugin"; this.getSettings = (request) => { request.include = request.include || {}; return request.include; }; this.prepare = request => { (0, lodash_defaultsdeep_1.default)(this.getSettings(request), this.defaults); }; this.runAsync = async (request) => { if (!request.input) { throw new core_1.ArgdownPluginError(this.name, "missing-input-request-field", "Missing input."); } if (!request.inputPath) { throw new core_1.ArgdownPluginError(this.name, "missing-inputPath-request-field", "Missing input path."); } const settings = this.getSettings(request); settings.regEx.lastIndex = 0; request.input = await this.replaceIncludesAsync(request.inputPath, request.input, settings.regEx, []); }; this.defaults = (0, lodash_defaultsdeep_1.default)({}, config, { regEx: /@include\(([^\)]+)\)/g }); } async replaceIncludesAsync(currentFilePath, str, regEx, filesAlreadyIncluded) { let match = null; const directoryPath = path.dirname(currentFilePath); regEx.lastIndex = 0; while ((match = regEx.exec(str))) { const absoluteFilePath = path.resolve(directoryPath, match[1]); let strToInclude = ""; if ((0, lodash_includes_1.default)(filesAlreadyIncluded, absoluteFilePath)) { strToInclude = "<!-- Include failed: File '" + absoluteFilePath + "' already included. To avoid infinite loops, each file can only be included once. -->"; } else { filesAlreadyIncluded.push(absoluteFilePath); try { strToInclude = await readFileAsync(absoluteFilePath, "utf8"); } catch (e) { throw new core_1.ArgdownPluginError(this.name, "file-not-found", `'${absoluteFilePath}' not found.`); } if (strToInclude == null) { strToInclude = "<!-- Include failed: File '" + absoluteFilePath + "' not found. -->\n"; } else { strToInclude = await this.replaceIncludesAsync(absoluteFilePath, strToInclude, regEx, filesAlreadyIncluded); } } str = str.substr(0, match.index) + strToInclude + str.substr(match.index + match[0].length); regEx.lastIndex = match.index + strToInclude.length; } return str; } } exports.IncludePlugin = IncludePlugin; //# sourceMappingURL=IncludePlugin.js.map