Skip to content

Commit

Permalink
feat(markdown-docx): add variable transformer - #397
Browse files Browse the repository at this point in the history
Add helpers and rule
Add the transformation logic
Add the test for above

Signed-off-by: k-kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 committed Jun 16, 2021
1 parent 5c6cf43 commit 9e9759f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
13 changes: 12 additions & 1 deletion packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ function sanitizeHtmlChars(node) {
return node.replace(/>/g, '&gt;').replace(/</g, '&lt;');
}

/**
* Generates a title from the variable using the title and type.
*
* @param {string} title Title of the variable. E.g. Receiver-1, Shipper-1
* @param {string} type Type of the variable
* @returns {string} New title combining title and type
*/
function titleGenerator(title, type) {
return `${title} | ${type}`;
}

/**
* Wraps OOXML in docx headers.
*
Expand Down Expand Up @@ -85,4 +96,4 @@ function wrapAroundDefaultDocxTags(ooxml) {
return ooxml;
}

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

'use strict';

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

const definedNodes = {
Expand Down Expand Up @@ -60,6 +60,28 @@ class CiceroMarkToOOXMLTransfomer {
* @returns {string} OOXML for the given node
*/
getNodes(node, counter, 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,
[tag]: {
...counter[tag],
count: ++counter[tag].count,
},
};
} else {
counter[tag] = {
count: 1,
type,
};
}
const value = node.value;
const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${counter[tag].count}`;
return VARIABLE_RULE(title, tag, value, type);
}

if (this.getClass(node) === definedNodes.text) {
if (parent !== null && parent.class === definedNodes.heading) {
return HEADING_RULE(node.text, parent.level);
Expand Down Expand Up @@ -110,7 +132,7 @@ class CiceroMarkToOOXMLTransfomer {
* @param {string} ooxml Initial OOXML string
* @returns {string} Converted OOXML string i.e. CicecoMark->OOXML
*/
toOOXML(ciceromark, counter, ooxml = '') {
toOOXML(ciceromark, counter = {}, ooxml = '') {
this.globalOOXML = ooxml;
ciceromark.nodes.forEach(node => {
this.getNodes(node, counter);
Expand Down
39 changes: 37 additions & 2 deletions packages/markdown-docx/src/CiceroMarkToOOXML/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const { sanitizeHtmlChars } = require('./helpers');
const { sanitizeHtmlChars, titleGenerator } = require('./helpers');

/**
* Inserts text.
Expand Down Expand Up @@ -78,4 +78,39 @@ const HEADING_RULE = (value, level) => {
`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE };
/**
* Inserts a variable.
*
* @param {string} title Title of the variable. Eg. receiver-1, shipper-1
* @param {string} tag Name of the variable. Eg. receiver, shipper
* @param {string} value Value of the variable
* @param {string} type Type of the variable - Long, Double, etc.
* @returns {string} OOXML string for the variable
*/
const VARIABLE_RULE = (title, tag, value, type) => {
return `
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:color w:val="000000"/>
<w:sz w:val="24"/>
<w:highlight w:val="green"/>
</w:rPr>
<w:alias w:val="${titleGenerator(title, type)}"/>
<w:tag w:val="${tag}"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:rPr>
<w:color w:val="000000"/>
<w:sz w:val="24"/>
<w:highlight w:val="green"/>
</w:rPr>
<w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE };
1 change: 1 addition & 0 deletions packages/markdown-docx/test/data/ciceroMark/variable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$class":"org.accordproject.commonmark.Document","xmlns":"http://commonmark.org/xml/1.0","nodes":[{"$class":"org.accordproject.commonmark.Heading","level":"2","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Acceptance of Delivery."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party A\"","name":"shipper","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" will be deemed to have completed its delivery obligations"},{"$class":"org.accordproject.commonmark.Text","text":"if in "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party B\"","name":"receiver","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":"'s opinion, the "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Widgets\"","name":"deliverable","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":" satisfies the"},{"$class":"org.accordproject.commonmark.Text","text":"Acceptance Criteria, and "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party B\"","name":"receiver","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" notifies "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party A\"","name":"shipper","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" in writing"},{"$class":"org.accordproject.commonmark.Text","text":"that it is accepting the "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Widgets\"","name":"deliverable","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":"."}]},{"$class":"org.accordproject.commonmark.Heading","level":"2","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Inspection and Notice."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party B\"","name":"receiver","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" will have "},{"$class":"org.accordproject.ciceromark.Variable","value":"10","name":"businessDays","elementType":"Long"},{"$class":"org.accordproject.commonmark.Text","text":" Business Days to inspect and"},{"$class":"org.accordproject.commonmark.Text","text":"evaluate the "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Widgets\"","name":"deliverable","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":" on the delivery date before notifying"},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party A\"","name":"shipper","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" that it is either accepting or rejecting the"},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Widgets\"","name":"deliverable","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":"."}]},{"$class":"org.accordproject.commonmark.Heading","level":"2","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Acceptance Criteria."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"The \"Acceptance Criteria\" are the specifications the "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Widgets\"","name":"deliverable","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":"must meet for the "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Party A\"","name":"shipper","elementType":"org.accordproject.organization.Organization"},{"$class":"org.accordproject.commonmark.Text","text":" to comply with its requirements and"},{"$class":"org.accordproject.commonmark.Text","text":"obligations under this agreement, detailed in "},{"$class":"org.accordproject.ciceromark.Variable","value":"\"Attachment X\"","name":"attachment","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":", attached"},{"$class":"org.accordproject.commonmark.Text","text":"to this agreement."}]}]}

0 comments on commit 9e9759f

Please sign in to comment.