To create a custom plugin, extend the TemplatePlugin class. You must define a unique contentType that matches the _type property in your input data.
Implement the simpleTagReplacements method to handle the logic for your specific tag type. You can use utilities from officeMarkup and xml to query and modify the document structure.
Example Implementation:
import { officeMarkup, xml } from "easy-template-x";
export class RawXmlPlugin extends TemplatePlugin {
// Declare the unique "content type" this plugin handles
public readonly contentType = 'rawXml';
public simpleTagReplacements(tag: Tag, data: ScopeData): void {
const value = data.getScopeData<RawXmlContent>();
if (value && typeof value.xml === 'string') {
// Find the actual XML text node in MS Word
const wordTextNode = officeMarkup.query.containingTextNode(tag.xmlTextNode);
// Parse and insert the new XML content
const newNode = xml.parser.parse(value.xml);
xml.modify.insertBefore(newNode, wordTextNode);
}
// Remove the placeholder tag
officeMarkup.modify.removeTag(tag);
}
}
Required Content Interface:
export interface RawXmlContent extends PluginContent {
_type: 'rawXml';
xml: string;
}
import { officeMarkup, xml } from "easy-template-x";
/**
* A plugin that inserts raw xml to the document.
*/
export class RawXmlPlugin extends TemplatePlugin {
// Declare the unique "content type" this plugin handles
public readonly contentType = 'rawXml';
// Plugin logic goes here:
public simpleTagReplacements(tag: Tag, data: ScopeData): void {
// Get the value to use from the input data.
const value = data.getScopeData<RawXmlContent>();
if (value && typeof value.xml === 'string') {
// Tag.xmlTextNode always reference the actual xml text node.
// In MS Word each text node is wrapped by a <w:t> node so we retrieve that.
const wordTextNode = officeMarkup.query.containingTextNode(tag.xmlTextNode);
// If the input data contains an "xml" string property, parse it and insert the content next to the placeholder tag.
const newNode = xml.parser.parse(value.xml);
xml.modify.insertBefore(newNode, wordTextNode);
}
// Remove the placeholder tag.
officeMarkup.modify.removeTag(tag);
}
}