diff --git a/packages/markdown-docx/src/CiceroMarkToOOXML/index.js b/packages/markdown-docx/src/CiceroMarkToOOXML/index.js index 89cac072..51f80380 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXML/index.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/index.js @@ -35,10 +35,11 @@ const definedNodes = { */ class CiceroMarkToOOXMLTransfomer { /** - * Declares the OOXML variable + * Declares the OOXML and counter variable. */ constructor() { this.globalOOXML = ''; + this.counter = {}; } /** @@ -51,34 +52,42 @@ class CiceroMarkToOOXMLTransfomer { return node.$class; } + /** + * Returns the counter holding variable count for a CiceroMark(JSON). + * + * @returns {object} Counter for variables in CiceroMark(JSON) + */ + getCounter() { + return this.counter; + } + /** * Gets the OOXML for the given node. * * @param {object} node Description of node type - * @param {object} counter Counter for different variables based on node name * @param {object} parent Parent object for a node * @returns {string} OOXML for the given node */ - getNodes(node, counter, parent = null) { + getNodes(node, parent = null) { if (this.getClass(node) === definedNodes.variable) { const tag = node.name; const type = node.elementType; - if (Object.prototype.hasOwnProperty.call(counter, tag)) { - counter = { - ...counter, + if (Object.prototype.hasOwnProperty.call(this.counter, tag)) { + this.counter = { + ...this.counter, [tag]: { - ...counter[tag], - count: ++counter[tag].count, + ...this.counter[tag], + count: ++this.counter[tag].count, }, }; } else { - counter[tag] = { + this.counter[tag] = { count: 1, type, }; } const value = node.value; - const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${counter[tag].count}`; + const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${this.counter[tag].count}`; return VARIABLE_RULE(title, tag, value, type); } @@ -96,7 +105,7 @@ class CiceroMarkToOOXMLTransfomer { if (this.getClass(node) === definedNodes.emphasize) { let ooxml = ''; node.nodes.forEach(subNode => { - ooxml += this.getNodes(subNode, counter, { class: node.$class }); + ooxml += this.getNodes(subNode, { class: node.$class }); }); return ooxml; } @@ -104,7 +113,7 @@ class CiceroMarkToOOXMLTransfomer { if (this.getClass(node) === definedNodes.heading) { let ooxml = ''; node.nodes.forEach(subNode => { - ooxml += this.getNodes(subNode, counter, { class: node.$class, level: node.level }); + ooxml += this.getNodes(subNode, { class: node.$class, level: node.level }); }); this.globalOOXML = ` ${this.globalOOXML} @@ -117,7 +126,7 @@ class CiceroMarkToOOXMLTransfomer { if (this.getClass(node) === definedNodes.paragraph) { let ooxml = ''; node.nodes.forEach(subNode => { - ooxml += this.getNodes(subNode, counter); + ooxml += this.getNodes(subNode); }); this.globalOOXML = `${this.globalOOXML}${ooxml}`; } @@ -128,14 +137,13 @@ class CiceroMarkToOOXMLTransfomer { * Transforms the given CiceroMark JSON to OOXML * * @param {Object} ciceromark CiceroMark JSON to be converted - * @param {Object} counter Counter for different variables based on node name * @param {string} ooxml Initial OOXML string * @returns {string} Converted OOXML string i.e. CicecoMark->OOXML */ - toOOXML(ciceromark, counter = {}, ooxml = '') { + toOOXML(ciceromark, ooxml = '') { this.globalOOXML = ooxml; ciceromark.nodes.forEach(node => { - this.getNodes(node, counter); + this.getNodes(node); }); this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML); return this.globalOOXML;