Skip to content

Commit

Permalink
feat(markdown-docx): add thematic-break transformer - #397
Browse files Browse the repository at this point in the history
Transformer(CiceroMark<->OOXML)
Tests for the same

Signed-off-by: K-Kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 committed Jul 11, 2021
1 parent 8248080 commit 79daf3c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
37 changes: 37 additions & 0 deletions packages/markdown-docx/src/ToCiceroMarkVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ class ToCiceroMarkVisitor {
}
}

/**
* Checks if the node is a thematic break or not
*
* @param {Array} paragraphProperty paragraph styling properties
* @returns {boolean} Node === thematic break
*/
checkThematicBreakProperties(paragraphProperty) {
if (!paragraphProperty) {
return false;
}

if (paragraphProperty.name === 'w:pBdr') {
for (const property of paragraphProperty.elements) {
if (property.name === 'w:bottom') {
const attributes = property.attributes;
if (attributes['w:val'] === 'single' && attributes['w:sz'] === '12') {
return true;
}
}
}
}

return false;
}

/**
* Constructs a ciceroMark Node for inline element from the information.
*
Expand Down Expand Up @@ -238,6 +263,18 @@ class ToCiceroMarkVisitor {
subNode.elements && subNode.elements[0].elements && subNode.elements[0].elements[0]
);

const isThematicBreak = this.checkThematicBreakProperties(
subNode.elements && subNode.elements[0].elements && subNode.elements[0].elements[0]
);

if (isThematicBreak) {
const thematicBreakNode = {
$class: TRANSFORMED_NODES.thematicBreak,
};
this.nodes = [...this.nodes, thematicBreakNode];
continue;
}

if (subNode.elements) {
this.traverseElements(subNode.elements);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/markdown-docx/src/ToOOXMLVisitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
SOFTBREAK_RULE,
STRONG_RULE,
CODE_PROPERTIES_RULE,
THEMATICBREAK_RULE,
} = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');
const { TRANSFORMED_NODES } = require('../constants');
Expand Down Expand Up @@ -121,6 +122,8 @@ class ToOOXMLVisitor {
this.tags = [...this.tags, VARIABLE_RULE(title, tag, value, type)];
} else if (this.getClass(subNode) === TRANSFORMED_NODES.softbreak) {
this.tags = [...this.tags, SOFTBREAK_RULE()];
} else if(this.getClass(subNode) === TRANSFORMED_NODES.thematicBreak){
this.globalOOXML += THEMATICBREAK_RULE();
} else {
if (subNode.nodes) {
if (this.getClass(subNode) === TRANSFORMED_NODES.paragraph) {
Expand Down
15 changes: 14 additions & 1 deletion packages/markdown-docx/src/ToOOXMLVisitor/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ const CODE_PROPERTIES_RULE = () => {
`;
};

const THEMATICBREAK_RULE = () => {
return `
<w:p>
<w:pPr>
<w:pBdr>
<w:bottom w:val="single" w:sz="12" w:space="1" w:color="auto"/>
</w:pBdr>
</w:pPr>
</w:p>
`;
};

module.exports = {
TEXT_RULE,
EMPHASIS_RULE,
Expand All @@ -179,5 +191,6 @@ module.exports = {
VARIABLE_RULE,
SOFTBREAK_RULE,
STRONG_RULE,
CODE_PROPERTIES_RULE
CODE_PROPERTIES_RULE,
THEMATICBREAK_RULE
};
1 change: 1 addition & 0 deletions packages/markdown-docx/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const TRANSFORMED_NODES = {
softbreak: `${NS_PREFIX_CommonMarkModel}Softbreak`,
strong: `${NS_PREFIX_CommonMarkModel}Strong`,
text: `${NS_PREFIX_CommonMarkModel}Text`,
thematicBreak: `${NS_PREFIX_CommonMarkModel}ThematicBreak`,
variable: `${NS_PREFIX_CiceroMarkModel}Variable`,
};

Expand Down
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":"hello"}]},{"$class":"org.accordproject.commonmark.ThematicBreak"},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"hello"}]}]}

0 comments on commit 79daf3c

Please sign in to comment.