Skip to content

Commit db4176f

Browse files
feat(markdown-docx): add inline-code transformer - #397 (#426)
Signed-off-by: K-Kumar-01 <[email protected]> Signed-off-by: Aman Sharma <[email protected]> Co-authored-by: Aman Sharma <[email protected]>
1 parent d1adb51 commit db4176f

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

packages/markdown-docx/src/ToCiceroMarkVisitor.js

+29
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class ToCiceroMarkVisitor {
103103
elementType: nodeInformation.elementType,
104104
name: nodeInformation.name,
105105
};
106+
} else if (nodeInformation.nodeType === 'Code') {
107+
ciceroMarkNode = {
108+
$class: `${NS_PREFIX_CommonMarkModel}Code`,
109+
text: nodeInformation.value,
110+
};
106111
} else {
107112
ciceroMarkNode = {
108113
$class: `${NS_PREFIX_CommonMarkModel}Text`,
@@ -164,6 +169,12 @@ class ToCiceroMarkVisitor {
164169
...rootNode.nodes[rootNodesLength - 1].nodes,
165170
constructedNode,
166171
];
172+
} else if (commonPropertiesLength === 2) {
173+
const subNodeLength = rootNode.nodes[rootNodesLength - 1].nodes.length;
174+
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes = [
175+
...rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes,
176+
constructedNode,
177+
];
167178
}
168179
}
169180
this.JSONXML = [];
@@ -180,6 +191,8 @@ class ToCiceroMarkVisitor {
180191
fetchFormattingProperties(node, nodeInformation) {
181192
for (const runTimeNodes of node.elements) {
182193
if (runTimeNodes.name === 'w:rPr') {
194+
let colorCodePresent = false;
195+
let shadeCodePresent = false;
183196
for (let runTimeProperties of runTimeNodes.elements) {
184197
if (runTimeProperties.name === 'w:i') {
185198
nodeInformation.properties = [
@@ -191,8 +204,24 @@ class ToCiceroMarkVisitor {
191204
...nodeInformation.properties,
192205
`${NS_PREFIX_CommonMarkModel}Strong`,
193206
];
207+
} else if (runTimeProperties.name === 'w:color') {
208+
if (runTimeProperties.attributes['w:val'] === 'C7254E') {
209+
colorCodePresent = true;
210+
}
211+
} else if (runTimeProperties.name === 'w:shd') {
212+
// `w:shd` tag is used to detect the highlight colour of
213+
// the text. Semantically, w:highlight should have been
214+
// used but the latter can render fixed colors only
215+
// unlike what is needed here.
216+
// Reference: http://officeopenxml.com/WPtextShading.php.
217+
if (runTimeProperties.attributes['w:fill'] === 'F9F2F4') {
218+
shadeCodePresent = true;
219+
}
194220
}
195221
}
222+
if (colorCodePresent && shadeCodePresent) {
223+
nodeInformation.nodeType = 'Code';
224+
}
196225
} else if (runTimeNodes.name === 'w:t') {
197226
nodeInformation.value = runTimeNodes.elements ? runTimeNodes.elements[0].text : ' ';
198227
this.JSONXML = [...this.JSONXML, nodeInformation];

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

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
VARIABLE_RULE,
2525
SOFTBREAK_RULE,
2626
STRONG_RULE,
27+
CODE_PROPERTIES_RULE,
2728
} = require('./rules');
2829
const { wrapAroundDefaultDocxTags } = require('./helpers');
2930

@@ -39,6 +40,7 @@ const definedNodes = {
3940
variable: 'org.accordproject.ciceromark.Variable',
4041
emphasize: 'org.accordproject.commonmark.Emph',
4142
strong: 'org.accordproject.commonmark.Strong',
43+
code: 'org.accordproject.commonmark.Code'
4244
};
4345

4446
/**
@@ -93,6 +95,21 @@ class ToOOXMLVisitor {
9395

9496
let textValueTag = TEXT_RULE(subNode.text);
9597

98+
let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
99+
this.tags = [...this.tags, tag];
100+
} else if (this.getClass(subNode) === definedNodes.code) {
101+
let propertyTag = CODE_PROPERTIES_RULE();
102+
for (let property of properties) {
103+
if (property === definedNodes.emphasize) {
104+
propertyTag += EMPHASIS_RULE();
105+
} else if (property === definedNodes.strong) {
106+
propertyTag += STRONG_RULE();
107+
}
108+
}
109+
propertyTag = TEXT_STYLES_RULE(propertyTag);
110+
111+
let textValueTag = TEXT_RULE(subNode.text);
112+
96113
let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
97114
this.tags = [...this.tags, tag];
98115
} else if (this.getClass(subNode) === definedNodes.variable) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ const SOFTBREAK_RULE = () => {
162162
`;
163163
};
164164

165+
const CODE_PROPERTIES_RULE = () => {
166+
return `
167+
<w:color w:val="C7254E" />
168+
<w:shd w:fill="F9F2F4"/>
169+
`;
170+
};
171+
165172
module.exports = {
166173
TEXT_RULE,
167174
EMPHASIS_RULE,
@@ -172,4 +179,5 @@ module.exports = {
172179
VARIABLE_RULE,
173180
SOFTBREAK_RULE,
174181
STRONG_RULE,
182+
CODE_PROPERTIES_RULE
175183
};
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.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Trying "},{"$class":"org.accordproject.commonmark.Code","text":"inline code"},{"$class":"org.accordproject.commonmark.Text","text":". Also, doing some "},{"$class":"org.accordproject.commonmark.Strong","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"nesting "},{"$class":"org.accordproject.commonmark.Code","text":"here"},{"$class":"org.accordproject.commonmark.Text","text":" "},{"$class":"org.accordproject.commonmark.Emph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"to see if it "},{"$class":"org.accordproject.commonmark.Code","text":"works"}]}]}]}]}

0 commit comments

Comments
 (0)