BookmarkCollectionNEW
BookmarkCollection:
{}The bookmarks of a document.
Methods
Section titled “Methods”all(
options?):Bookmark[]
Lists bookmarks in document order: the body first, including table cells, footnotes, endnotes and shape text, then each section’s headers and footers. Bookmarks inside comments are not listed.
Parameters
Section titled “Parameters”options?
Section titled “options?”Filters to apply
Returns
Section titled “Returns”Bookmark[]
The matching bookmarks
Example
Section titled “Example”List every bookmark, including those of a table of contents’ entries:
const names = draft.bookmarks().all({ includeHidden: true }).map(({ name }) => name);Finds a bookmark by its exact name, hidden or not.
Parameters
Section titled “Parameters”The bookmark’s Bookmark.name
Returns
Section titled “Returns”The bookmark, or undefined when the document has no bookmark with that name
Remarks
Section titled “Remarks”A shortcut for finding the bookmark with this name in all({ includeHidden: true }).
The match is exact, while bookmark names must be unique in any case: when the
document has Intro, get('intro') returns undefined, and adding intro is refused.
Example
Section titled “Example”Check whether a page reference’s bookmark exists:
for (const field of draft.fields().all()) { if (field.definition.type === 'pageReference' && !draft.bookmarks().get(field.definition.bookmark)) { console.log(`Broken reference to ${field.definition.bookmark}`); }}Adds a bookmark on a stretch of text or at a position.
Parameters
Section titled “Parameters”The bookmark’s name and where it goes
Returns
Section titled “Returns”The new bookmark, or undefined when the name is invalid or taken, the
anchor is invalid, or the participant may not add it; the document is then unchanged
Remarks
Section titled “Remarks”A span of text must not be empty, and its ends must be in the same container (the body, one table cell, one header…) with only paragraphs between them. It may overlap fields and other bookmarks.
Bookmark changes are never tracked, so only an owner may add one, and not in a review transaction.
Examples
Section titled “Examples”Bookmark a word, so a text reference can show it:
await doc.transaction(async ({ draft }) => { const paragraph = draft.body().content().blocklevels()[0]; if (paragraph?.type !== 'paragraph') return false; const textView = paragraph.asTextView(); const hit = textView.searchText('Introduction'); if (!hit) return false; draft.bookmarks().add({ name: 'Intro', anchor: { type: 'text', span: { from: { textView, range: hit.range } } } }); return true;});Mark the start of a paragraph, so a page reference can show its page:
await doc.transaction(async ({ draft }) => { const paragraph = draft.body().content().blocklevels()[3]; if (paragraph?.type !== 'paragraph') return false; draft.bookmarks().add({ name: 'Results', anchor: { type: 'point', point: { textView: paragraph.asTextView(), placement: 'before' } } }); return true;});