Merge pull request #448 from Sraleik/master

fix hyperlink id
This commit is contained in:
Dolan
2019-11-12 00:48:21 +00:00
committed by GitHub
5 changed files with 11 additions and 9 deletions

View File

@ -51,6 +51,7 @@
"dependencies": { "dependencies": {
"@types/jszip": "^3.1.4", "@types/jszip": "^3.1.4",
"jszip": "^3.1.5", "jszip": "^3.1.5",
"shortid": "^2.2.15",
"xml": "^1.0.1", "xml": "^1.0.1",
"xml-js": "^1.6.8" "xml-js": "^1.6.8"
}, },

View File

@ -1,3 +1,4 @@
import * as shortid from "shortid";
import { AppProperties } from "./app-properties/app-properties"; import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types"; import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties"; import { CoreProperties, IPropertiesOptions } from "./core-properties";
@ -139,7 +140,7 @@ export class File {
public createHyperlink(link: string, text?: string): Hyperlink { public createHyperlink(link: string, text?: string): Hyperlink {
const newText = text === undefined ? link : text; 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( this.docRelationships.createRelationship(
hyperlink.linkId, hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
@ -151,7 +152,7 @@ export class File {
public createInternalHyperLink(anchor: string, text?: string): Hyperlink { public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
const newText = text === undefined ? anchor : text; 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 // NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
// we don't need to create a new relationship. // we don't need to create a new relationship.
return hyperlink; return hyperlink;

View File

@ -8,7 +8,7 @@ describe("Hyperlink", () => {
let hyperlink: Hyperlink; let hyperlink: Hyperlink;
beforeEach(() => { beforeEach(() => {
hyperlink = new Hyperlink("https://example.com", 0); hyperlink = new Hyperlink("https://example.com", "superid");
}); });
describe("#constructor()", () => { describe("#constructor()", () => {
@ -19,7 +19,7 @@ describe("Hyperlink", () => {
{ {
_attr: { _attr: {
"w:history": 1, "w:history": 1,
"r:id": "rId1", "r:id": "rIdsuperid",
}, },
}, },
{ {
@ -34,7 +34,7 @@ describe("Hyperlink", () => {
describe("with optional anchor parameter", () => { describe("with optional anchor parameter", () => {
beforeEach(() => { beforeEach(() => {
hyperlink = new Hyperlink("Anchor Text", 0, "anchor"); hyperlink = new Hyperlink("Anchor Text", "superid2", "anchor");
}); });
it("should create an internal link with anchor tag", () => { it("should create an internal link with anchor tag", () => {

View File

@ -4,13 +4,13 @@ import { TextRun } from "../run";
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes"; import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
export class Hyperlink extends XmlComponent { export class Hyperlink extends XmlComponent {
public readonly linkId: number; public readonly linkId: string;
private readonly textRun: TextRun; private readonly textRun: TextRun;
constructor(text: string, relationshipsCount: number, anchor?: string) { constructor(text: string, relationshipId: string, anchor?: string) {
super("w:hyperlink"); super("w:hyperlink");
this.linkId = relationshipsCount + 1; this.linkId = relationshipId;
const props: IHyperlinkAttributesProperties = { const props: IHyperlinkAttributesProperties = {
history: 1, history: 1,

View File

@ -16,7 +16,7 @@ export class Relationships extends XmlComponent {
this.root.push(relationship); 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); const relationship = new Relationship(`rId${id}`, type, target, targetMode);
this.addRelationship(relationship); this.addRelationship(relationship);