diff --git a/demo/85-template-document.ts b/demo/85-template-document.ts index 2a032586fb..d3aca499ee 100644 --- a/demo/85-template-document.ts +++ b/demo/85-template-document.ts @@ -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, diff --git a/src/patcher/from-docx.ts b/src/patcher/from-docx.ts index c05ccbb45d..38d45b23c8 100644 --- a/src/patcher/from-docx.ts +++ b/src/patcher/from-docx.ts @@ -56,13 +56,10 @@ const imageReplacer = new ImageReplacer(); export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise => { const zipContent = await JSZip.loadAsync(data); - const context: IContext = { - file: { - Media: new Media(), - } as unknown as File, - viewWrapper: {} as unknown as IViewWrapper, - stack: [], - }; + const contexts = new Map(); + const file = { + Media: new Media(), + } as unknown as File; const map = new Map(); @@ -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); }