@npmstuff/argdown-core
Version:
A pluggable parser for the Argdown argumentation syntax
91 lines • 3.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONExportPlugin = void 0;
const ArgdownPluginError_1 = require("../ArgdownPluginError");
const toJSON_1 = require("../model/toJSON");
const model_1 = require("../model/model");
const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep"));
const utils_1 = require("../utils");
const defaultSettings = {
spaces: 2,
removeEmbeddedRelations: false,
exportMap: true,
exportSections: true,
exportTags: true,
exportData: true
};
class JSONExportPlugin {
constructor(config) {
this.name = "JSONExportPlugin";
this.prepare = request => {
(0, utils_1.mergeDefaults)(this.getSettings(request), this.defaults);
};
this.run = (request, response) => {
(0, ArgdownPluginError_1.checkResponseFields)(this, response, [
"statements",
"arguments",
"relations"
]);
const argdown = {
arguments: response.arguments,
statements: response.statements,
relations: response.relations
};
const settings = this.getSettings(request);
const mapResponse = response;
if (settings.exportMap &&
mapResponse.map &&
mapResponse.map.nodes &&
mapResponse.map.edges) {
argdown.map = {
nodes: mapResponse.map.nodes,
edges: mapResponse.map.edges
};
}
if (settings.exportSections && response.sections) {
argdown.sections = response.sections;
}
if (settings.exportTags && response.tags) {
argdown.tags = response.tags;
}
response.json = (0, toJSON_1.stringifyArgdownData)(argdown, function (key, value) {
if (!settings.exportData && key === "data") {
return undefined;
}
if (settings.removeEmbeddedRelations &&
key === "relations" &&
value.type &&
this &&
(this.type === model_1.ArgdownTypes.ARGUMENT ||
this.types === model_1.ArgdownTypes.EQUIVALENCE_CLASS)) {
return undefined;
}
if (!settings.exportSections &&
key === "section" &&
this &&
this.type &&
(this.type === model_1.ArgdownTypes.ARGUMENT ||
this.types === model_1.ArgdownTypes.EQUIVALENCE_CLASS)) {
return undefined;
}
return value;
}, settings.spaces);
return response;
};
this.defaults = (0, lodash_defaultsdeep_1.default)({}, config, defaultSettings);
}
getSettings(request) {
if ((0, utils_1.isObject)(request.json)) {
return request.json;
}
else {
request.json = {};
return request.json;
}
}
}
exports.JSONExportPlugin = JSONExportPlugin;
//# sourceMappingURL=JSONExportPlugin.js.map