Skip to content

Commit

Permalink
refactor(markdown-docx): use DEFINE_NODES for nodeType and class (#427)
Browse files Browse the repository at this point in the history
Signed-off-by: K-Kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 authored Jul 9, 2021
1 parent db4176f commit 8248080
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
39 changes: 16 additions & 23 deletions packages/markdown-docx/src/ToCiceroMarkVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

const xmljs = require('xml-js');

const { NS_PREFIX_CommonMarkModel } = require('@accordproject/markdown-common').CommonMarkModel;
const { NS_PREFIX_CiceroMarkModel } = require('@accordproject/markdown-cicero').CiceroMarkModel;
const { TRANSFORMED_NODES } = require('./constants');

/**
* Transforms OOXML to CiceroMark
Expand Down Expand Up @@ -92,25 +91,25 @@ class ToCiceroMarkVisitor {
*/
constructCiceroMarkNodeJSON(nodeInformation) {
let ciceroMarkNode = {};
if (nodeInformation.nodeType === 'softbreak') {
if (nodeInformation.nodeType === TRANSFORMED_NODES.softbreak) {
ciceroMarkNode = {
$class: `${NS_PREFIX_CommonMarkModel}Softbreak`,
$class: TRANSFORMED_NODES.softbreak,
};
} else if (nodeInformation.nodeType === 'variable') {
} else if (nodeInformation.nodeType === TRANSFORMED_NODES.variable) {
ciceroMarkNode = {
$class: `${NS_PREFIX_CiceroMarkModel}Variable`,
$class: TRANSFORMED_NODES.variable,
value: nodeInformation.value,
elementType: nodeInformation.elementType,
name: nodeInformation.name,
};
} else if (nodeInformation.nodeType === 'Code') {
} else if (nodeInformation.nodeType === TRANSFORMED_NODES.code) {
ciceroMarkNode = {
$class: `${NS_PREFIX_CommonMarkModel}Code`,
$class: TRANSFORMED_NODES.code,
text: nodeInformation.value,
};
} else {
ciceroMarkNode = {
$class: `${NS_PREFIX_CommonMarkModel}Text`,
$class: TRANSFORMED_NODES.text,
text: nodeInformation.value,
};
}
Expand Down Expand Up @@ -195,15 +194,9 @@ class ToCiceroMarkVisitor {
let shadeCodePresent = false;
for (let runTimeProperties of runTimeNodes.elements) {
if (runTimeProperties.name === 'w:i') {
nodeInformation.properties = [
...nodeInformation.properties,
`${NS_PREFIX_CommonMarkModel}Emph`,
];
nodeInformation.properties = [...nodeInformation.properties, TRANSFORMED_NODES.emphasize];
} else if (runTimeProperties.name === 'w:b') {
nodeInformation.properties = [
...nodeInformation.properties,
`${NS_PREFIX_CommonMarkModel}Strong`,
];
nodeInformation.properties = [...nodeInformation.properties, TRANSFORMED_NODES.strong];
} else if (runTimeProperties.name === 'w:color') {
if (runTimeProperties.attributes['w:val'] === 'C7254E') {
colorCodePresent = true;
Expand All @@ -220,13 +213,13 @@ class ToCiceroMarkVisitor {
}
}
if (colorCodePresent && shadeCodePresent) {
nodeInformation.nodeType = 'Code';
nodeInformation.nodeType = TRANSFORMED_NODES.code;
}
} else if (runTimeNodes.name === 'w:t') {
nodeInformation.value = runTimeNodes.elements ? runTimeNodes.elements[0].text : ' ';
this.JSONXML = [...this.JSONXML, nodeInformation];
} else if (runTimeNodes.name === 'w:sym') {
nodeInformation.nodeType = 'softbreak';
nodeInformation.nodeType = TRANSFORMED_NODES.softbreak;
this.JSONXML = [...this.JSONXML, nodeInformation];
}
}
Expand All @@ -251,14 +244,14 @@ class ToCiceroMarkVisitor {

if (isHeading) {
let headingNode = {
$class: `${NS_PREFIX_CommonMarkModel}Heading`,
$class: TRANSFORMED_NODES.heading,
level,
nodes: [],
};
this.generateNodes(headingNode);
} else {
let paragraphNode = {
$class: `${NS_PREFIX_CommonMarkModel}Paragraph`,
$class: TRANSFORMED_NODES.paragraph,
nodes: [],
};
this.generateNodes(paragraphNode);
Expand All @@ -271,7 +264,7 @@ class ToCiceroMarkVisitor {
let nodeInformation = {
properties: [],
value: '',
nodeType: 'variable',
nodeType: TRANSFORMED_NODES.variable,
name: null,
elementType: null,
};
Expand Down Expand Up @@ -323,7 +316,7 @@ class ToCiceroMarkVisitor {
this.traverseElements(documentNode.elements[0].elements, 'body');

return {
$class: `${NS_PREFIX_CommonMarkModel}${'Document'}`,
$class: TRANSFORMED_NODES.document,
xmlns: 'http://commonmark.org/xml/1.0',
nodes: this.nodes,
};
Expand Down
36 changes: 11 additions & 25 deletions packages/markdown-docx/src/ToOOXMLVisitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,7 @@ const {
CODE_PROPERTIES_RULE,
} = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');

const definedNodes = {
computedVariable: 'org.accordproject.ciceromark.ComputedVariable',
heading: 'org.accordproject.commonmark.Heading',
item: 'org.accordproject.commonmark.Item',
list: 'org.accordproject.commonmark.List',
listBlock: 'org.accordproject.ciceromark.ListBlock',
paragraph: 'org.accordproject.commonmark.Paragraph',
softbreak: 'org.accordproject.commonmark.Softbreak',
text: 'org.accordproject.commonmark.Text',
variable: 'org.accordproject.ciceromark.Variable',
emphasize: 'org.accordproject.commonmark.Emph',
strong: 'org.accordproject.commonmark.Strong',
code: 'org.accordproject.commonmark.Code'
};
const { TRANSFORMED_NODES } = require('../constants');

/**
* Transforms the ciceromark to OOXML
Expand Down Expand Up @@ -80,12 +66,12 @@ class ToOOXMLVisitor {
this.traverseNodes(node.nodes, properties);
} else {
for (let subNode of node) {
if (this.getClass(subNode) === definedNodes.text) {
if (this.getClass(subNode) === TRANSFORMED_NODES.text) {
let propertyTag = '';
for (let property of properties) {
if (property === definedNodes.emphasize) {
if (property === TRANSFORMED_NODES.emphasize) {
propertyTag += EMPHASIS_RULE();
} else if (property === definedNodes.strong) {
} else if (property === TRANSFORMED_NODES.strong) {
propertyTag += STRONG_RULE();
}
}
Expand All @@ -97,12 +83,12 @@ class ToOOXMLVisitor {

let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
this.tags = [...this.tags, tag];
} else if (this.getClass(subNode) === definedNodes.code) {
} else if (this.getClass(subNode) === TRANSFORMED_NODES.code) {
let propertyTag = CODE_PROPERTIES_RULE();
for (let property of properties) {
if (property === definedNodes.emphasize) {
if (property === TRANSFORMED_NODES.emphasize) {
propertyTag += EMPHASIS_RULE();
} else if (property === definedNodes.strong) {
} else if (property === TRANSFORMED_NODES.strong) {
propertyTag += STRONG_RULE();
}
}
Expand All @@ -112,7 +98,7 @@ class ToOOXMLVisitor {

let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
this.tags = [...this.tags, tag];
} else if (this.getClass(subNode) === definedNodes.variable) {
} else if (this.getClass(subNode) === TRANSFORMED_NODES.variable) {
const tag = subNode.name;
const type = subNode.elementType;
if (Object.prototype.hasOwnProperty.call(this.counter, tag)) {
Expand All @@ -133,11 +119,11 @@ class ToOOXMLVisitor {
const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${this.counter[tag].count}`;

this.tags = [...this.tags, VARIABLE_RULE(title, tag, value, type)];
} else if (this.getClass(subNode) === definedNodes.softbreak) {
} else if (this.getClass(subNode) === TRANSFORMED_NODES.softbreak) {
this.tags = [...this.tags, SOFTBREAK_RULE()];
} else {
if (subNode.nodes) {
if (this.getClass(subNode) === definedNodes.paragraph) {
if (this.getClass(subNode) === TRANSFORMED_NODES.paragraph) {
this.traverseNodes(subNode.nodes, properties);
let ooxml = '';
for (let xmlTag of this.tags) {
Expand All @@ -148,7 +134,7 @@ class ToOOXMLVisitor {
this.globalOOXML += ooxml;
// Clear all the tags as all nodes of paragraph have been traversed.
this.tags = [];
} else if (this.getClass(subNode) === definedNodes.heading) {
} else if (this.getClass(subNode) === TRANSFORMED_NODES.heading) {
this.traverseNodes(subNode.nodes, properties);
let ooxml = '';
for (let xmlTag of this.tags) {
Expand Down
34 changes: 34 additions & 0 deletions packages/markdown-docx/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const { NS_PREFIX_CommonMarkModel } = require('@accordproject/markdown-common').CommonMarkModel;
const { NS_PREFIX_CiceroMarkModel } = require('@accordproject/markdown-cicero').CiceroMarkModel;

const TRANSFORMED_NODES = {
code: `${NS_PREFIX_CommonMarkModel}Code`,
computedVariable: `${NS_PREFIX_CiceroMarkModel}ComputedVariable`,
document: `${NS_PREFIX_CommonMarkModel}Document`,
emphasize: `${NS_PREFIX_CommonMarkModel}Emph`,
heading: `${NS_PREFIX_CommonMarkModel}Heading`,
item: `${NS_PREFIX_CommonMarkModel}Item`,
paragraph: `${NS_PREFIX_CommonMarkModel}Paragraph`,
softbreak: `${NS_PREFIX_CommonMarkModel}Softbreak`,
strong: `${NS_PREFIX_CommonMarkModel}Strong`,
text: `${NS_PREFIX_CommonMarkModel}Text`,
variable: `${NS_PREFIX_CiceroMarkModel}Variable`,
};

module.exports = { TRANSFORMED_NODES };

0 comments on commit 8248080

Please sign in to comment.