diff --git a/package.json b/package.json index 976e4d23f3..3be39c2012 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "dependencies": { "@types/jszip": "^3.1.4", "jszip": "^3.1.5", + "shortid": "^2.2.15", "xml": "^1.0.1", "xml-js": "^1.6.8" }, diff --git a/src/file/file.ts b/src/file/file.ts index ccf5fe374c..ded794fd40 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -25,6 +25,7 @@ import { ExternalStylesFactory } from "./styles/external-styles-factory"; import { DefaultStylesFactory } from "./styles/factory"; import { Table } from "./table"; import { TableOfContents } from "./table-of-contents"; +import * as shortid from "shortid"; export interface ISectionOptions { readonly headers?: { @@ -133,7 +134,7 @@ export class File { public createHyperlink(link: string, text?: string): Hyperlink { const newText = text === undefined ? link : text; - const hyperlink = new Hyperlink(newText, this.docRelationships.RelationshipCount); + const hyperlink = new Hyperlink(newText, shortid.generate().toLowerCase()); this.docRelationships.createRelationship( hyperlink.linkId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", @@ -145,7 +146,7 @@ export class File { public createInternalHyperLink(anchor: string, text?: string): Hyperlink { const newText = text === undefined ? anchor : text; - const hyperlink = new Hyperlink(newText, this.docRelationships.RelationshipCount, anchor); + const hyperlink = new Hyperlink(newText, shortid.generate().toLowerCase(), anchor); // NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark // we don't need to create a new relationship. return hyperlink; diff --git a/src/file/paragraph/links/hyperlink.spec.ts b/src/file/paragraph/links/hyperlink.spec.ts index 09dd07b86f..836f3f733f 100644 --- a/src/file/paragraph/links/hyperlink.spec.ts +++ b/src/file/paragraph/links/hyperlink.spec.ts @@ -8,7 +8,7 @@ describe("Hyperlink", () => { let hyperlink: Hyperlink; beforeEach(() => { - hyperlink = new Hyperlink("https://example.com", 0); + hyperlink = new Hyperlink("https://example.com", 'superid'); }); describe("#constructor()", () => { @@ -19,7 +19,7 @@ describe("Hyperlink", () => { { _attr: { "w:history": 1, - "r:id": "rId1", + "r:id": "rIdsuperid", }, }, { @@ -34,7 +34,7 @@ describe("Hyperlink", () => { describe("with optional anchor parameter", () => { beforeEach(() => { - hyperlink = new Hyperlink("Anchor Text", 0, "anchor"); + hyperlink = new Hyperlink("Anchor Text", 'superid2', "anchor"); }); it("should create an internal link with anchor tag", () => { diff --git a/src/file/paragraph/links/hyperlink.ts b/src/file/paragraph/links/hyperlink.ts index b41980ed77..30e60616ec 100644 --- a/src/file/paragraph/links/hyperlink.ts +++ b/src/file/paragraph/links/hyperlink.ts @@ -4,13 +4,13 @@ import { TextRun } from "../run"; import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes"; export class Hyperlink extends XmlComponent { - public readonly linkId: number; + public readonly linkId: string; private readonly textRun: TextRun; - constructor(text: string, relationshipsCount: number, anchor?: string) { + constructor(text: string, relationshipId: string, anchor?: string) { super("w:hyperlink"); - this.linkId = relationshipsCount + 1; + this.linkId = relationshipId; const props: IHyperlinkAttributesProperties = { history: 1, diff --git a/src/file/relationships/relationships.ts b/src/file/relationships/relationships.ts index bd56ff1aab..3a5447c2d9 100644 --- a/src/file/relationships/relationships.ts +++ b/src/file/relationships/relationships.ts @@ -16,7 +16,7 @@ export class Relationships extends XmlComponent { this.root.push(relationship); } - public createRelationship(id: number, type: RelationshipType, target: string, targetMode?: TargetModeType): Relationship { + public createRelationship(id: number | string, type: RelationshipType, target: string, targetMode?: TargetModeType): Relationship { const relationship = new Relationship(`rId${id}`, type, target, targetMode); this.addRelationship(relationship);