Files
docx-js/src/patcher/util.ts

31 lines
993 B
TypeScript
Raw Normal View History

2023-02-18 20:36:24 +00:00
import { xml2js, Element } from "xml-js";
2023-02-24 01:05:43 +00:00
import * as xml from "xml";
import { Formatter } from "@export/formatter";
import { Text } from "@file/paragraph/run/run-components/text";
const formatter = new Formatter();
2023-02-18 20:36:24 +00:00
export const toJson = (xmlData: string): Element => {
const xmlObj = xml2js(xmlData, { compact: false }) as Element;
return xmlObj;
};
2023-02-24 01:05:43 +00:00
// eslint-disable-next-line functional/prefer-readonly-type
export const createTextElementContents = (text: string): Element[] => {
const textJson = toJson(xml(formatter.format(new Text({ text }))));
return textJson.elements![0].elements ?? [];
};
2023-02-25 22:18:56 +00:00
export const patchSpaceAttribute = (element: Element): Element => ({
...element,
attributes: {
"xml:space": "preserve",
},
});
2023-03-03 23:47:50 +00:00
// eslint-disable-next-line functional/prefer-readonly-type
export const getFirstLevelElements = (relationships: Element, id: string): Element[] =>
relationships.elements?.filter((e) => e.name === id)[0].elements ?? [];