2023-02-08 03:18:11 +00:00
|
|
|
import * as JSZip from "jszip";
|
|
|
|
import { xml2js, ElementCompact, js2xml } from "xml-js";
|
2023-02-16 20:17:48 +00:00
|
|
|
import { replacer } from "./replacer";
|
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-16 20:17:48 +00:00
|
|
|
export interface PatchDocumentOptions {
|
|
|
|
readonly children: any[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
|
2023-02-08 03:18:11 +00:00
|
|
|
const zipContent = await JSZip.loadAsync(data);
|
|
|
|
|
|
|
|
const map = new Map<string, ElementCompact>();
|
2023-02-16 20:17:48 +00:00
|
|
|
console.log(options);
|
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-02-16 20:17:48 +00:00
|
|
|
if (key === "word/document.xml") {
|
|
|
|
console.log(json);
|
|
|
|
replacer(json, options);
|
|
|
|
}
|
|
|
|
|
2023-02-08 03:18:11 +00:00
|
|
|
map.set(key, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
const zip = new JSZip();
|
|
|
|
|
|
|
|
for (const [key, value] of map) {
|
|
|
|
const output = toXml(value);
|
|
|
|
|
|
|
|
zip.file(key, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "nodebuffer",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
|
|
|
|
return zipData;
|
|
|
|
};
|
|
|
|
|
|
|
|
const toJson = (xmlData: string): ElementCompact => {
|
|
|
|
const xmlObj = xml2js(xmlData, { compact: false }) as ElementCompact;
|
|
|
|
return xmlObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
const toXml = (jsonObj: ElementCompact): string => {
|
|
|
|
const output = js2xml(jsonObj);
|
|
|
|
return output;
|
|
|
|
};
|