Skip to content

Commit ea1ea32

Browse files
K-Kumar-01algomaster99
authored andcommitted
feat(markdown-docx): add variable transformer - #397 (#410)
Signed-off-by: k-kumar-01 <[email protected]>
1 parent b2a0a93 commit ea1ea32

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ function sanitizeHtmlChars(node) {
2424
return node.replace(/>/g, '&gt;').replace(/</g, '&lt;');
2525
}
2626

27+
/**
28+
* Generates a title from the variable using the title and type.
29+
*
30+
* @param {string} title Title of the variable. E.g. Receiver-1, Shipper-1
31+
* @param {string} type Type of the variable
32+
* @returns {string} New title combining title and type
33+
*/
34+
function titleGenerator(title, type) {
35+
return `${title} | ${type}`;
36+
}
37+
2738
/**
2839
* Wraps OOXML in docx headers.
2940
*
@@ -85,4 +96,4 @@ function wrapAroundDefaultDocxTags(ooxml) {
8596
return ooxml;
8697
}
8798

88-
module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags };
99+
module.exports = { sanitizeHtmlChars, titleGenerator, wrapAroundDefaultDocxTags };

packages/markdown-docx/src/CiceroMarkToOOXML/index.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE } = require('./rules');
17+
const { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE } = require('./rules');
1818
const { wrapAroundDefaultDocxTags } = require('./helpers');
1919

2020
const definedNodes = {
@@ -60,6 +60,28 @@ class CiceroMarkToOOXMLTransfomer {
6060
* @returns {string} OOXML for the given node
6161
*/
6262
getNodes(node, counter, parent = null) {
63+
if (this.getClass(node) === definedNodes.variable) {
64+
const tag = node.name;
65+
const type = node.elementType;
66+
if (Object.prototype.hasOwnProperty.call(counter, tag)) {
67+
counter = {
68+
...counter,
69+
[tag]: {
70+
...counter[tag],
71+
count: ++counter[tag].count,
72+
},
73+
};
74+
} else {
75+
counter[tag] = {
76+
count: 1,
77+
type,
78+
};
79+
}
80+
const value = node.value;
81+
const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${counter[tag].count}`;
82+
return VARIABLE_RULE(title, tag, value, type);
83+
}
84+
6385
if (this.getClass(node) === definedNodes.text) {
6486
if (parent !== null && parent.class === definedNodes.heading) {
6587
return HEADING_RULE(node.text, parent.level);
@@ -110,7 +132,7 @@ class CiceroMarkToOOXMLTransfomer {
110132
* @param {string} ooxml Initial OOXML string
111133
* @returns {string} Converted OOXML string i.e. CicecoMark->OOXML
112134
*/
113-
toOOXML(ciceromark, counter, ooxml = '') {
135+
toOOXML(ciceromark, counter = {}, ooxml = '') {
114136
this.globalOOXML = ooxml;
115137
ciceromark.nodes.forEach(node => {
116138
this.getNodes(node, counter);

packages/markdown-docx/src/CiceroMarkToOOXML/rules.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const { sanitizeHtmlChars } = require('./helpers');
17+
const { sanitizeHtmlChars, titleGenerator } = require('./helpers');
1818

1919
/**
2020
* Inserts text.
@@ -78,4 +78,35 @@ const HEADING_RULE = (value, level) => {
7878
`;
7979
};
8080

81-
module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE };
81+
/**
82+
* Inserts a variable.
83+
*
84+
* @param {string} title Title of the variable. Eg. receiver-1, shipper-1
85+
* @param {string} tag Name of the variable. Eg. receiver, shipper
86+
* @param {string} value Value of the variable
87+
* @param {string} type Type of the variable - Long, Double, etc.
88+
* @returns {string} OOXML string for the variable
89+
*/
90+
const VARIABLE_RULE = (title, tag, value, type) => {
91+
return `
92+
<w:sdt>
93+
<w:sdtPr>
94+
<w:rPr>
95+
<w:sz w:val="24"/>
96+
</w:rPr>
97+
<w:alias w:val="${titleGenerator(title, type)}"/>
98+
<w:tag w:val="${tag}"/>
99+
</w:sdtPr>
100+
<w:sdtContent>
101+
<w:r>
102+
<w:rPr>
103+
<w:sz w:val="24"/>
104+
</w:rPr>
105+
<w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t>
106+
</w:r>
107+
</w:sdtContent>
108+
</w:sdt>
109+
`;
110+
};
111+
112+
module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)