From 3427d220c79baffefb92753f2d459c88362de58f Mon Sep 17 00:00:00 2001 From: Nicolas Rotier Date: Fri, 8 Nov 2019 10:50:18 +0100 Subject: [PATCH 1/2] fix hyperlink id --- package.json | 1 + src/file/file.ts | 5 +++-- src/file/paragraph/links/hyperlink.spec.ts | 6 +++--- src/file/paragraph/links/hyperlink.ts | 6 +++--- src/file/relationships/relationships.ts | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) 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); From e8410ff692c668e8118093c3cf8f455ac6f8cfd5 Mon Sep 17 00:00:00 2001 From: Nicolas Rotier Date: Fri, 8 Nov 2019 11:22:27 +0100 Subject: [PATCH 2/2] fix lint --- src/file/file.ts | 2 +- src/file/paragraph/links/hyperlink.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file/file.ts b/src/file/file.ts index ded794fd40..10a85bf79a 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,3 +1,4 @@ +import * as shortid from "shortid"; import { AppProperties } from "./app-properties/app-properties"; import { ContentTypes } from "./content-types/content-types"; import { CoreProperties, IPropertiesOptions } from "./core-properties"; @@ -25,7 +26,6 @@ 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?: { diff --git a/src/file/paragraph/links/hyperlink.spec.ts b/src/file/paragraph/links/hyperlink.spec.ts index 836f3f733f..93b59e07b6 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", 'superid'); + hyperlink = new Hyperlink("https://example.com", "superid"); }); describe("#constructor()", () => { @@ -34,7 +34,7 @@ describe("Hyperlink", () => { describe("with optional anchor parameter", () => { beforeEach(() => { - hyperlink = new Hyperlink("Anchor Text", 'superid2', "anchor"); + hyperlink = new Hyperlink("Anchor Text", "superid2", "anchor"); }); it("should create an internal link with anchor tag", () => {