* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types, the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and the pages side by side, or one of them. The demos a page includes are live too. Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain. Running them showed docs that were wrong, fixed here: - Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page marked as landscape - Comments: `comments.children` takes plain objects, not `new Comment(...)` - Custom properties are text only - APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...), createParagraph().heading1(), a border's `value`, TextRun's `.break()` - The templates and patcher examples read files that aren't in the repository. They now patch the templates in demo/assets, or make their own `npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document "ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example now awaits patchDocument and writes the document it makes, as the example after it does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
5.8 KiB
Hyperlinks
!> Hyperlinks require an understanding of Paragraphs and Text.
There are two types of hyperlinks: internal (pointing to a bookmark inside the document) and external (pointing to an external url).
Internal
To create an internal hyperlink you need first to create a Bookmark, which contains the content that will be the destination of the hyperlink.
A bookmark is composed of an anchor (an identifier) and the text displayed. After creating a bookmark just add it to a paragraph. For example, creating a bookmarked heading:
import { Bookmark, Document, HeadingLevel, Paragraph, TextRun } from "docx";
const chapter1 = new Paragraph({
heading: HeadingLevel.HEADING_1,
children: [
new Bookmark({
id: "anchorForChapter1",
children: [new TextRun("Chapter 1")],
}),
],
});
const doc = new Document({
sections: [
{
children: [chapter1],
},
],
});
Then you can create an hyperlink pointing to that bookmark with an InternalHyperLink:
import { Bookmark, Document, HeadingLevel, InternalHyperlink, Paragraph, TextRun } from "docx";
const link = new InternalHyperlink({
children: [
new TextRun({
text: "See Chapter 1",
style: "Hyperlink",
}),
],
anchor: "anchorForChapter1",
});
const doc = new Document({
sections: [
{
children: [
new Paragraph({ children: [link] }),
new Paragraph({
heading: HeadingLevel.HEADING_1,
pageBreakBefore: true,
children: [new Bookmark({ id: "anchorForChapter1", children: [new TextRun("Chapter 1")] })],
}),
],
},
],
});
Page reference
You can also get the page number of the bookmark by creating a page reference to it:
import { Bookmark, Document, HeadingLevel, PageReference, Paragraph, TextRun } from "docx";
const paragraph = new Paragraph({
children: [new TextRun("Chapter 1 can be seen on page "), new PageReference("anchorForChapter1")],
});
const doc = new Document({
sections: [
{
children: [
paragraph,
new Paragraph({
heading: HeadingLevel.HEADING_1,
pageBreakBefore: true,
children: [new Bookmark({ id: "anchorForChapter1", children: [new TextRun("Chapter 1")] })],
}),
],
},
],
});
Numbered item reference
You can also create cross references for numbered items with NumberedItemReference.
import { AlignmentType, Bookmark, Document, LevelFormat, NumberedItemReference, Paragraph, TextRun } from "docx";
const paragraph = new Paragraph({
children: [new TextRun("See Paragraph "), new NumberedItemReference("anchorForParagraph1", "1.1")],
});
const doc = new Document({
numbering: {
config: [
{
reference: "paragraph-numbering",
levels: [
{ level: 0, format: LevelFormat.DECIMAL, text: "%1.", alignment: AlignmentType.START },
{ level: 1, format: LevelFormat.DECIMAL, text: "%1.%2.", alignment: AlignmentType.START },
],
},
],
},
sections: [
{
children: [
paragraph,
new Paragraph({ text: "Introduction", numbering: { reference: "paragraph-numbering", level: 0 } }),
new Paragraph({
numbering: { reference: "paragraph-numbering", level: 1 },
children: [new Bookmark({ id: "anchorForParagraph1", children: [new TextRun("The paragraph being referenced")] })],
}),
],
},
],
});
Note
The
NumberedItemReferencecurrently needs a cached value (in this case1.1)
External
To create an external hyperlink you just need to specify the url and the text of the link, then add it to a paragraph:
import { Document, ExternalHyperlink, Paragraph, TextRun } from "docx";
const paragraph = new Paragraph({
children: [
new ExternalHyperlink({
children: [
new TextRun({
text: "This is an external link!",
style: "Hyperlink",
}),
],
link: "https://docx.js.org",
}),
],
});
const doc = new Document({
sections: [
{
children: [paragraph],
},
],
});
Related Topics
- Bookmarks - For detailed coverage of bookmarks, internal hyperlinks, and page references
Demo
Source: https://github.com/dolanmiu/docx/blob/master/demo/links-and-bookmarks/hyperlinks.ts
Styling hyperlinks
It is possible to set the style of the text of both internal and external hyperlinks. This can be done applying run formatting on any of the TextRun children of the hyperlink. Use the style: "Hyperlink" property to show the default link styles, which can be combined with any other style.
Example:
import { Document, ExternalHyperlink, Paragraph, TextRun } from "docx";
const styledLink = new ExternalHyperlink({
children: [
new TextRun({
text: "This is a ",
style: "Hyperlink",
}),
new TextRun({
text: "bold",
bold: true,
style: "Hyperlink",
}),
new TextRun({
text: " link!",
style: "Hyperlink",
}),
],
link: "https://docx.js.org",
});
const doc = new Document({
sections: [
{
children: [new Paragraph({ children: [styledLink] })],
},
],
});