From 79daf3c661fd20128582628948997523a29577b9 Mon Sep 17 00:00:00 2001 From: K-Kumar-01 Date: Sun, 11 Jul 2021 19:28:18 +0530 Subject: [PATCH] feat(markdown-docx): add thematic-break transformer - #397 Transformer(CiceroMark<->OOXML) Tests for the same Signed-off-by: K-Kumar-01 --- .../markdown-docx/src/ToCiceroMarkVisitor.js | 37 +++++++++++++++++++ .../markdown-docx/src/ToOOXMLVisitor/index.js | 3 ++ .../markdown-docx/src/ToOOXMLVisitor/rules.js | 15 +++++++- packages/markdown-docx/src/constants.js | 1 + .../test/data/ciceroMark/thematic-break.json | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) 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..8368f6c4 100644 --- a/packages/markdown-docx/src/ToCiceroMarkVisitor.js +++ b/packages/markdown-docx/src/ToCiceroMarkVisitor.js @@ -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. * @@ -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); } 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..725150ec 100644 --- a/packages/markdown-docx/src/ToOOXMLVisitor/rules.js +++ b/packages/markdown-docx/src/ToOOXMLVisitor/rules.js @@ -169,6 +169,18 @@ const CODE_PROPERTIES_RULE = () => { `; }; +const THEMATICBREAK_RULE = () => { + return ` + + + + + + + + `; +}; + module.exports = { TEXT_RULE, EMPHASIS_RULE, @@ -179,5 +191,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/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