Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markdown-docx): add strong transformer - #397 #417

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 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, VARIABLE_RULE, SOFTBREAK_RULE } = require('./rules');
const { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE, SOFTBREAK_RULE, STRONG_RULE } = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');

const definedNodes = {
Expand All @@ -28,6 +28,7 @@ const definedNodes = {
text: 'org.accordproject.commonmark.Text',
variable: 'org.accordproject.ciceromark.Variable',
emphasize: 'org.accordproject.commonmark.Emph',
strong: 'org.accordproject.commonmark.Strong',
};

/**
Expand Down Expand Up @@ -96,7 +97,10 @@ class CiceroMarkToOOXMLTransfomer {
return HEADING_RULE(node.text, parent.level);
}
if (parent !== null && parent.class === definedNodes.emphasize) {
return EMPHASIS_RULE(node.text, true);
return EMPHASIS_RULE(node.text);
}
if (parent !== null && parent.class === definedNodes.strong) {
return STRONG_RULE(node.text);
} else {
return TEXT_RULE(node.text);
}
Expand All @@ -106,6 +110,14 @@ class CiceroMarkToOOXMLTransfomer {
return SOFTBREAK_RULE();
}

if (this.getClass(node) === definedNodes.strong) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, { class: node.$class });
});
return ooxml;
}

if (this.getClass(node) === definedNodes.emphasize) {
let ooxml = '';
node.nodes.forEach(subNode => {
Expand Down
20 changes: 19 additions & 1 deletion packages/markdown-docx/src/CiceroMarkToOOXML/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ const EMPHASIS_RULE = (value) => {
`;
};

/**
* Inserts strong text.
*
* @param {string} value Enclosing value of the OOXML tag
* @returns {string} OOXML tag for the strong text
*/
const STRONG_RULE = value => {
return `
<w:r>
<w:rPr>
<w:b />
<w:bCs />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this tag for? <w:b> is already there. Do we need <w:bCs>` too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bCs is used for complex script.
This is the reason why I have used it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using complex scripts? All templates are in English only, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as i remember, one of the templates used some different language. The template was probably car rental. Besides, it might be possible that later templates might be created in different languages. So i guess there would be no harm keeping this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, it might be possible that later templates might be created in different languages.

Good point. In that case, we can keep it.

</w:rPr>
<w:t>${sanitizeHtmlChars(value)}</w:t>
</w:r>
`;
};

/**
* Transforms the given heading node into OOXML heading.
*
Expand Down Expand Up @@ -122,4 +140,4 @@ const SOFTBREAK_RULE = () => {
`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE, SOFTBREAK_RULE };
module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE, VARIABLE_RULE, SOFTBREAK_RULE, STRONG_RULE };
16 changes: 16 additions & 0 deletions packages/markdown-docx/src/OOXMLTransformer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ class OoxmlTransformer {
return subElement.name === 'w:i';
}
);

let strongTextFound = element.elements[0].elements.some(
subElement => {
return subElement.name === 'w:b';
}
);

if(strongTextFound){
return {
$class: `${NS_PREFIX_CommonMarkModel}Strong`,
nodes: [
...this.deserializeElements(element.elements),
],
};
}

if (emphasisedTextFound) {
return {
$class: `${NS_PREFIX_CommonMarkModel}Emph`,
Expand Down
1 change: 1 addition & 0 deletions packages/markdown-docx/test/data/ciceroMark/strong.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":"Try TemplateMark"}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"You can try "},{"$class":"org.accordproject.commonmark.Strong","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"TemplateMark"}]},{"$class":"org.accordproject.commonmark.Text","text":" here. This dingus is powered by"}]}]}