diff --git a/src/file/paragraph/links/hyperlink-attributes.ts b/src/file/paragraph/links/hyperlink-attributes.ts new file mode 100644 index 0000000000..d51f5a8b65 --- /dev/null +++ b/src/file/paragraph/links/hyperlink-attributes.ts @@ -0,0 +1,13 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IHyperlinkAttributesProperties { + id?: string; + history: number; +} + +export class HyperlinkAttributes extends XmlAttributeComponent { + protected xmlKeys = { + id: "r:id", + history: "w:history", + }; +} diff --git a/src/file/paragraph/links/hyperlink.spec.ts b/src/file/paragraph/links/hyperlink.spec.ts new file mode 100644 index 0000000000..9e3aebc29f --- /dev/null +++ b/src/file/paragraph/links/hyperlink.spec.ts @@ -0,0 +1,37 @@ +import { assert, expect } from "chai"; + +import { Formatter } from "../../../export/formatter"; +import { Utility } from "../../../tests/utility"; +import { Hyperlink } from "./"; + +describe("Hyperlink", () => { + let hyperlink: Hyperlink; + + describe("#constructor()", () => { + it("should create a hyperlink with correct root key", () => { + hyperlink = new Hyperlink("https://example.com", 0); + const newJson = Utility.jsonify(hyperlink); + assert.equal(newJson.rootKey, "w:hyperlink"); + }); + + it("should create a hyperlink with right attributes", () => { + hyperlink = new Hyperlink("https://example.com", 0); + const newJson = Utility.jsonify(hyperlink); + const attributes = { + id: "rId1", + history: 1, + }; + assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes)); + }); + + it("should create a hyperlink with a run component", () => { + hyperlink = new Hyperlink("https://example.com", 0); + const tree = new Formatter().format(hyperlink); + expect(tree["w:hyperlink"][1]).to.deep.equal({ + "w:r": [ + { "w:rPr": [{ "w:rStyle": [{ _attr: { "w:val": "Hyperlink"} }] }] }, + { "w:t": [{_attr: {"xml:space": "preserve"}}, "https://example.com"]}, + ]}); + }); + }); +}); diff --git a/src/file/paragraph/links/hyperlink.ts b/src/file/paragraph/links/hyperlink.ts new file mode 100644 index 0000000000..f700ae0a4d --- /dev/null +++ b/src/file/paragraph/links/hyperlink.ts @@ -0,0 +1,20 @@ +// http://officeopenxml.com/WPhyperlink.php +import { XmlComponent } from "file/xml-components"; +import { TextRun } from "../run"; +import { HyperlinkAttributes } from "./hyperlink-attributes"; + +export class Hyperlink extends XmlComponent { + public linkId: number; + + constructor(text: string, relationshipsCount: number) { + super("w:hyperlink"); + + this.linkId = relationshipsCount + 1; + this.root.push(new HyperlinkAttributes({ + id: `rId${this.linkId}`, + history: 1, + })); + this.root.push(new TextRun(text).style("Hyperlink")); + return this; + } +} diff --git a/src/file/paragraph/links/index.ts b/src/file/paragraph/links/index.ts new file mode 100644 index 0000000000..619d1bee30 --- /dev/null +++ b/src/file/paragraph/links/index.ts @@ -0,0 +1 @@ +export * from "./hyperlink";