From c169dd1a833feee5ffedb204cd3f85ce830296be Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 28 Sep 2026 13:00:29 +0100 Subject: [PATCH] feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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) 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) Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) --- .cspell.json | 1 + .github/workflows/github-pages.yml | 1 + .gitignore | 1 + docs/README.md | 2 +- docs/contribution-guidelines.md | 25 + docs/index.html | 2 + docs/live-examples/live-examples.css | 137 ++ docs/live-examples/live-examples.js | 637 ++++++++ docs/quickstart.md | 151 +- docs/troubleshooting.md | 316 ++-- docs/usage/bookmarks.md | 116 +- docs/usage/bullet-points.md | 38 +- docs/usage/change-tracking.md | 220 ++- docs/usage/chart-axes.md | 278 +++- docs/usage/chart-bubble.md | 105 +- docs/usage/chart-colors.md | 77 +- docs/usage/chart-column-and-bar.md | 149 +- docs/usage/chart-combo.md | 135 +- docs/usage/chart-data-labels.md | 242 ++- docs/usage/chart-data.md | 121 +- docs/usage/chart-fonts-and-fills.md | 86 +- docs/usage/chart-line-and-area.md | 178 ++- docs/usage/chart-mermaid.md | 82 +- docs/usage/chart-pie-and-doughnut.md | 184 ++- docs/usage/chart-radar.md | 60 +- docs/usage/chart-scatter.md | 113 +- docs/usage/chart-size-and-position.md | 211 ++- docs/usage/chart-stock.md | 117 +- docs/usage/chart-templates.md | 123 +- docs/usage/chart-titles-and-legends.md | 173 ++- docs/usage/chart-trendlines-and-error-bars.md | 127 +- docs/usage/charts.md | 2 +- docs/usage/checkboxes.md | 88 +- docs/usage/columns.md | 84 +- docs/usage/comments.md | 164 +- docs/usage/convenience-functions.md | 155 +- docs/usage/document.md | 85 +- docs/usage/endnotes.md | 4 +- docs/usage/fields.md | 60 +- docs/usage/fonts.md | 140 +- docs/usage/footnotes.md | 14 +- docs/usage/headers-and-footers.md | 215 ++- docs/usage/hyperlinks.md | 104 +- docs/usage/images.md | 293 +++- docs/usage/line-numbers.md | 135 +- docs/usage/math.md | 898 ++++++++--- docs/usage/numbering.md | 182 ++- docs/usage/packers.md | 76 +- docs/usage/page-layout.md | 61 +- docs/usage/page-numbers.md | 150 +- docs/usage/paragraph.md | 428 +++-- docs/usage/patcher.md | 23 +- docs/usage/sections.md | 48 +- docs/usage/shape-adjustments.md | 23 +- docs/usage/shapes.md | 1384 +++++++++++++---- docs/usage/styling-with-js.md | 64 +- docs/usage/styling-with-xml.md | 46 +- docs/usage/symbols.md | 62 +- docs/usage/table-of-contents.md | 33 +- docs/usage/tables.md | 389 ++++- docs/usage/tabs.md | 137 +- docs/usage/templates.md | 286 ++-- docs/usage/text-box.md | 194 ++- docs/usage/text-frames.md | 135 +- docs/usage/text.md | 410 ++++- docs/usage/themes.md | 63 +- docs/usage/watermarks.md | 166 +- docs/usage/wps-text-box.md | 258 ++- eslint.config.ts | 37 +- package.json | 1 + 70 files changed, 8901 insertions(+), 2404 deletions(-) create mode 100644 docs/live-examples/live-examples.css create mode 100644 docs/live-examples/live-examples.js diff --git a/.cspell.json b/.cspell.json index 556b6a2f5a..740b723a4c 100644 --- a/.cspell.json +++ b/.cspell.json @@ -91,6 +91,7 @@ "ignorePaths": [ "package.json", "docs/api", + "docs/live-examples/lib", "*.docx", "build" ], diff --git a/.github/workflows/github-pages.yml b/.github/workflows/github-pages.yml index 479b3d6168..a9b580f091 100644 --- a/.github/workflows/github-pages.yml +++ b/.github/workflows/github-pages.yml @@ -16,6 +16,7 @@ jobs: - name: Build 🔧 run: | npm run typedoc + npm run build.docs echo "docx.js.org" > docs/.nojekyll echo "docx.js.org" > docs/CNAME - name: Archive Production Artifact diff --git a/.gitignore b/.gitignore index ef25b06c45..c2b77015df 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ build # Documentation docs/api/ docs/.nojekyll +docs/live-examples/lib/ # VSCode .vscode/* diff --git a/docs/README.md b/docs/README.md index 39085b5d2a..a8bf780ab4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -50,7 +50,7 @@ import { ChartRun } from "docx/charts"; ## Basic Usage -```ts +```ts live import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "docx"; diff --git a/docs/contribution-guidelines.md b/docs/contribution-guidelines.md index 6d6d95274f..b7eed4a3ae 100644 --- a/docs/contribution-guidelines.md +++ b/docs/contribution-guidelines.md @@ -302,3 +302,28 @@ describe("ClassName", () => { }); }); ``` + +## Live examples in the docs + +The code examples in the docs are live: the page runs each one, draws the document it makes next to the code, and lets readers edit the code and download the `.docx`. Write an example that makes a document as a whole file with its imports, and mark the code block `ts live`: + +````md +```ts live +import { Document, Paragraph } from "docx"; + +const doc = new Document({ + sections: [{ children: [new Paragraph("Hello World")] }], +}); +``` +```` + +The last `Document` the example makes is the one shown, so it doesn't need `Packer`. It can still end with `Packer.toBuffer` and `fs.writeFileSync` as in Node, and read files in the `demo` folder with `fs.readFileSync`, such as `./demo/assets/images/pizza.gif`. The demos a page includes from the `demo` folder are live without being marked. Code that isn't about the document, such as a server handler or bundler setup, stays a plain `ts` block. + +The preview is drawn by [docx-preview](https://github.com/VolodymyrBaydalka/docxjs), which leaves out shapes, charts and watermarks, so check those in Word. + +To see the live examples locally, build docx for the docs, then serve them: + +```terminal +npm run build.docs +npm run serve.docs +``` diff --git a/docs/index.html b/docs/index.html index 6dd429e382..baedf3ed2f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -12,6 +12,7 @@ title="docsify-darklight-theme" type="text/css" /> +