diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index 45be0d7a15..0e1e2fcdbb 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -41,6 +41,7 @@ export class Compiler { }, }); const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); + const xmlRelationships = xml(this.formatter.format(this.file.Relationships)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -58,6 +59,10 @@ export class Compiler { name: "word/numbering.xml", }); + this.archive.append(xmlRelationships, { + name: "word/_rels/document.xml.rels", + }); + for (const data of this.file.Media.array) { this.archive.append(data.stream, { name: `media/${data.fileName}`, diff --git a/src/file/file.ts b/src/file/file.ts index 59e913743f..5b76af52a3 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,3 +1,4 @@ +import { Relationships } from "file/relationships"; import { Document } from "./document"; import { Media } from "./media"; import { Numbering } from "./numbering"; @@ -14,6 +15,7 @@ export class File { private properties: Properties; private numbering: Numbering; private media: Media; + private relationships: Relationships; constructor(options?: IPropertiesOptions) { this.document = new Document(); @@ -31,6 +33,7 @@ export class File { this.properties = new Properties(options); this.numbering = new Numbering(); this.media = new Media(); + this.relationships = new Relationships(); } public addParagraph(paragraph: Paragraph): void { @@ -68,4 +71,8 @@ export class File { public get Media(): Media { return this.media; } + + public get Relationships(): Relationships { + return this.relationships; + } } diff --git a/src/file/relationships/relationship/relationship-attributes.ts b/src/file/relationships/relationship/relationship-attributes.ts new file mode 100644 index 0000000000..1b23e26977 --- /dev/null +++ b/src/file/relationships/relationship/relationship-attributes.ts @@ -0,0 +1,15 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IRelationshipAttributesProperties { + id: string; + type: string; + target: string; +} + +export class RelationshipAttributes extends XmlAttributeComponent { + protected xmlKeys = { + id: "Id", + type: "Type", + target: "Target", + }; +} diff --git a/src/file/relationships/relationship/relationship.ts b/src/file/relationships/relationship/relationship.ts new file mode 100644 index 0000000000..a19536b09c --- /dev/null +++ b/src/file/relationships/relationship/relationship.ts @@ -0,0 +1,23 @@ +import { XmlComponent } from "file/xml-components"; +import { RelationshipAttributes } from "./relationship-attributes"; + +export type RelationshipType = + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"; + +export class Relationship extends XmlComponent { + constructor(id: string, type: RelationshipType, target: string) { + super("Relationship"); + + this.root.push(new RelationshipAttributes({ + id, + type, + target, + })); + } +} diff --git a/src/file/relationships/relationships.ts b/src/file/relationships/relationships.ts index d9c0562a74..a38d85f828 100644 --- a/src/file/relationships/relationships.ts +++ b/src/file/relationships/relationships.ts @@ -1,5 +1,6 @@ import { XmlComponent } from "file/xml-components"; import { RelationshipsAttributes } from "./attributes"; +import { Relationship, RelationshipType } from "./relationship/relationship"; export class Relationships extends XmlComponent { @@ -9,6 +10,18 @@ export class Relationships extends XmlComponent { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships", })); - // this.root.push(new Created()); + this.createRelationship(1, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "styles.xml"); + this.createRelationship(2, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering", "numbering.xml"); + } + + public addRelationship(relationship: Relationship): void { + this.root.push(relationship); + } + + public createRelationship(id: number, type: RelationshipType, target: string): Relationship { + const relationship = new Relationship(`rId${id}`, type, target); + this.addRelationship(relationship); + + return relationship; } } diff --git a/template/word/_rels/document.xml.rels b/template/word/_rels/document.xml.rels deleted file mode 100644 index 50d83cc54c..0000000000 --- a/template/word/_rels/document.xml.rels +++ /dev/null @@ -1,5 +0,0 @@ - - - - -