-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathrawXmlPlugin.ts
More file actions
47 lines (38 loc) · 1.75 KB
/
rawXmlPlugin.ts
File metadata and controls
47 lines (38 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ScopeData } from "src/compilation/scopeData";
import { Tag, TagPlacement } from "src/compilation/tag";
import { TemplateSyntaxError } from "src/errors";
import { officeMarkup } from "src/office";
import { TemplatePlugin } from "src/plugins/templatePlugin";
import { xml } from "src/xml";
import { RawXmlContent } from "./rawXmlContent";
export class RawXmlPlugin extends TemplatePlugin {
public readonly contentType = 'rawXml';
public simpleTagReplacements(tag: Tag, data: ScopeData): void {
if (tag.placement !== TagPlacement.TextNode) {
throw new TemplateSyntaxError(`RawXml tag "${tag.rawText}" must be placed in a text node but was placed in ${tag.placement}`);
}
const value = data.getScopeData<RawXmlContent>();
const replaceNode = value?.replaceParagraph ?
officeMarkup.query.containingParagraphNode(tag.xmlTextNode) :
officeMarkup.query.containingTextNode(tag.xmlTextNode);
if (
typeof value?.xml === 'string' ||
(Array.isArray(value?.xml) && value.xml.every(item => typeof item === "string"))
) {
// Parse the xml content
const xmlContent = Array.isArray(value.xml) ? value.xml.join('') : value.xml;
const wrappedXml = `<root>${xmlContent}</root>`;
const parsedRoot = xml.parser.parse(wrappedXml);
// Insert the xml content
const children = [...(parsedRoot.childNodes || [])];
for (const child of children) {
xml.modify.insertBefore(child, replaceNode);
}
}
if (value?.replaceParagraph) {
xml.modify.remove(replaceNode);
} else {
officeMarkup.modify.removeTag(tag);
}
}
}