Skip to content

Commit

Permalink
feat(markdown-docx): add inline-code transformer - #397 (#426)
Browse files Browse the repository at this point in the history
Signed-off-by: K-Kumar-01 <[email protected]>

Signed-off-by: Aman Sharma <[email protected]>

Co-authored-by: Aman Sharma <[email protected]>
  • Loading branch information
K-Kumar-01 and algomaster99 committed Jul 8, 2021
1 parent d1adb51 commit db4176f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/markdown-docx/src/ToCiceroMarkVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class ToCiceroMarkVisitor {
elementType: nodeInformation.elementType,
name: nodeInformation.name,
};
} else if (nodeInformation.nodeType === 'Code') {
ciceroMarkNode = {
$class: `${NS_PREFIX_CommonMarkModel}Code`,
text: nodeInformation.value,
};
} else {
ciceroMarkNode = {
$class: `${NS_PREFIX_CommonMarkModel}Text`,
Expand Down Expand Up @@ -164,6 +169,12 @@ class ToCiceroMarkVisitor {
...rootNode.nodes[rootNodesLength - 1].nodes,
constructedNode,
];
} else if (commonPropertiesLength === 2) {
const subNodeLength = rootNode.nodes[rootNodesLength - 1].nodes.length;
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes = [
...rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes,
constructedNode,
];
}
}
this.JSONXML = [];
Expand All @@ -180,6 +191,8 @@ class ToCiceroMarkVisitor {
fetchFormattingProperties(node, nodeInformation) {
for (const runTimeNodes of node.elements) {
if (runTimeNodes.name === 'w:rPr') {
let colorCodePresent = false;
let shadeCodePresent = false;
for (let runTimeProperties of runTimeNodes.elements) {
if (runTimeProperties.name === 'w:i') {
nodeInformation.properties = [
Expand All @@ -191,8 +204,24 @@ class ToCiceroMarkVisitor {
...nodeInformation.properties,
`${NS_PREFIX_CommonMarkModel}Strong`,
];
} else if (runTimeProperties.name === 'w:color') {
if (runTimeProperties.attributes['w:val'] === 'C7254E') {
colorCodePresent = true;
}
} else if (runTimeProperties.name === 'w:shd') {
// `w:shd` tag is used to detect the highlight colour of
// the text. Semantically, w:highlight should have been
// used but the latter can render fixed colors only
// unlike what is needed here.
// Reference: http://officeopenxml.com/WPtextShading.php.
if (runTimeProperties.attributes['w:fill'] === 'F9F2F4') {
shadeCodePresent = true;
}
}
}
if (colorCodePresent && shadeCodePresent) {
nodeInformation.nodeType = 'Code';
}
} else if (runTimeNodes.name === 'w:t') {
nodeInformation.value = runTimeNodes.elements ? runTimeNodes.elements[0].text : ' ';
this.JSONXML = [...this.JSONXML, nodeInformation];
Expand Down
17 changes: 17 additions & 0 deletions packages/markdown-docx/src/ToOOXMLVisitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
VARIABLE_RULE,
SOFTBREAK_RULE,
STRONG_RULE,
CODE_PROPERTIES_RULE,
} = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');

Expand All @@ -39,6 +40,7 @@ const definedNodes = {
variable: 'org.accordproject.ciceromark.Variable',
emphasize: 'org.accordproject.commonmark.Emph',
strong: 'org.accordproject.commonmark.Strong',
code: 'org.accordproject.commonmark.Code'
};

/**
Expand Down Expand Up @@ -93,6 +95,21 @@ class ToOOXMLVisitor {

let textValueTag = TEXT_RULE(subNode.text);

let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
this.tags = [...this.tags, tag];
} else if (this.getClass(subNode) === definedNodes.code) {
let propertyTag = CODE_PROPERTIES_RULE();
for (let property of properties) {
if (property === definedNodes.emphasize) {
propertyTag += EMPHASIS_RULE();
} else if (property === definedNodes.strong) {
propertyTag += STRONG_RULE();
}
}
propertyTag = TEXT_STYLES_RULE(propertyTag);

let textValueTag = TEXT_RULE(subNode.text);

let tag = TEXT_WRAPPER_RULE(propertyTag, textValueTag);
this.tags = [...this.tags, tag];
} else if (this.getClass(subNode) === definedNodes.variable) {
Expand Down
8 changes: 8 additions & 0 deletions packages/markdown-docx/src/ToOOXMLVisitor/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ const SOFTBREAK_RULE = () => {
`;
};

const CODE_PROPERTIES_RULE = () => {
return `
<w:color w:val="C7254E" />
<w:shd w:fill="F9F2F4"/>
`;
};

module.exports = {
TEXT_RULE,
EMPHASIS_RULE,
Expand All @@ -172,4 +179,5 @@ module.exports = {
VARIABLE_RULE,
SOFTBREAK_RULE,
STRONG_RULE,
CODE_PROPERTIES_RULE
};
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.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 comments on commit db4176f

Please sign in to comment.