Skip to content

Commit

Permalink
feat(markdown-docx): add thematic-break transformer - #397 (#428)
Browse files Browse the repository at this point in the history
Signed-off-by: K-Kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 authored Jul 18, 2021
1 parent 8248080 commit 3f47a12
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
47 changes: 44 additions & 3 deletions packages/markdown-docx/src/ToCiceroMarkVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ class ToCiceroMarkVisitor {
}
}

/**
* Checks if the node is a thematic break or not
*
* @param {Array} paragraphProperties paragraph styling properties
* @returns {boolean} true if the node is of type thematic break or else, false
*/
checkThematicBreakProperties(paragraphProperties) {
if (!paragraphProperties) {
return false;
}

let isBorderPresent = false;

for (const property of paragraphProperties) {
if (property.name === 'w:pBdr') {
for (const subProperty of property.elements) {
if (subProperty.name === 'w:bottom') {
const attributes = subProperty.attributes;
if (attributes['w:val'] === 'single' && attributes['w:sz'] === '6') {
isBorderPresent = true;
}
}
}
}
}

return isBorderPresent;
}

/**
* Constructs a ciceroMark Node for inline element from the information.
*
Expand Down Expand Up @@ -234,14 +263,26 @@ class ToCiceroMarkVisitor {
traverseElements(node, parent = '') {
for (const subNode of node) {
if (subNode.name === 'w:p') {
if (!subNode.elements) {
continue;
}

const { isHeading, level } = this.getHeading(
subNode.elements && subNode.elements[0].elements && subNode.elements[0].elements[0]
subNode.elements[0].elements && subNode.elements[0].elements[0]
);

if (subNode.elements) {
this.traverseElements(subNode.elements);
const isThematicBreak = this.checkThematicBreakProperties(subNode.elements[0].elements);

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

this.traverseElements(subNode.elements);

if (isHeading) {
let headingNode = {
$class: TRANSFORMED_NODES.heading,
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
20 changes: 19 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,23 @@ const CODE_PROPERTIES_RULE = () => {
`;
};

const LINEBREAK_RULE = () => {
return '<w:p/>';
};

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

module.exports = {
TEXT_RULE,
EMPHASIS_RULE,
Expand All @@ -179,5 +196,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":"First paragraph"}]},{"$class":"org.accordproject.commonmark.ThematicBreak"},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Second paragraph"}]},{"$class":"org.accordproject.commonmark.ThematicBreak"},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Third paragraph"}]}]}
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":"First paragraph"}]},{"$class":"org.accordproject.commonmark.ThematicBreak"},{"$class":"org.accordproject.commonmark.ThematicBreak"},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Third paragraph"}]}]}
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 3f47a12

Please sign in to comment.