From 8dec9d1af80954acdc2c8cdcc5c56a08325d60bb Mon Sep 17 00:00:00 2001 From: Kushal Kumar <59891164+K-Kumar-01@users.noreply.github.com> Date: Sun, 18 Jul 2021 19:55:41 +0530 Subject: [PATCH] feat(markdown-docx): add thematic-break transformer - #397 (#428) Signed-off-by: K-Kumar-01 --- .../markdown-docx/src/ToCiceroMarkVisitor.js | 47 +++++++++++++++++-- .../markdown-docx/src/ToOOXMLVisitor/index.js | 3 ++ .../markdown-docx/src/ToOOXMLVisitor/rules.js | 20 +++++++- packages/markdown-docx/src/constants.js | 1 + .../ciceroMark/alternate-thematic-break.json | 1 + .../ciceroMark/continuous-thematic-break.json | 1 + .../test/data/ciceroMark/thematic-break.json | 1 + 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 packages/markdown-docx/test/data/ciceroMark/alternate-thematic-break.json create mode 100644 packages/markdown-docx/test/data/ciceroMark/continuous-thematic-break.json create mode 100644 packages/markdown-docx/test/data/ciceroMark/thematic-break.json diff --git a/packages/markdown-docx/src/ToCiceroMarkVisitor.js b/packages/markdown-docx/src/ToCiceroMarkVisitor.js index 997c8932..a179a3d4 100644 --- a/packages/markdown-docx/src/ToCiceroMarkVisitor.js +++ b/packages/markdown-docx/src/ToCiceroMarkVisitor.js @@ -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. * @@ -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, diff --git a/packages/markdown-docx/src/ToOOXMLVisitor/index.js b/packages/markdown-docx/src/ToOOXMLVisitor/index.js index 21b69a98..6a870774 100644 --- a/packages/markdown-docx/src/ToOOXMLVisitor/index.js +++ b/packages/markdown-docx/src/ToOOXMLVisitor/index.js @@ -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'); @@ -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) { diff --git a/packages/markdown-docx/src/ToOOXMLVisitor/rules.js b/packages/markdown-docx/src/ToOOXMLVisitor/rules.js index dbc22ea4..919e18c3 100644 --- a/packages/markdown-docx/src/ToOOXMLVisitor/rules.js +++ b/packages/markdown-docx/src/ToOOXMLVisitor/rules.js @@ -169,6 +169,23 @@ const CODE_PROPERTIES_RULE = () => { `; }; +const LINEBREAK_RULE = () => { + return ''; +}; + +const THEMATICBREAK_RULE = () => { + return ` + + + + + + + + ${LINEBREAK_RULE()} + `; +}; + module.exports = { TEXT_RULE, EMPHASIS_RULE, @@ -179,5 +196,6 @@ module.exports = { VARIABLE_RULE, SOFTBREAK_RULE, STRONG_RULE, - CODE_PROPERTIES_RULE + CODE_PROPERTIES_RULE, + THEMATICBREAK_RULE, }; diff --git a/packages/markdown-docx/src/constants.js b/packages/markdown-docx/src/constants.js index fd2d48b1..84666929 100644 --- a/packages/markdown-docx/src/constants.js +++ b/packages/markdown-docx/src/constants.js @@ -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`, }; diff --git a/packages/markdown-docx/test/data/ciceroMark/alternate-thematic-break.json b/packages/markdown-docx/test/data/ciceroMark/alternate-thematic-break.json new file mode 100644 index 00000000..60e7f90b --- /dev/null +++ b/packages/markdown-docx/test/data/ciceroMark/alternate-thematic-break.json @@ -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"}]}]} \ No newline at end of file diff --git a/packages/markdown-docx/test/data/ciceroMark/continuous-thematic-break.json b/packages/markdown-docx/test/data/ciceroMark/continuous-thematic-break.json new file mode 100644 index 00000000..c3033820 --- /dev/null +++ b/packages/markdown-docx/test/data/ciceroMark/continuous-thematic-break.json @@ -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"}]}]} \ No newline at end of file diff --git a/packages/markdown-docx/test/data/ciceroMark/thematic-break.json b/packages/markdown-docx/test/data/ciceroMark/thematic-break.json new file mode 100644 index 00000000..b27fc62c --- /dev/null +++ b/packages/markdown-docx/test/data/ciceroMark/thematic-break.json @@ -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"}]}]} \ No newline at end of file