UNPKG

@npmstuff/argdown-core

Version:

A pluggable parser for the Argdown argumentation syntax

69 lines 2.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SyncDotToSvgExportPlugin = void 0; const sync_1 = __importDefault(require("@aduh95/viz.js/sync")); const utils_1 = require("../utils"); const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep")); const ArgdownPluginError_1 = require("../ArgdownPluginError"); const VizJsSettings_1 = require("./VizJsSettings"); const defaultSettings = { removeProlog: true, engine: VizJsSettings_1.GraphvizEngine.DOT }; class SyncDotToSvgExportPlugin { constructor(config) { this.name = "SyncDotToSvgExportPlugin"; this.prepare = request => { (0, utils_1.mergeDefaults)(this.getSettings(request), this.defaults); }; this.run = (request, response) => { var _a, _b; const requiredResponseFields = ["dot"]; (0, ArgdownPluginError_1.checkResponseFields)(this, response, requiredResponseFields); let { engine, nop, removeProlog } = this.getSettings(request); const files = (_a = request.images) === null || _a === void 0 ? void 0 : _a.files; const settings = { engine, nop, format: "svg" }; if (files) { settings.images = Object.values(files).map(({ path, width, height }) => ({ path, width: width || 100, height: height || 100 })); } response.svg = (0, sync_1.default)(response.dot, settings); if (removeProlog) { response.svg = response.svg.replace(/<\?[ ]*xml[\S ]+?\?>[\s]*<\![ ]*DOCTYPE[\S\s]+?\.dtd\"[ ]*>/, ""); } if (request.images && request.images.convertToDataUrls && request.images.files) { for (let image of Object.values(request.images.files)) { if (image.dataUrl) { const stringToReplace = new RegExp(image.path, "g"); response.svg = (_b = response.svg) === null || _b === void 0 ? void 0 : _b.replace(stringToReplace, image.dataUrl); } } } return response; }; this.defaults = (0, lodash_defaultsdeep_1.default)({}, config, defaultSettings); } getSettings(request) { if ((0, utils_1.isObject)(request.vizJs)) { return request.vizJs; } else { request.vizJs = {}; return request.vizJs; } } } exports.SyncDotToSvgExportPlugin = SyncDotToSvgExportPlugin; //# sourceMappingURL=SyncDotToSvgExportPlugin.js.map