2023-02-08 03:18:11 +00:00
|
|
|
import * as JSZip from "jszip";
|
2023-02-18 20:36:24 +00:00
|
|
|
import { Element, js2xml } from "xml-js";
|
2023-02-25 19:33:12 +00:00
|
|
|
|
2023-03-08 23:30:51 +00:00
|
|
|
import { ConcreteHyperlink, ExternalHyperlink, ParagraphChild } from "@file/paragraph";
|
2023-02-25 19:33:12 +00:00
|
|
|
import { FileChild } from "@file/file-child";
|
2023-03-03 23:47:50 +00:00
|
|
|
import { IMediaData, Media } from "@file/media";
|
|
|
|
import { IViewWrapper } from "@file/document-wrapper";
|
|
|
|
import { File } from "@file/file";
|
|
|
|
import { IContext } from "@file/xml-components";
|
|
|
|
import { ImageReplacer } from "@export/packer/image-replacer";
|
2023-03-08 23:30:51 +00:00
|
|
|
import { TargetModeType } from "@file/relationships/relationship/relationship";
|
|
|
|
import { uniqueId } from "@util/convenience-functions";
|
2023-02-25 19:33:12 +00:00
|
|
|
|
2023-02-16 20:17:48 +00:00
|
|
|
import { replacer } from "./replacer";
|
2023-02-17 10:38:03 +00:00
|
|
|
import { findLocationOfText } from "./traverser";
|
2023-02-18 20:36:24 +00:00
|
|
|
import { toJson } from "./util";
|
2023-03-03 23:47:50 +00:00
|
|
|
import { appendRelationship, getNextRelationshipIndex } from "./relationship-manager";
|
|
|
|
import { appendContentType } from "./content-types-manager";
|
2023-02-08 03:18:11 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
|
|
|
type InputDataType = Buffer | string | number[] | Uint8Array | ArrayBuffer | Blob | NodeJS.ReadableStream;
|
|
|
|
|
2023-02-25 19:33:12 +00:00
|
|
|
export enum PatchType {
|
|
|
|
DOCUMENT = "file",
|
|
|
|
PARAGRAPH = "paragraph",
|
2023-02-17 10:38:03 +00:00
|
|
|
}
|
2023-02-25 19:33:12 +00:00
|
|
|
|
|
|
|
type ParagraphPatch = {
|
|
|
|
readonly type: PatchType.PARAGRAPH;
|
|
|
|
readonly children: readonly ParagraphChild[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type FilePatch = {
|
|
|
|
readonly type: PatchType.DOCUMENT;
|
|
|
|
readonly children: readonly FileChild[];
|
|
|
|
};
|
|
|
|
|
2023-03-08 23:30:51 +00:00
|
|
|
interface IImageRelationshipAddition {
|
2023-03-03 23:47:50 +00:00
|
|
|
readonly key: string;
|
|
|
|
readonly mediaDatas: readonly IMediaData[];
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:51 +00:00
|
|
|
interface IHyperlinkRelationshipAddition {
|
|
|
|
readonly key: string;
|
|
|
|
readonly hyperlink: { readonly id: string; readonly link: string };
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:18:31 +00:00
|
|
|
export type IPatch = ParagraphPatch | FilePatch;
|
2023-02-25 19:33:12 +00:00
|
|
|
|
2023-02-17 10:38:03 +00:00
|
|
|
export interface PatchDocumentOptions {
|
2023-02-25 22:18:31 +00:00
|
|
|
readonly patches: { readonly [key: string]: IPatch };
|
2023-02-16 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 23:47:50 +00:00
|
|
|
const imageReplacer = new ImageReplacer();
|
|
|
|
|
2023-02-16 20:17:48 +00:00
|
|
|
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
|
2023-02-08 03:18:11 +00:00
|
|
|
const zipContent = await JSZip.loadAsync(data);
|
2023-03-13 21:35:16 +00:00
|
|
|
const contexts = new Map<string, IContext>();
|
|
|
|
const file = {
|
|
|
|
Media: new Media(),
|
|
|
|
} as unknown as File;
|
2023-03-03 23:47:50 +00:00
|
|
|
|
2023-02-17 10:38:03 +00:00
|
|
|
const map = new Map<string, Element>();
|
2023-02-08 03:18:11 +00:00
|
|
|
|
2023-03-03 23:47:50 +00:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2023-03-08 23:30:51 +00:00
|
|
|
const imageRelationshipAdditions: IImageRelationshipAddition[] = [];
|
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
|
|
|
const hyperlinkRelationshipAdditions: IHyperlinkRelationshipAddition[] = [];
|
2023-03-03 23:47:50 +00:00
|
|
|
let hasMedia = false;
|
|
|
|
|
2023-02-08 03:18:11 +00:00
|
|
|
for (const [key, value] of Object.entries(zipContent.files)) {
|
|
|
|
const json = toJson(await value.async("text"));
|
2023-03-03 23:47:50 +00:00
|
|
|
if (key.startsWith("word/") && !key.endsWith(".xml.rels")) {
|
2023-03-13 21:35:16 +00:00
|
|
|
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);
|
|
|
|
|
2023-02-25 22:18:31 +00:00
|
|
|
for (const [patchKey, patchValue] of Object.entries(options.patches)) {
|
|
|
|
const patchText = `{{${patchKey}}}`;
|
|
|
|
const renderedParagraphs = findLocationOfText(json, patchText);
|
2023-02-25 22:18:56 +00:00
|
|
|
// TODO: mutates json. Make it immutable
|
2023-03-08 23:30:51 +00:00
|
|
|
replacer(
|
|
|
|
json,
|
|
|
|
{
|
|
|
|
...patchValue,
|
|
|
|
children: patchValue.children.map((element) => {
|
|
|
|
// We need to replace external hyperlinks with concrete hyperlinks
|
|
|
|
if (element instanceof ExternalHyperlink) {
|
|
|
|
const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
|
|
|
|
// eslint-disable-next-line functional/immutable-data
|
|
|
|
hyperlinkRelationshipAdditions.push({
|
|
|
|
key,
|
|
|
|
hyperlink: {
|
|
|
|
id: concreteHyperlink.linkId,
|
|
|
|
link: element.options.link,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return concreteHyperlink;
|
|
|
|
} else {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
patchText,
|
|
|
|
renderedParagraphs,
|
|
|
|
context,
|
|
|
|
);
|
2023-03-03 23:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mediaDatas = imageReplacer.getMediaData(JSON.stringify(json), context.file.Media);
|
|
|
|
if (mediaDatas.length > 0) {
|
|
|
|
hasMedia = true;
|
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2023-03-08 23:30:51 +00:00
|
|
|
imageRelationshipAdditions.push({
|
2023-03-03 23:47:50 +00:00
|
|
|
key,
|
|
|
|
mediaDatas,
|
|
|
|
});
|
2023-02-17 10:38:03 +00:00
|
|
|
}
|
2023-02-16 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 03:18:11 +00:00
|
|
|
map.set(key, json);
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:51 +00:00
|
|
|
for (const { key, mediaDatas } of imageRelationshipAdditions) {
|
2023-03-03 23:47:50 +00:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2023-03-08 23:30:51 +00:00
|
|
|
const relationshipKey = `word/_rels/${key.split("/").pop()}.rels`;
|
|
|
|
|
|
|
|
if (!map.has(relationshipKey)) {
|
|
|
|
map.set(relationshipKey, createRelationshipFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
const relationshipsJson = map.get(relationshipKey);
|
|
|
|
|
|
|
|
if (!relationshipsJson) {
|
|
|
|
throw new Error("Could not find relationships file");
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = getNextRelationshipIndex(relationshipsJson);
|
|
|
|
const newJson = imageReplacer.replace(JSON.stringify(map.get(key)), mediaDatas, index);
|
|
|
|
map.set(key, JSON.parse(newJson) as Element);
|
|
|
|
|
|
|
|
for (const { fileName } of mediaDatas) {
|
|
|
|
appendRelationship(
|
|
|
|
relationshipsJson,
|
|
|
|
index,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
|
|
|
`media/${fileName}`,
|
|
|
|
);
|
2023-03-03 23:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:51 +00:00
|
|
|
for (const { key, hyperlink } of hyperlinkRelationshipAdditions) {
|
|
|
|
// eslint-disable-next-line functional/immutable-data
|
|
|
|
const relationshipKey = `word/_rels/${key.split("/").pop()}.rels`;
|
|
|
|
|
2023-03-15 03:14:38 +00:00
|
|
|
const relationshipsJson = map.get(relationshipKey) ?? createRelationshipFile();
|
|
|
|
map.set(relationshipKey, relationshipsJson);
|
2023-03-08 23:30:51 +00:00
|
|
|
|
|
|
|
appendRelationship(
|
|
|
|
relationshipsJson,
|
|
|
|
hyperlink.id,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
|
|
|
hyperlink.link,
|
|
|
|
TargetModeType.EXTERNAL,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-03 23:47:50 +00:00
|
|
|
if (hasMedia) {
|
|
|
|
const contentTypesJson = map.get("[Content_Types].xml");
|
|
|
|
|
|
|
|
if (!contentTypesJson) {
|
|
|
|
throw new Error("Could not find content types file");
|
|
|
|
}
|
|
|
|
|
|
|
|
appendContentType(contentTypesJson, "image/png", "png");
|
|
|
|
appendContentType(contentTypesJson, "image/jpeg", "jpeg");
|
|
|
|
appendContentType(contentTypesJson, "image/jpeg", "jpg");
|
|
|
|
appendContentType(contentTypesJson, "image/bmp", "bmp");
|
|
|
|
appendContentType(contentTypesJson, "image/gif", "gif");
|
|
|
|
}
|
|
|
|
|
2023-02-08 03:18:11 +00:00
|
|
|
const zip = new JSZip();
|
|
|
|
|
|
|
|
for (const [key, value] of map) {
|
|
|
|
const output = toXml(value);
|
|
|
|
|
|
|
|
zip.file(key, output);
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:35:16 +00:00
|
|
|
for (const { stream, fileName } of file.Media.Array) {
|
2023-03-03 23:47:50 +00:00
|
|
|
zip.file(`word/media/${fileName}`, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return zip.generateAsync({
|
2023-02-08 03:18:11 +00:00
|
|
|
type: "nodebuffer",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-17 10:38:03 +00:00
|
|
|
const toXml = (jsonObj: Element): string => {
|
2023-02-08 03:18:11 +00:00
|
|
|
const output = js2xml(jsonObj);
|
|
|
|
return output;
|
|
|
|
};
|
2023-03-08 23:30:51 +00:00
|
|
|
|
|
|
|
const createRelationshipFile = (): Element => ({
|
|
|
|
declaration: {
|
|
|
|
attributes: {
|
|
|
|
version: "1.0",
|
|
|
|
encoding: "UTF-8",
|
|
|
|
standalone: "yes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
elements: [
|
|
|
|
{
|
|
|
|
type: "element",
|
|
|
|
name: "Relationships",
|
|
|
|
attributes: {
|
|
|
|
xmlns: "http://schemas.openxmlformats.org/package/2006/relationships",
|
|
|
|
},
|
|
|
|
elements: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|