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" /> +