UNPKG

@npmstuff/argdown-core

Version:

A pluggable parser for the Argdown argumentation syntax

86 lines 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegroupPlugin = void 0; const ArgdownPluginError_1 = require("../ArgdownPluginError"); const model_1 = require("../model/model"); const utils_1 = require("../utils"); class RegroupPlugin { constructor() { this.name = "RegroupPlugin"; this.getSettings = (request) => { if ((0, utils_1.isObject)(request.group)) { return request.group; } else { request.group = {}; return request.group; } }; this.run = (request, response) => { const settings = this.getSettings(request); if (settings.regroup && settings.regroup.length) { (0, ArgdownPluginError_1.checkResponseFields)(this, response, [ "statements", "arguments", "relations" ]); response.sections = []; for (let ec of Object.values(response.statements)) { ec.section = null; } for (let a of Object.values(response.arguments)) { a.section = null; } for (let i = 0; i < settings.regroup.length; i++) { const sectionConfig = settings.regroup[i]; if (!(0, utils_1.isObject)(sectionConfig)) { continue; } const section = regroupRecursively(sectionConfig, response, 1, i); response.sections.push(section); } } }; } } exports.RegroupPlugin = RegroupPlugin; const regroupRecursively = (sectionConfig, response, sectionLevel, sectionCounter, parentSection) => { const newSection = { type: model_1.ArgdownTypes.SECTION, id: "s" + sectionCounter, level: sectionLevel, title: sectionConfig.title, tags: sectionConfig.tags, children: [], isClosed: sectionConfig.isClosed }; if (parentSection) { newSection.parent = parentSection; } sectionCounter++; if (sectionConfig.statements) { for (let statementTitle of sectionConfig.statements) { const ec = response.statements[statementTitle]; if (ec) { ec.section = newSection; } } } if (sectionConfig.arguments) { for (let argumentTitle of sectionConfig.arguments) { const ec = response.arguments[argumentTitle]; if (ec) { ec.section = newSection; } } } if (sectionConfig.children) { for (let i = 0; i < sectionConfig.children.length; i++) { const child = sectionConfig.children[i]; const childSection = regroupRecursively(child, response, sectionLevel + 1, sectionCounter + i, newSection); newSection.children.push(childSection); } } return newSection; }; //# sourceMappingURL=RegroupPlugin.js.map