Skip to content

Commit

Permalink
refactor(markdown-docx): export getClass as helper - #397
Browse files Browse the repository at this point in the history
use getClass as helper instead of transformer class function

Signed-off-by: k-kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 committed Jun 18, 2021
1 parent 3c3ddb4 commit f5cdbfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
12 changes: 11 additions & 1 deletion packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ function wrapAroundDefaultDocxTags(ooxml) {
return ooxml;
}

module.exports = { sanitizeHtmlChars, titleGenerator, wrapAroundDefaultDocxTags };
/**
* Gets the class of a given CiceroMark node.
*
* @param {object} node CiceroMark node entity
* @returns {string} Class of given node
*/
function getClass(node) {
return node.$class;
}

module.exports = { sanitizeHtmlChars, titleGenerator, wrapAroundDefaultDocxTags, getClass };
24 changes: 7 additions & 17 deletions packages/markdown-docx/src/CiceroMarkToOOXML/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'use strict';

const { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE, SOFTBREAK_RULE } = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');
const { wrapAroundDefaultDocxTags, getClass } = require('./helpers');

const definedNodes = {
computedVariable: 'org.accordproject.ciceromark.ComputedVariable',
Expand All @@ -42,16 +42,6 @@ class CiceroMarkToOOXMLTransfomer {
this.counter = {};
}

/**
* Gets the class of a given CiceroMark node.
*
* @param {object} node CiceroMark node entity
* @returns {string} Class of given node
*/
getClass(node) {
return node.$class;
}

/**
* Returns the counter holding variable count for a CiceroMark(JSON).
*
Expand All @@ -69,7 +59,7 @@ class CiceroMarkToOOXMLTransfomer {
* @returns {string} OOXML for the given node
*/
getNodes(node, parent = null) {
if (this.getClass(node) === definedNodes.variable) {
if (getClass(node) === definedNodes.variable) {
const tag = node.name;
const type = node.elementType;
if (Object.prototype.hasOwnProperty.call(this.counter, tag)) {
Expand All @@ -91,7 +81,7 @@ class CiceroMarkToOOXMLTransfomer {
return VARIABLE_RULE(title, tag, value, type);
}

if (this.getClass(node) === definedNodes.text) {
if (getClass(node) === definedNodes.text) {
if (parent !== null && parent.class === definedNodes.heading) {
return HEADING_RULE(node.text, parent.level);
}
Expand All @@ -102,19 +92,19 @@ class CiceroMarkToOOXMLTransfomer {
}
}

if (this.getClass(node) === definedNodes.softbreak) {
if (getClass(node) === definedNodes.softbreak) {
return SOFTBREAK_RULE();
}

if (this.getClass(node) === definedNodes.emphasize) {
if (getClass(node) === definedNodes.emphasize) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, { class: node.$class });
});
return ooxml;
}

if (this.getClass(node) === definedNodes.heading) {
if (getClass(node) === definedNodes.heading) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, { class: node.$class, level: node.level });
Expand All @@ -127,7 +117,7 @@ class CiceroMarkToOOXMLTransfomer {
`;
}

if (this.getClass(node) === definedNodes.paragraph) {
if (getClass(node) === definedNodes.paragraph) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode);
Expand Down

0 comments on commit f5cdbfa

Please sign in to comment.