diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts index d104ca7574..1ad50eb9b1 100644 --- a/demo/21-bookmarks.ts +++ b/demo/21-bookmarks.ts @@ -1,7 +1,7 @@ // This demo shows how to create bookmarks then link to them with internal hyperlinks // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph } from "../build"; +import { Bookmark, Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph } from "../build"; const LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; @@ -18,14 +18,11 @@ const doc = new Document({ }, }); -// First create the bookmark -const bookmark = doc.createBookmark("myAnchorId", "Lorem Ipsum"); - doc.addSection({ children: [ new Paragraph({ heading: HeadingLevel.HEADING_1, - children: [bookmark], + children: [new Bookmark("myAnchorId", "Lorem Ipsum")], }), new Paragraph("\n"), new Paragraph(LOREM_IPSUM), diff --git a/src/file/file.ts b/src/file/file.ts index 21a8487d71..e549b4b624 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -17,7 +17,7 @@ import { Footer, Header } from "./header"; import { HeaderWrapper, IDocumentHeader } from "./header-wrapper"; import { Media } from "./media"; import { Numbering } from "./numbering"; -import { Bookmark, Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph"; +import { Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { TargetModeType } from "./relationships/relationship/relationship"; import { Settings } from "./settings"; @@ -172,10 +172,6 @@ export class File { } } - public createBookmark(name: string, text: string = name): Bookmark { - return new Bookmark(name, text, this.docRelationships.RelationshipCount); - } - public addSection({ headers = { default: new Header() }, footers = { default: new Header() }, diff --git a/src/file/paragraph/links/bookmark.spec.ts b/src/file/paragraph/links/bookmark.spec.ts index a597347013..fe342fc374 100644 --- a/src/file/paragraph/links/bookmark.spec.ts +++ b/src/file/paragraph/links/bookmark.spec.ts @@ -1,4 +1,4 @@ -import { assert } from "chai"; +import { assert, expect } from "chai"; import { Utility } from "tests/utility"; @@ -8,7 +8,7 @@ describe("Bookmark", () => { let bookmark: Bookmark; beforeEach(() => { - bookmark = new Bookmark("anchor", "Internal Link", 0); + bookmark = new Bookmark("anchor", "Internal Link"); }); it("should create a bookmark with three root elements", () => { @@ -21,11 +21,8 @@ describe("Bookmark", () => { it("should create a bookmark with the correct attributes on the bookmark start element", () => { const newJson = Utility.jsonify(bookmark); - const attributes = { - name: "anchor", - id: "1", - }; - assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes)); + + assert.equal(newJson.start.root[0].root.name, "anchor"); }); it("should create a bookmark with the correct attributes on the text element", () => { @@ -35,9 +32,6 @@ describe("Bookmark", () => { it("should create a bookmark with the correct attributes on the bookmark end element", () => { const newJson = Utility.jsonify(bookmark); - const attributes = { - id: "1", - }; - assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes)); + expect(newJson.end.root[0].root.id).to.be.a("string"); }); }); diff --git a/src/file/paragraph/links/bookmark.ts b/src/file/paragraph/links/bookmark.ts index 5ef567899b..addf14530e 100644 --- a/src/file/paragraph/links/bookmark.ts +++ b/src/file/paragraph/links/bookmark.ts @@ -1,5 +1,6 @@ // http://officeopenxml.com/WPbookmark.php import { XmlComponent } from "file/xml-components"; +import * as shortid from "shortid"; import { TextRun } from "../run"; import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes"; @@ -8,46 +9,37 @@ export class BookmarkRef { } export class Bookmark { - public readonly linkId: number; public readonly start: BookmarkStart; public readonly text: TextRun; public readonly end: BookmarkEnd; - constructor(name: string, text: string, relationshipsCount: number) { - this.linkId = relationshipsCount + 1; + constructor(name: string, text: string) { + const linkId = shortid.generate().toLowerCase(); - this.start = new BookmarkStart(name, this.linkId); + this.start = new BookmarkStart(name, linkId); this.text = new TextRun(text); - this.end = new BookmarkEnd(this.linkId); + this.end = new BookmarkEnd(linkId); } } export class BookmarkStart extends XmlComponent { - public readonly linkId: number; - - constructor(name: string, relationshipsCount: number) { + constructor(name: string, linkId: string) { super("w:bookmarkStart"); - this.linkId = relationshipsCount; - const id = `${this.linkId}`; const attributes = new BookmarkStartAttributes({ name, - id, + id: linkId, }); this.root.push(attributes); } } export class BookmarkEnd extends XmlComponent { - public readonly linkId: number; - - constructor(relationshipsCount: number) { + constructor(linkId: string) { super("w:bookmarkEnd"); - this.linkId = relationshipsCount; - const id = `${this.linkId}`; const attributes = new BookmarkEndAttributes({ - id, + id: linkId, }); this.root.push(attributes); }