Simple patcher working

This commit is contained in:
Dolan Miu
2023-02-18 20:36:24 +00:00
parent 86de252a52
commit 5233b4b5e6
6 changed files with 121 additions and 32 deletions

View File

@ -1,15 +1,48 @@
import { Formatter } from "@export/formatter";
import { Paragraph, TextRun } from "@file/paragraph";
import { ElementCompact } from "xml-js";
import { IPatch } from "./from-docx";
import { Element } from "xml-js";
import * as xml from "xml";
export const replacer = (json: ElementCompact, options: IPatch): ElementCompact => {
import { IPatch } from "./from-docx";
import { toJson } from "./util";
import { IRenderedParagraphNode } from "./run-renderer";
const formatter = new Formatter();
export const replacer = (json: Element, options: IPatch, renderedParagraphs: readonly IRenderedParagraphNode[]): Element => {
for (const child of options.children) {
if (child instanceof Paragraph) {
console.log("is para");
} else if (child instanceof TextRun) {
console.log("is text");
const text = formatter.format(child);
const textJson = toJson(xml(text));
console.log("paragrapghs", JSON.stringify(renderedParagraphs, null, 2));
const paragraphElement = goToElementFromPath(json, renderedParagraphs[0].path);
console.log(paragraphElement);
// eslint-disable-next-line functional/immutable-data
paragraphElement.elements = textJson.elements;
console.log("is text", text);
}
}
return json;
};
const goToElementFromPath = (json: Element, path: readonly number[]): Element => {
let element = json;
// We start from 1 because the first element is the root element
// Which we do not want to double count
for (let i = 1; i < path.length; i++) {
const index = path[i];
const nextElements = element.elements;
if (!nextElements) {
throw new Error("Could not find element");
}
element = nextElements[index];
}
return element;
};