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: add heading transformer - #397 #404

Merged
merged 1 commit into from
Jun 12, 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
22 changes: 18 additions & 4 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 } = require('./rules');
const { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE } = require('./rules');
const { wrapAroundDefaultDocxTags } = require('./helpers');

const definedNodes = {
Expand All @@ -34,8 +34,6 @@ const definedNodes = {
* Transforms the ciceromark to OOXML
*/
class CiceroMarkToOOXMLTransfomer {


/**
* Declares the OOXML variable
*/
Expand Down Expand Up @@ -63,6 +61,9 @@ class CiceroMarkToOOXMLTransfomer {
*/
getNodes(node, counter, parent = null) {
if (this.getClass(node) === definedNodes.text) {
if (parent !== null && parent.class === definedNodes.heading) {
return HEADING_RULE(node.text, parent.level);
}
if (parent !== null && parent.class === definedNodes.emphasize) {
return EMPHASIS_RULE(node.text, true);
} else {
Expand All @@ -78,10 +79,23 @@ class CiceroMarkToOOXMLTransfomer {
return ooxml;
}

if (this.getClass(node) === definedNodes.heading) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, counter, { class: node.$class, level: node.level });
});
this.globalOOXML = `
${this.globalOOXML}
<w:p>
${ooxml}
</w:p>
`;
}

if (this.getClass(node) === definedNodes.paragraph) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, counter,);
ooxml += this.getNodes(subNode, counter);
});
this.globalOOXML = `${this.globalOOXML}<w:p>${ooxml}</w:p>`;
}
Expand Down
16 changes: 16 additions & 0 deletions packages/markdown-docx/src/CiceroMarkToOOXML/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ describe('Perform roundtripping between CiceroMark and OOXML', () => {
const convertedObject = ooxmlTransformer.toCiceroMark(ooxml);
expect(convertedObject).to.deep.equal(textAndEmphasisCiceroMark);
});

it('should parse text and heading nodes.', async () => {
let textandHeadingCiceroMark = await fs.readFileSync(
'test/data/ciceroMark/text-and-heading.json',
'utf-8'
);
// converts from string to JSON object
textandHeadingCiceroMark = JSON.parse(textandHeadingCiceroMark);

const ciceroMarkTransformer = new CiceroMarkToOOXMLTransfomer();
const ooxml = ciceroMarkTransformer.toOOXML(textandHeadingCiceroMark);

const ooxmlTransformer = new OoxmlTransformer();
const convertedObject = ooxmlTransformer.toCiceroMark(ooxml);
expect(convertedObject).to.deep.equal(textandHeadingCiceroMark);
Comment on lines +45 to +57
Copy link
Member

Choose a reason for hiding this comment

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

The logic is similar to what you did for text and paragraph test. I think this can be reused but not in this PR. You can refactor this in a separate PR but do create an issue to keep track of it.

});
});
33 changes: 32 additions & 1 deletion packages/markdown-docx/src/CiceroMarkToOOXML/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,35 @@ const EMPHASIS_RULE = (value) => {
`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE };
/**
* Transforms the given heading node into OOXML heading.
*
* @param {string} value Text to be rendered as heading
* @param {number} level Level of heading - ranges from 1 to 6
* @returns {string} OOXMl for heading
*
*/
const HEADING_RULE = (value, level) => {
const definedLevels = {
1: { style: 'Heading1', size: 25 },
2: { style: 'Heading2', size: 20 },
3: { style: 'Heading3', size: 16 },
4: { style: 'Heading4', size: 16 },
5: { style: 'Heading5', size: 16 },
6: { style: 'Heading6', size: 16 },
};

return `
<w:pPr>
<w:pStyle w:val="${definedLevels[level].style}"/>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="${definedLevels[level].size * 2}"/>
</w:rPr>
<w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t>
</w:r>
`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE, HEADING_RULE };
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":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,"}]},{"$class":"org.accordproject.commonmark.Heading","level":"3","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Heading level 3"}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"hello there"}]},{"$class":"org.accordproject.commonmark.Heading","level":"1","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Level 1 heading"}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Some more text here."}]},{"$class":"org.accordproject.commonmark.Heading","level":"4","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Level 4"}]},{"$class":"org.accordproject.commonmark.Heading","level":"5","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"5th lvl"}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"and last but not least, a"}]},{"$class":"org.accordproject.commonmark.Heading","level":"6","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"level 6 heading"}]}]}