Skip to content
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,26 @@ Takes the HTML resulting from evaluating a JavaScript snippet and converts it to
`+++
```

### `XML`

Takes the literal XML resulting from evaluating a JavaScript snippet and insert it to Word directly. It is alternative way of using literalXml delimeter:

```
+++XML `
<w:p><w:r><w:t>Created by literal XML command</w:t></w:r></w:p>
`+++
```
If literal XML has more than one root, please wrap it under &lt;fragment/> root element:
```
+++XML `
<fragment>
<w:p><w:r><w:t>First paragraph</w:t></w:r></w:p>
<w:p><w:r><w:t>Second paragraph</w:t></w:r></w:p>
...
</fragment>
`+++
```

### `FOR` and `END-FOR`

Loop over a group of elements (resulting from the evaluation of a JavaScript expression):
Expand Down
43 changes: 43 additions & 0 deletions src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ObjectCommandResultError,
} from './errors';
import { logger } from './debug';
import { parseXml } from './xml';

export function newContext(options: CreateReportOptions, imageId = 0): Context {
return {
Expand Down Expand Up @@ -312,6 +313,32 @@ export async function walkTemplate(
delete ctx.pendingHtmlNode;
}

// Replace the parent `w:p` node with the xml node(s)
if (
ctx.pendingXmlNode &&
!nodeOut._fTextNode && // Flow-prevention
nodeOut._tag === 'w:p'
) {
const xmlNode = ctx.pendingXmlNode;
const parent = nodeOut._parent;
if (parent) {
xmlNode._parent = parent;
parent._children.pop();
// check for fragment node
if (!xmlNode._fTextNode && xmlNode._tag === 'fragment') {
xmlNode._children.forEach(childNode => {
parent._children.push(childNode)
})
} else {
parent._children.push(xmlNode);
}
// Prevent containing paragraph or table row from being removed
ctx.buffers['w:p'].fInsertedText = true;
ctx.buffers['w:tr'].fInsertedText = true;
}
delete ctx.pendingXmlNode;
}

// `w:tc` nodes shouldn't be left with no `w:p` children; if that's the
// case, add an empty `w:p` inside
if (
Expand Down Expand Up @@ -585,6 +612,17 @@ const processCmd: CommandProcessor = async (
if (html != null) await processHtml(ctx, html);
}

// XML <code>
} else if (cmdName === 'XML') {
if (!isLoopExploring(ctx)) {
const xml: string | undefined = await runUserJsAndGetRaw(
data,
cmdRest,
ctx
);
if (xml != null) await processXml(ctx, xml);
}

// Invalid command
} else throw new CommandSyntaxError(cmd);
return;
Expand Down Expand Up @@ -924,6 +962,11 @@ const processHtml = async (ctx: Context, data: string) => {
ctx.pendingHtmlNode = html;
};

const processXml = async (ctx: Context, data: string) => {
const xml = await parseXml(data);
ctx.pendingXmlNode = xml;
};

// ==========================================
// Helpers
// ==========================================
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export type Context = {
options: CreateReportOptions;
jsSandbox?: Object;
textRunPropsNode?: NonTextNode;
pendingXmlNode?: Node;
};

export type Images = { [id: string]: Image };
Expand Down Expand Up @@ -247,4 +248,5 @@ export const BUILT_IN_COMMANDS = [
'IMAGE',
'LINK',
'HTML',
'XML',
] as const;