Re-name templater to patcher

This commit is contained in:
Dolan Miu
2023-02-25 20:18:00 +00:00
parent 3f6c006716
commit 4e875b4744
9 changed files with 1 additions and 1 deletions

74
src/patcher/from-docx.ts Normal file
View File

@ -0,0 +1,74 @@
import * as JSZip from "jszip";
import { Element, js2xml } from "xml-js";
import { ParagraphChild } from "@file/paragraph";
import { FileChild } from "@file/file-child";
import { replacer } from "./replacer";
import { findLocationOfText } from "./traverser";
import { toJson } from "./util";
// eslint-disable-next-line functional/prefer-readonly-type
type InputDataType = Buffer | string | number[] | Uint8Array | ArrayBuffer | Blob | NodeJS.ReadableStream;
export enum PatchType {
DOCUMENT = "file",
PARAGRAPH = "paragraph",
}
type ParagraphPatch = {
readonly type: PatchType.PARAGRAPH;
readonly children: readonly ParagraphChild[];
};
type FilePatch = {
readonly type: PatchType.DOCUMENT;
readonly children: readonly FileChild[];
};
export type IPatch = {
readonly text: string;
} & (ParagraphPatch | FilePatch);
export interface PatchDocumentOptions {
readonly patches: readonly IPatch[];
}
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
const zipContent = await JSZip.loadAsync(data);
const map = new Map<string, Element>();
for (const [key, value] of Object.entries(zipContent.files)) {
const json = toJson(await value.async("text"));
if (key.startsWith("word/")) {
for (const patch of options.patches) {
const renderedParagraphs = findLocationOfText(json, patch.text);
replacer(json, patch, renderedParagraphs);
}
}
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 toXml = (jsonObj: Element): string => {
const output = js2xml(jsonObj);
return output;
};