easy-template-x 7.0.3 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -615,7 +615,7 @@ Input data:
615
615
  {
616
616
  "Dont worry be happy": {
617
617
  _type: 'rawXml',
618
- xml: '<w:sym w:font="Wingdings" w:char="F04A"/>',
618
+ xml: '<w:sym w:font="Wingdings" w:char="F04A"/>', // string | string[]
619
619
  replaceParagraph: false, // Optional - should the plugin replace an entire paragraph or just the tag itself
620
620
  }
621
621
  }
@@ -633,7 +633,7 @@ make it easier to do the actual xml modification.
633
633
 
634
634
  _To better understand the internal structure of Word documents check out [this excellent source](http://officeopenxml.com/WPcontentOverview.php)._
635
635
 
636
- Example plugin implementation ([source](./src/plugins/rawXml/rawXmlPlugin.ts)):
636
+ Example plugin implementation (this is a simplified example of the [RawXmlPlugin](#raw-xml-plugin) - read the full source [here](./src/plugins/rawXml/rawXmlPlugin.ts)):
637
637
 
638
638
  ```typescript
639
639
  import { officeMarkup, xml } from "easy-template-x";
@@ -3757,9 +3757,17 @@ class RawXmlPlugin extends TemplatePlugin {
3757
3757
  }
3758
3758
  const value = data.getScopeData();
3759
3759
  const replaceNode = value?.replaceParagraph ? officeMarkup.query.containingParagraphNode(tag.xmlTextNode) : officeMarkup.query.containingTextNode(tag.xmlTextNode);
3760
- if (typeof value?.xml === 'string') {
3761
- const newNode = xml.parser.parse(value.xml);
3762
- xml.modify.insertBefore(newNode, replaceNode);
3760
+ if (typeof value?.xml === 'string' || Array.isArray(value?.xml) && value.xml.every(item => typeof item === "string")) {
3761
+ // Parse the xml content
3762
+ const xmlContent = Array.isArray(value.xml) ? value.xml.join('') : value.xml;
3763
+ const wrappedXml = `<root>${xmlContent}</root>`;
3764
+ const parsedRoot = xml.parser.parse(wrappedXml);
3765
+
3766
+ // Insert the xml content
3767
+ const children = [...(parsedRoot.childNodes || [])];
3768
+ for (const child of children) {
3769
+ xml.modify.insertBefore(child, replaceNode);
3770
+ }
3763
3771
  }
3764
3772
  if (value?.replaceParagraph) {
3765
3773
  xml.modify.remove(replaceNode);
@@ -3828,7 +3836,7 @@ class TextPlugin extends TemplatePlugin {
3828
3836
  const namespace = runNode.nodeName.split(':')[0];
3829
3837
 
3830
3838
  // First line
3831
- if (lines[0]) {
3839
+ if (typeof lines[0] === 'string') {
3832
3840
  textNode.textContent = lines[0];
3833
3841
  }
3834
3842
 
@@ -5236,7 +5244,7 @@ class TemplateHandler {
5236
5244
  /**
5237
5245
  * Version number of the `easy-template-x` library.
5238
5246
  */
5239
- version = "7.0.3" ;
5247
+ version = "7.1.0" ;
5240
5248
  constructor(options) {
5241
5249
  this.options = new TemplateHandlerOptions(options);
5242
5250
  const delimiters = this.options.delimiters;
@@ -3755,9 +3755,17 @@ class RawXmlPlugin extends TemplatePlugin {
3755
3755
  }
3756
3756
  const value = data.getScopeData();
3757
3757
  const replaceNode = value?.replaceParagraph ? officeMarkup.query.containingParagraphNode(tag.xmlTextNode) : officeMarkup.query.containingTextNode(tag.xmlTextNode);
3758
- if (typeof value?.xml === 'string') {
3759
- const newNode = xml.parser.parse(value.xml);
3760
- xml.modify.insertBefore(newNode, replaceNode);
3758
+ if (typeof value?.xml === 'string' || Array.isArray(value?.xml) && value.xml.every(item => typeof item === "string")) {
3759
+ // Parse the xml content
3760
+ const xmlContent = Array.isArray(value.xml) ? value.xml.join('') : value.xml;
3761
+ const wrappedXml = `<root>${xmlContent}</root>`;
3762
+ const parsedRoot = xml.parser.parse(wrappedXml);
3763
+
3764
+ // Insert the xml content
3765
+ const children = [...(parsedRoot.childNodes || [])];
3766
+ for (const child of children) {
3767
+ xml.modify.insertBefore(child, replaceNode);
3768
+ }
3761
3769
  }
3762
3770
  if (value?.replaceParagraph) {
3763
3771
  xml.modify.remove(replaceNode);
@@ -3826,7 +3834,7 @@ class TextPlugin extends TemplatePlugin {
3826
3834
  const namespace = runNode.nodeName.split(':')[0];
3827
3835
 
3828
3836
  // First line
3829
- if (lines[0]) {
3837
+ if (typeof lines[0] === 'string') {
3830
3838
  textNode.textContent = lines[0];
3831
3839
  }
3832
3840
 
@@ -5234,7 +5242,7 @@ class TemplateHandler {
5234
5242
  /**
5235
5243
  * Version number of the `easy-template-x` library.
5236
5244
  */
5237
- version = "7.0.3" ;
5245
+ version = "7.1.0" ;
5238
5246
  constructor(options) {
5239
5247
  this.options = new TemplateHandlerOptions(options);
5240
5248
  const delimiters = this.options.delimiters;
@@ -1,6 +1,6 @@
1
1
  import { PluginContent } from '../pluginContent';
2
2
  export interface RawXmlContent extends PluginContent {
3
3
  _type: 'rawXml';
4
- xml: string;
4
+ xml: string | string[];
5
5
  replaceParagraph?: boolean;
6
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-template-x",
3
- "version": "7.0.3",
3
+ "version": "7.1.1",
4
4
  "description": "Generate docx documents from templates, in Node or in the browser.",
5
5
  "keywords": [
6
6
  "docx",
@@ -2,7 +2,7 @@ import { PluginContent } from '../pluginContent';
2
2
 
3
3
  export interface RawXmlContent extends PluginContent {
4
4
  _type: 'rawXml';
5
- xml: string;
5
+ xml: string | string[];
6
6
  /**
7
7
  * Replace a part of the document with raw xml content.
8
8
  * If set to `true` the plugin will replace the parent paragraph (<w:p>) of
@@ -22,9 +22,20 @@ export class RawXmlPlugin extends TemplatePlugin {
22
22
  officeMarkup.query.containingParagraphNode(tag.xmlTextNode) :
23
23
  officeMarkup.query.containingTextNode(tag.xmlTextNode);
24
24
 
25
- if (typeof value?.xml === 'string') {
26
- const newNode = xml.parser.parse(value.xml);
27
- xml.modify.insertBefore(newNode, replaceNode);
25
+ if (
26
+ typeof value?.xml === 'string' ||
27
+ (Array.isArray(value?.xml) && value.xml.every(item => typeof item === "string"))
28
+ ) {
29
+ // Parse the xml content
30
+ const xmlContent = Array.isArray(value.xml) ? value.xml.join('') : value.xml;
31
+ const wrappedXml = `<root>${xmlContent}</root>`;
32
+ const parsedRoot = xml.parser.parse(wrappedXml);
33
+
34
+ // Insert the xml content
35
+ const children = [...(parsedRoot.childNodes || [])];
36
+ for (const child of children) {
37
+ xml.modify.insertBefore(child, replaceNode);
38
+ }
28
39
  }
29
40
 
30
41
  if (value?.replaceParagraph) {
@@ -4,7 +4,7 @@ import { TemplateSyntaxError } from "src/errors";
4
4
  import { officeMarkup } from "src/office";
5
5
  import { TemplatePlugin } from "src/plugins/templatePlugin";
6
6
  import { stringValue } from "src/utils";
7
- import { xml, XmlNode, XmlTextNode } from "src/xml";
7
+ import { xml, XmlNode } from "src/xml";
8
8
 
9
9
  export const TEXT_CONTENT_TYPE = 'text';
10
10
 
@@ -22,7 +22,7 @@ export class TextPlugin extends TemplatePlugin {
22
22
 
23
23
  if (tag.placement === TagPlacement.TextNode) {
24
24
  this.replaceInTextNode(tag, strValue);
25
- return;
25
+ return;
26
26
  }
27
27
 
28
28
  if (tag.placement === TagPlacement.Attribute) {
@@ -40,7 +40,7 @@ export class TextPlugin extends TemplatePlugin {
40
40
  if (lines.length < 2) {
41
41
  this.replaceSingleLine(tag, lines.length ? lines[0] : '');
42
42
  } else {
43
- this.replaceMultiLine(tag.xmlTextNode, lines);
43
+ this.replaceMultiLine(tag, lines);
44
44
  }
45
45
  }
46
46
 
@@ -73,15 +73,15 @@ export class TextPlugin extends TemplatePlugin {
73
73
  officeMarkup.modify.setSpacePreserveAttribute(wordTextNode);
74
74
  }
75
75
 
76
- private replaceMultiLine(textNode: XmlTextNode, lines: string[]) {
76
+ private replaceMultiLine(tag: TextNodeTag, lines: string[]) {
77
77
 
78
+ const textNode = tag.xmlTextNode;
78
79
  const runNode = officeMarkup.query.containingRunNode(textNode);
79
80
  const namespace = runNode.nodeName.split(':')[0];
80
81
 
81
82
  // First line
82
- if (lines[0]) {
83
- textNode.textContent = lines[0];
84
- }
83
+ const firstLine = lines[0];
84
+ textNode.textContent = firstLine;
85
85
 
86
86
  // Other lines
87
87
  for (let i = 1; i < lines.length; i++) {
@@ -96,6 +96,11 @@ export class TextPlugin extends TemplatePlugin {
96
96
  xml.modify.appendChild(runNode, lineNode);
97
97
  }
98
98
  }
99
+
100
+ // Clean up if the original text node is now empty
101
+ if (!firstLine) {
102
+ officeMarkup.modify.removeTag(tag);
103
+ }
99
104
  }
100
105
 
101
106
  private getLineBreak(namespace: string): XmlNode {