Files
docx-js/src/patcher/relationship-manager.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-03-03 23:47:50 +00:00
import { Element } from "xml-js";
import { RelationshipType } from "@file/relationships/relationship/relationship";
import { getFirstLevelElements } from "./util";
const getIdFromRelationshipId = (relationshipId: string): number => parseInt(relationshipId.substring(3), 10);
export const getNextRelationshipIndex = (relationships: Element): number => {
const relationshipElements = getFirstLevelElements(relationships, "Relationships");
return (
(relationshipElements
.map((e) => getIdFromRelationshipId(e.attributes?.Id?.toString() ?? ""))
.reduce((acc, curr) => Math.max(acc, curr), 0) ?? 0) + 1
);
};
export const appendRelationship = (relationships: Element, id: number, type: RelationshipType, target: string): void => {
const relationshipElements = getFirstLevelElements(relationships, "Relationships");
// eslint-disable-next-line functional/immutable-data
relationshipElements.push({
attributes: {
Id: `rId${id}`,
Type: type,
Target: target,
},
name: "Relationship",
type: "element",
});
};