Files
docx-js/src/templater/replacer.ts

73 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-18 20:36:24 +00:00
import { Element } from "xml-js";
import * as xml from "xml";
2023-02-23 19:43:19 +00:00
import { Formatter } from "@export/formatter";
import { Paragraph, TextRun } from "@file/paragraph";
2023-02-17 10:38:03 +00:00
import { IPatch } from "./from-docx";
2023-02-18 20:36:24 +00:00
import { toJson } from "./util";
import { IRenderedParagraphNode } from "./run-renderer";
2023-02-23 19:43:19 +00:00
import { replaceTokenInParagraphElement } from "./paragraph-token-replacer";
2023-02-18 20:36:24 +00:00
const formatter = new Formatter();
2023-02-16 20:17:48 +00:00
2023-02-23 19:43:19 +00:00
const SPLIT_TOKEN = "ɵ";
2023-02-18 20:36:24 +00:00
export const replacer = (json: Element, options: IPatch, renderedParagraphs: readonly IRenderedParagraphNode[]): Element => {
2023-02-16 20:17:48 +00:00
for (const child of options.children) {
if (child instanceof Paragraph) {
console.log("is para");
} else if (child instanceof TextRun) {
2023-02-18 20:36:24 +00:00
console.log("paragrapghs", JSON.stringify(renderedParagraphs, null, 2));
2023-02-23 19:43:19 +00:00
for (const renderedParagraph of renderedParagraphs) {
const textJson = toJson(xml(formatter.format(child)));
const paragraphElement = goToElementFromPath(json, renderedParagraph.path);
const startIndex = renderedParagraph.text.indexOf(options.text);
const endIndex = startIndex + options.text.length - 1;
if (startIndex === 0 && endIndex === renderedParagraph.text.length - 1) {
// Easy case where the text is the entire paragraph
// eslint-disable-next-line functional/immutable-data
paragraphElement.elements = textJson.elements;
} else {
// Hard case where the text is only part of the paragraph
console.log("hard case");
console.log("paragraphElement", JSON.stringify(paragraphElement, null, 2));
replaceTokenInParagraphElement({
paragraphElement,
renderedParagraph,
originalText: options.text,
replacementText: SPLIT_TOKEN,
});
console.log("paragraphElement after", JSON.stringify(paragraphElement, null, 2));
}
}
2023-02-16 20:17:48 +00:00
}
}
return json;
};
2023-02-18 20:36:24 +00:00
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;
};