Merge pull request #2053 from dolanmiu/feat/fix-templating
#2028 - Null check
This commit is contained in:
29
demo/89-template-document.ts
Normal file
29
demo/89-template-document.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Patch a document with patches
|
||||||
|
// Import from 'docx' rather than '../build' if you install from npm
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { IPatch, patchDocument, PatchType, TextRun } from "../build";
|
||||||
|
|
||||||
|
export const font = "Trebuchet MS";
|
||||||
|
export const getPatches = (fields: { [key: string]: string }) => {
|
||||||
|
const patches: { [key: string]: IPatch } = {};
|
||||||
|
|
||||||
|
for (const field in fields) {
|
||||||
|
patches[field] = {
|
||||||
|
type: PatchType.PARAGRAPH,
|
||||||
|
children: [new TextRun({ text: fields[field], font })],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return patches;
|
||||||
|
};
|
||||||
|
|
||||||
|
const patches = getPatches({
|
||||||
|
salutation: "Mr.",
|
||||||
|
"first-name": "John",
|
||||||
|
});
|
||||||
|
|
||||||
|
patchDocument(fs.readFileSync("demo/assets/simple-template-3.docx"), {
|
||||||
|
patches,
|
||||||
|
}).then((doc) => {
|
||||||
|
fs.writeFileSync("My Document.docx", doc);
|
||||||
|
});
|
BIN
demo/assets/simple-template-3.docx
Normal file
BIN
demo/assets/simple-template-3.docx
Normal file
Binary file not shown.
@ -86,6 +86,59 @@ describe("paragraph-split-inject", () => {
|
|||||||
),
|
),
|
||||||
).to.throw();
|
).to.throw();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should continue if text run doesn't have text", () => {
|
||||||
|
expect(() =>
|
||||||
|
findRunElementIndexWithToken(
|
||||||
|
{
|
||||||
|
name: "w:p",
|
||||||
|
type: "element",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
name: "w:r",
|
||||||
|
type: "element",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
name: "w:t",
|
||||||
|
type: "element",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"hello",
|
||||||
|
),
|
||||||
|
).to.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should continue if text run doesn't have text", () => {
|
||||||
|
expect(() =>
|
||||||
|
findRunElementIndexWithToken(
|
||||||
|
{
|
||||||
|
name: "w:p",
|
||||||
|
type: "element",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
name: "w:r",
|
||||||
|
type: "element",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
name: "w:t",
|
||||||
|
type: "element",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"hello",
|
||||||
|
),
|
||||||
|
).to.throw();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("splitRunElement", () => {
|
describe("splitRunElement", () => {
|
||||||
|
@ -8,7 +8,11 @@ export const findRunElementIndexWithToken = (paragraphElement: Element, token: s
|
|||||||
const textElement = (element.elements ?? []).filter((e) => e.type === "element" && e.name === "w:t");
|
const textElement = (element.elements ?? []).filter((e) => e.type === "element" && e.name === "w:t");
|
||||||
|
|
||||||
for (const text of textElement) {
|
for (const text of textElement) {
|
||||||
if ((text.elements?.[0].text as string)?.includes(token)) {
|
if (!text.elements?.[0]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((text.elements[0].text as string)?.includes(token)) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user