Context per view wrapper

This commit is contained in:
Dolan Miu
2023-03-13 21:35:16 +00:00
parent 0fba450c9a
commit 8ce057e25c
2 changed files with 42 additions and 9 deletions

View File

@ -42,7 +42,23 @@ patchDocument(fs.readFileSync("demo/assets/simple-template.docx"), {
},
paragraph_replace: {
type: PatchType.DOCUMENT,
children: [new Paragraph("Lorem ipsum paragraph"), new Paragraph("Another paragraph")],
children: [
new Paragraph("Lorem ipsum paragraph"),
new Paragraph("Another paragraph"),
new Paragraph({
children: [
new TextRun("This is a "),
new ExternalHyperlink({
children: [
new TextRun({
text: "Google Link",
}),
],
link: "https://www.google.co.uk",
}),
],
}),
],
},
header_adjective: {
type: PatchType.PARAGRAPH,

View File

@ -56,13 +56,10 @@ const imageReplacer = new ImageReplacer();
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
const zipContent = await JSZip.loadAsync(data);
const context: IContext = {
file: {
const contexts = new Map<string, IContext>();
const file = {
Media: new Media(),
} as unknown as File,
viewWrapper: {} as unknown as IViewWrapper,
stack: [],
};
} as unknown as File;
const map = new Map<string, Element>();
@ -75,6 +72,26 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
for (const [key, value] of Object.entries(zipContent.files)) {
const json = toJson(await value.async("text"));
if (key.startsWith("word/") && !key.endsWith(".xml.rels")) {
const context: IContext = {
file,
viewWrapper: {
Relationships: {
createRelationship: (linkId: string, _: string, target: string, __: TargetModeType) => {
// eslint-disable-next-line functional/immutable-data
hyperlinkRelationshipAdditions.push({
key,
hyperlink: {
id: linkId,
link: target,
},
});
},
},
} as unknown as IViewWrapper,
stack: [],
};
contexts.set(key, context);
for (const [patchKey, patchValue] of Object.entries(options.patches)) {
const patchText = `{{${patchKey}}}`;
const renderedParagraphs = findLocationOfText(json, patchText);
@ -194,7 +211,7 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
zip.file(key, output);
}
for (const { stream, fileName } of context.file.Media.Array) {
for (const { stream, fileName } of file.Media.Array) {
zip.file(`word/media/${fileName}`, stream);
}