mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-14 06:35:17 +00:00
Compare commits
488 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 63971b9803 | |||
| 64455b55e4 | |||
| 68308154b5 | |||
| 36af03f4d5 | |||
| cbc71a56b6 | |||
| 1420180220 | |||
| e1b2cf6d81 | |||
| c0e9c49679 | |||
| afc4bd4415 | |||
| bcfa8d8b19 | |||
| 43766d6519 | |||
| 42c17f5c74 | |||
| d655d3eece | |||
| 3500de95e8 | |||
| 7f03b94949 | |||
| f0cf060191 | |||
| 5a137e7b14 | |||
| 28f7fbaa8a | |||
| ff96bb5787 | |||
| 87841124e0 | |||
| a650b58276 | |||
| 388b20d44f | |||
| ea7e94fc51 | |||
| d30bcbabdb | |||
| fb858a1941 | |||
| cc2e615b9a | |||
| a862cde6b4 | |||
| e2b7bec162 | |||
| c1b577ecc9 | |||
| ba7ec14f76 | |||
| 9f0117723c | |||
| f72b6a20db | |||
| 2f798209bb | |||
| eec8f16424 | |||
| 885264f132 | |||
| f1a6dec951 | |||
| 44f41510fa | |||
| f220229e5e | |||
| 61b4cb8d7c | |||
| 185cdb4883 | |||
| 2cdba53451 | |||
| 950f6c36cf | |||
| ea37977b1e | |||
| 81da82aed7 | |||
| 39a3518651 | |||
| d903273759 | |||
| d130532f69 | |||
| 94fcd21f34 | |||
| 1d2db4ae2b | |||
| d270b4af9b | |||
| ccfbd3c1a8 | |||
| 5c4923557f | |||
| b1f608b865 | |||
| c7993756dd | |||
| d02465823f | |||
| 6b9f6b5b79 | |||
| df7de8fb0c | |||
| 72f5ce3985 | |||
| 04c80262e2 | |||
| 56d1b8fe2d | |||
| 054a1634ab | |||
| 6ff63f2db6 | |||
| b5ad0ce1e5 | |||
| 2ba7e54c81 | |||
| 0a858e74ad | |||
| b0c534fc2c | |||
| 250c194a02 | |||
| 5b10cde936 | |||
| 4aeb81bfa5 | |||
| 955cebf944 | |||
| 7c951ef437 | |||
| 68f0dbcc35 | |||
| 39c4f76aea | |||
| 2fe58e8cbb | |||
| 765141a4d8 | |||
| eb1bd3aa01 | |||
| b7284b0223 | |||
| 5bbd254742 | |||
| 611e28626a | |||
| 2a3bb0eb22 | |||
| 6816f0fa5a | |||
| 17386776ec | |||
| 9ee9082614 | |||
| 334cfdd522 | |||
| 8c961a5dbd | |||
| 4708956e77 | |||
| 813dd03dbb | |||
| 0522dbc61e | |||
| acb9f53156 | |||
| 6016846156 | |||
| cf1fae5837 | |||
| 7212345466 | |||
| e38caf78ff | |||
| 0a8dd67541 | |||
| ed684c19a9 | |||
| 52ab3681e3 | |||
| 9cc5a787e9 | |||
| 0003030bf5 | |||
| 9ad8f45d38 | |||
| a3855257c8 | |||
| 9169c2f053 | |||
| abe028bd38 | |||
| 12c2451f4f | |||
| 8daede5427 | |||
| 4c89aa60eb | |||
| d0a3f64ae4 | |||
| 433d66482d | |||
| bd553bcf9e | |||
| f5894bcf74 | |||
| 1b59e7ce36 | |||
| 32d6f5845d | |||
| 4f5cbfe732 | |||
| afba8fcb34 | |||
| 377a4899e4 | |||
| 938bdeb34b | |||
| d216eaa3f6 | |||
| 5ca5da0f21 | |||
| ebe6eb3b39 | |||
| 3bfe347096 | |||
| 7b38f89078 | |||
| 09b689b0ea | |||
| 2350baa75f | |||
| bc1eca815e | |||
| 78c10340e7 | |||
| c16890a3a6 | |||
| 3b4fe2827a | |||
| 7e807aabb5 | |||
| 973c74ee69 | |||
| efed0b0617 | |||
| ca3b9aca5d | |||
| e2afc07ab2 | |||
| e8fb84d362 | |||
| 57f951a7d9 | |||
| 28008667ed | |||
| f3affc3034 | |||
| 59da30f3c6 | |||
| 2db721112a | |||
| b60039b7d5 | |||
| 8fe9c9afea | |||
| 216ba7a556 | |||
| a2ff57081b | |||
| 40dbd9a14b | |||
| e5bac69c5c | |||
| 8d6598393f | |||
| 59b86eb9dc | |||
| 52c9f6da2f | |||
| dd65987c94 | |||
| ace61901a4 | |||
| e6c3c7635d | |||
| 5134d9e7f8 | |||
| 6821fb2937 | |||
| cfe4812045 | |||
| 14843ac343 | |||
| 8fb6d7465f | |||
| 0ef647586f | |||
| b4039627d9 | |||
| 4a295e6db4 | |||
| 5726999a5a | |||
| 58d7f3c140 | |||
| 07eab986d5 | |||
| 711cf38f9b | |||
| 4f7c0eea9f | |||
| f3448de2da | |||
| ec92075ef8 | |||
| 1e013cd87c | |||
| f033734308 | |||
| 50c05164f7 | |||
| 7fb9004b43 | |||
| 4721403986 | |||
| df848ef64e | |||
| 0b40ccd286 | |||
| caaa692f0b | |||
| 42d43e09c4 | |||
| 7ccbf55ba7 | |||
| 81f5bfc2f9 | |||
| a7a713bca2 | |||
| d6613c787f | |||
| 427f748e1c | |||
| 4f5fec7213 | |||
| 8c0a53ab40 | |||
| 2dfd5aab20 | |||
| 8d3c6562a2 | |||
| dae019c115 | |||
| 73e0cbb2d6 | |||
| 392f59450c | |||
| b4918f60ce | |||
| 8f7c304d58 | |||
| a3a944d7dd | |||
| 88a35ed057 | |||
| 67998b6f15 | |||
| ff6d1150a5 | |||
| 500c8cb649 | |||
| 6859f7d239 | |||
| dfa8a37be1 | |||
| 11a6511b7a | |||
| af98402655 | |||
| b7a0db3339 | |||
| 382a19885b | |||
| d99da6a3ae | |||
| 72cd0e59e6 | |||
| 42d88da73c | |||
| 2863fe0dc1 | |||
| 87218062be | |||
| 8a3910e208 | |||
| 6294bbb53c | |||
| a28a997851 | |||
| 28a9ee701e | |||
| 50e8b2ec52 | |||
| 315717fa2d | |||
| da67711c90 | |||
| fecdbe8dde | |||
| 9316ccd1f4 | |||
| c3b620e975 | |||
| 7a00880e5b | |||
| b6b597564d | |||
| d410381957 | |||
| 902c3c6c1a | |||
| 4fb2a4a6d7 | |||
| 2d64c78787 | |||
| fbfcca5dae | |||
| 664ae8bff6 | |||
| fa3d2aae08 | |||
| e7a591d9f6 | |||
| 2860beacdc | |||
| 231167d3e1 | |||
| c0ee60ec13 | |||
| f58b904ba6 | |||
| d5992f1a5c | |||
| fa12f9eeb6 | |||
| 33726c88a0 | |||
| 6aa3ff9c5e | |||
| 7d3fe23195 | |||
| 521052d84f | |||
| c730845e17 | |||
| 7b5a3616c4 | |||
| c9373efdb5 | |||
| 015615de8a | |||
| 0f867e533a | |||
| 7d5a6d2255 | |||
| a1da4f023d | |||
| 666e8426e1 | |||
| df2fa42bda | |||
| b3a7ad6d2c | |||
| 2932077bf9 | |||
| 957afd967f | |||
| 4c9a9d0416 | |||
| 40df91a363 | |||
| af66ff71ed | |||
| 6f8ecd6111 | |||
| fa3eb07617 | |||
| dc4268db64 | |||
| a0c06a72cc | |||
| 19c20c0be3 | |||
| fae34e56ac | |||
| ba0094a862 | |||
| b259009c9c | |||
| d88dd5ed8a | |||
| 57bb146075 | |||
| 4b67fe5317 | |||
| 938125e772 | |||
| 1c8d9febb3 | |||
| 223edae17d | |||
| 12a455dab5 | |||
| d00dc8ca72 | |||
| 2c85a302fc | |||
| 3460b78b45 | |||
| 95a1f652f9 | |||
| cc37e2ae15 | |||
| 08382ce62f | |||
| 80cb52a237 | |||
| c6637b09e2 | |||
| 778624af00 | |||
| e15757d01f | |||
| aed79fd6c3 | |||
| 0c973a3292 | |||
| 56c923c0ed | |||
| 7d7806df30 | |||
| cb5598dc19 | |||
| 47f01edcb8 | |||
| e6ddc0f07f | |||
| 373f18f451 | |||
| 3f292065cd | |||
| 5e975ab95b | |||
| a3ec264764 | |||
| 5d504ffa4c | |||
| 04999662db | |||
| f7a34706c7 | |||
| f328a41982 | |||
| 20ebecb114 | |||
| 5a69692f54 | |||
| 42ca7e1580 | |||
| c4ebb3fecd | |||
| 2a805cc082 | |||
| 89a5816ee4 | |||
| c3f6b8c003 | |||
| 0e201d5899 | |||
| ae5f1abc58 | |||
| 580101d04e | |||
| 81b4a4db53 | |||
| 8339e91e2b | |||
| 3119066728 | |||
| 326e28e9b0 | |||
| 90f0127ada | |||
| 0353a8186c | |||
| dd043df00f | |||
| 97b7c79bc6 | |||
| 634e0b4302 | |||
| 2a23f1f003 | |||
| 88ffdcc303 | |||
| b6b09d42af | |||
| ebc39e9db9 | |||
| 0df3849ea2 | |||
| 8ed1bda2f2 | |||
| d67699a1f1 | |||
| 3363a18856 | |||
| ad1dc0b914 | |||
| e4c7307c99 | |||
| f9c942c8ff | |||
| eeb977fc03 | |||
| 4b8a62122a | |||
| e44e947fc1 | |||
| 7588cfe1c5 | |||
| a9a643f222 | |||
| 77d6cb75be | |||
| 1942d221df | |||
| 041601ed65 | |||
| 5567281a85 | |||
| 4c3753046b | |||
| 89f8e16184 | |||
| a3e1125ced | |||
| 4dae8acfed | |||
| bc081c4344 | |||
| e4275b235d | |||
| 77397cac82 | |||
| b6a08bc679 | |||
| 116575c811 | |||
| 4ba7bd5ce1 | |||
| 223ab5377f | |||
| ae43ca146c | |||
| 42342893d6 | |||
| eedefdd5c9 | |||
| f5e152dd74 | |||
| 95ea22d9c5 | |||
| f23023ddfa | |||
| da74077954 | |||
| 29aaba5949 | |||
| daf41fc728 | |||
| b08eb1af59 | |||
| 8322cd3e35 | |||
| 444039c142 | |||
| 3b0eff7f71 | |||
| c765463e38 | |||
| e8ca25b67e | |||
| 528eee3fc6 | |||
| 168a4fa998 | |||
| 3ea75adba4 | |||
| cbd768a095 | |||
| 2d01653a78 | |||
| aa3357a57d | |||
| de53946896 | |||
| d51204148e | |||
| 319761a39e | |||
| 7fbf01a348 | |||
| c22490bd80 | |||
| 860fd6d02e | |||
| c43c524804 | |||
| 66bb938d85 | |||
| 0e6d928496 | |||
| f07982d9f8 | |||
| b92ecfad58 | |||
| 416bde70bc | |||
| 580f7ade15 | |||
| cdffd7c47b | |||
| 5a959cf041 | |||
| 31466546a2 | |||
| 90ead98eaa | |||
| 65e9cb9d5c | |||
| e59076a074 | |||
| 653920dec9 | |||
| 23cedd2d8b | |||
| 70c4182fd8 | |||
| b62b101065 | |||
| b9c929beeb | |||
| 2b6c593289 | |||
| c0b339e406 | |||
| da485f5144 | |||
| 139a8050a5 | |||
| da7b369ffd | |||
| 432da64919 | |||
| 7d0b422200 | |||
| fb90eb0acc | |||
| b647538e41 | |||
| e50797a5c1 | |||
| 5b1e01c786 | |||
| 813b2af06b | |||
| 6f71da88d8 | |||
| fa91eb7eaf | |||
| c756761591 | |||
| 434af90366 | |||
| 0f41e194cb | |||
| bd353489e9 | |||
| 77fd393f15 | |||
| 8701b5e078 | |||
| 61db166f92 | |||
| 0d9b9cea0f | |||
| 7bb37c7f0a | |||
| 5b2bf828e3 | |||
| c85d3f9752 | |||
| 4d7088bbf0 | |||
| dd6ab6044c | |||
| d236bf029c | |||
| d70d566abe | |||
| a25fdedea9 | |||
| 373183a2cd | |||
| e9fa50dfb5 | |||
| 57ca4e0e88 | |||
| bc90adb73a | |||
| 1505ddbd5a | |||
| cd99ed57fb | |||
| 27de1cd01d | |||
| ae78fc5394 | |||
| 40e8e69c6d | |||
| e84a46a558 | |||
| 18a72d4466 | |||
| d7aa5fa003 | |||
| 27532a5f41 | |||
| 48c18c03b9 | |||
| bba2e52e24 | |||
| 54ee0149e1 | |||
| 8fda9d2fab | |||
| 77a72d79ce | |||
| 73e2b472ef | |||
| 2f28cc1323 | |||
| f5516d4e21 | |||
| d2facb2db1 | |||
| 6eae764b7c | |||
| 970ef8f886 | |||
| ed0720699e | |||
| ad94d00d5f | |||
| c7843f7f52 | |||
| 1fe3286bdb | |||
| ce030fdf76 | |||
| 9ba8783952 | |||
| 2f3966cc53 | |||
| 8f65a22e13 | |||
| 2787d884cd | |||
| fc00b51683 | |||
| f80c3a7b37 | |||
| 4e5d6c7487 | |||
| 96f84b5917 | |||
| 6b8a5fdd1b | |||
| 89083ac790 | |||
| 70be2c6b2b | |||
| bcf877841b | |||
| a84b06bb19 | |||
| 24b7808d9a | |||
| 8364ee988e | |||
| 07f6e2d233 | |||
| 7212973f34 | |||
| af44e0436b | |||
| 3f9c35d505 | |||
| 2c13f8b95c | |||
| 4dab1c56e5 | |||
| 4723f03ef1 | |||
| 278689e0a0 | |||
| 886e204e68 | |||
| c59947ab39 | |||
| 4f0a772a21 | |||
| 903f962673 | |||
| 31d800e48b | |||
| c1c58eb919 | |||
| eec3eed100 | |||
| 99da8df3d1 | |||
| c9ea23919e | |||
| 16292def79 | |||
| 9108c56ca1 | |||
| b011ea0af8 | |||
| d33171b779 | |||
| 6aaeae269e | |||
| e5f0cc8663 | |||
| 9b5f971aa1 | |||
| ff3b708ad3 | |||
| e996904013 | |||
| f32bf1f15d | |||
| 6cda2dd57b | |||
| c60e9ba27f | |||
| 9bc3d504c8 | |||
| b5e4fd75e9 |
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve `Button` and `ButtonGroup` styling
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix scroll of page outline
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Support "objectivec" as alias for Objective C syntax
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Support custom AI providers
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@gitbook/browser-types": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
First version of the public package for typing script integrations.
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes to `PageAside`
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Tweaks to AIChatButton and AIChatInput
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Allow integrations to provide tools to the Docs Assistant
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve vertical alignment of site items and fix floating page aside
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix search bar layout shift caused by ToC
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Emit a <link rel="alternate"> for the markdown version of a page
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'gitbook': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Support new coverDefinitionDark for cards & image type
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Clear AI chat properly
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix bug in search highlight in GBO
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@gitbook/openapi-parser": patch
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve OpenAPI parsing errors
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix Custom logo not rendering on the published site
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Changes to the API data cache functions can invalidate all existing data cache
|
||||||
|
# causing a massive amount of revalidation, impacting our API.
|
||||||
|
packages/gitbook/src/lib/data/api.ts @SamyPesse
|
||||||
+29
-6
@@ -53,19 +53,42 @@ After forking this repository, you'll want to [create a branch](https://docs.git
|
|||||||
|
|
||||||
#### 3. Install dependencies and run the project locally
|
#### 3. Install dependencies and run the project locally
|
||||||
|
|
||||||
GitBook uses [Bun](https://bun.sh/) to run the project. Make sure you're using the specified version of `node` before running any of the development commands to ensure a smooth development experience.
|
##### Prerequisites:
|
||||||
|
- Node.js (Version: >=20.6)
|
||||||
|
- Use `nvm` for easy Node management
|
||||||
|
- [Bun](https://bun.sh/) (Version: >=1.2.15)
|
||||||
|
- We use a text-based lockfile which isn't supported below 1.2.15
|
||||||
|
|
||||||
You can easily do this by running the command `nvm use`.
|
##### Setup steps:
|
||||||
|
|
||||||
To start your local version of GitBook, run the command `bun dev`.
|
1. Ensure you are using the project's version of Node:
|
||||||
|
```bash
|
||||||
|
nvm use
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Install dependencies using Bun:
|
||||||
|
```bash
|
||||||
|
bun install
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Start the development server:
|
||||||
|
```bash
|
||||||
|
bun dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Additional development commands:
|
||||||
|
- `bun format`: Format the code using Biome
|
||||||
|
- `bun typecheck`: Run TypeScript type checking
|
||||||
|
- `bun unit`: Run unit tests
|
||||||
|
- `bun e2e`: Run end-to-end tests
|
||||||
|
|
||||||
#### 4. Preview your changes
|
#### 4. Preview your changes
|
||||||
|
|
||||||
When running the development server, published GitBook sites can be rendered through your local version at `http://localhost:3000/`.
|
When running the development server, published GitBook sites can be rendered through your local version at `http://localhost:3000/url`.
|
||||||
|
|
||||||
For example, our published docs can be viewed using the local version by visiting `http://localhost:3000/docs.gitbook.com` after running the development server.
|
For example, our published docs can be viewed using the local version by visiting `http://localhost:3000/url/gitbook.com/docs` after running the development server.
|
||||||
|
|
||||||
You can visit any published GitBook site behind your development server. Please make sure your site is [published publicly](https://docs.gitbook.com/published-documentation/publish-your-content-as-a-docs-site) to ensure you can view the site correctly in your development version.
|
You can visit any published GitBook site behind your development server. Please make sure your site is [published publicly](https://gitbook.com/docs/published-documentation/publish-your-content-as-a-docs-site) to ensure you can view the site correctly in your development version.
|
||||||
|
|
||||||
### Commit your update
|
### Commit your update
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
name: Gradual Deploy to Cloudflare
|
||||||
|
description: Use gradual deployment to deploy to Cloudflare. This action will upload the middleware and server versions to Cloudflare and kept them bound together
|
||||||
|
inputs:
|
||||||
|
apiToken:
|
||||||
|
description: 'Cloudflare API token'
|
||||||
|
required: true
|
||||||
|
accountId:
|
||||||
|
description: 'Cloudflare account ID'
|
||||||
|
required: true
|
||||||
|
environment:
|
||||||
|
description: 'Cloudflare environment to deploy to (staging, production, preview)'
|
||||||
|
required: true
|
||||||
|
middlewareVersionId:
|
||||||
|
description: 'Middleware version ID to deploy'
|
||||||
|
required: true
|
||||||
|
serverVersionId:
|
||||||
|
description: 'Server version ID to deploy'
|
||||||
|
required: true
|
||||||
|
outputs:
|
||||||
|
deployment-url:
|
||||||
|
description: "Deployment URL"
|
||||||
|
value: ${{ steps.deploy_middleware.outputs.deployment-url }}
|
||||||
|
runs:
|
||||||
|
using: 'composite'
|
||||||
|
steps:
|
||||||
|
- id: wrangler_status
|
||||||
|
name: Check wrangler deployment status
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: deployments status --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||||
|
|
||||||
|
# This step is used to get the version ID that is currently deployed to Cloudflare.
|
||||||
|
- id: extract_current_version
|
||||||
|
name: Extract current version
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
version_id=$(echo "${{ steps.wrangler_status.outputs.command-output }}" | grep -A 3 "(100%)" | grep -oP '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
|
||||||
|
echo "version_id=$version_id" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- id: deploy_server
|
||||||
|
name: Deploy server to Cloudflare at 0%
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: versions deploy ${{ steps.extract_current_version.outputs.version_id }}@100% ${{ inputs.serverVersionId }}@0% -y --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||||
|
|
||||||
|
# Since we use version overrides headers, we can directly deploy the middleware to 100%.
|
||||||
|
- id: deploy_middleware
|
||||||
|
name: Deploy middleware to Cloudflare at 100%
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: versions deploy ${{ inputs.middlewareVersionId }}@100% -y --config ./packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc
|
||||||
|
|
||||||
|
- name: Deploy server to Cloudflare at 100%
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: versions deploy ${{ inputs.serverVersionId }}@100% -y --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||||
|
|
||||||
|
- name: Outputs
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
DEPLOYMENT_URL: ${{ steps.deploy_middleware.outputs.deployment-url }}
|
||||||
|
run: |
|
||||||
|
echo "URL: ${{ steps.deploy_middleware.outputs.deployment-url }}"
|
||||||
@@ -19,10 +19,16 @@ inputs:
|
|||||||
deploy:
|
deploy:
|
||||||
description: 'Deploy as main version for all traffic instead of uploading versions'
|
description: 'Deploy as main version for all traffic instead of uploading versions'
|
||||||
required: true
|
required: true
|
||||||
|
commitTag:
|
||||||
|
description: 'Commit branch to associate with the deployment'
|
||||||
|
required: true
|
||||||
|
commitMessage:
|
||||||
|
description: 'Commit message to associate with the deployment'
|
||||||
|
required: true
|
||||||
outputs:
|
outputs:
|
||||||
deployment-url:
|
deployment-url:
|
||||||
description: "Deployment URL"
|
description: "Deployment URL"
|
||||||
value: ${{ steps.deploy.outputs.deployment-url }}
|
value: ${{ steps.upload_middleware.outputs.deployment-url }}
|
||||||
runs:
|
runs:
|
||||||
using: 'composite'
|
using: 'composite'
|
||||||
steps:
|
steps:
|
||||||
@@ -44,27 +50,89 @@ runs:
|
|||||||
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
||||||
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
||||||
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
||||||
|
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
|
||||||
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
||||||
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
||||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
||||||
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
||||||
|
GITBOOK_IMAGE_RESIZE_MODE: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_MODE
|
||||||
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
||||||
|
GITBOOK_FONTS_URL: ${{ inputs.opItem }}/GITBOOK_FONTS_URL
|
||||||
- name: Build worker
|
- name: Build worker
|
||||||
run: bun run turbo build:v2:cloudflare
|
run: bun run turbo build:cloudflare
|
||||||
|
env:
|
||||||
|
GITBOOK_RUNTIME: cloudflare
|
||||||
shell: bash
|
shell: bash
|
||||||
- id: deploy
|
|
||||||
name: Deploy to Cloudflare
|
- name: Upload the DO worker
|
||||||
uses: cloudflare/wrangler-action@v3.14.0
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ inputs.apiToken }}
|
apiToken: ${{ inputs.apiToken }}
|
||||||
accountId: ${{ inputs.accountId }}
|
accountId: ${{ inputs.accountId }}
|
||||||
workingDirectory: ./
|
workingDirectory: ./
|
||||||
wranglerVersion: '3.112.0'
|
wranglerVersion: '4.10.0'
|
||||||
environment: ${{ inputs.environment }}
|
environment: ${{ inputs.environment }}
|
||||||
command: ${{ fromJSON(inputs.deploy) == true && 'deploy' || 'versions upload' }} --config ./packages/gitbook-v2/wrangler.toml
|
command: deploy --config ./packages/gitbook/openNext/customWorkers/doWrangler.jsonc
|
||||||
|
|
||||||
|
- id: upload_server
|
||||||
|
name: Upload server to Cloudflare
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||||
|
|
||||||
|
- name: Extract server version worker ID
|
||||||
|
shell: bash
|
||||||
|
id: extract_server_version_id
|
||||||
|
run: |
|
||||||
|
version_id=$(echo '${{ steps.upload_server.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
|
||||||
|
echo "version_id=$version_id" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Run updateWrangler scripts
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
bun run ./packages/gitbook/openNext/customWorkers/script/updateWrangler.ts ${{ steps.extract_server_version_id.outputs.version_id }}
|
||||||
|
|
||||||
|
- id: upload_middleware
|
||||||
|
name: Upload middleware to Cloudflare
|
||||||
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
workingDirectory: ./
|
||||||
|
wranglerVersion: '4.10.0'
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc
|
||||||
|
|
||||||
|
- name: Extract middleware version worker ID
|
||||||
|
shell: bash
|
||||||
|
id: extract_middleware_version_id
|
||||||
|
run: |
|
||||||
|
version_id=$(echo '${{ steps.upload_middleware.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
|
||||||
|
echo "version_id=$version_id" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Deploy server and middleware to Cloudflare
|
||||||
|
if: ${{ inputs.deploy == 'true' }}
|
||||||
|
uses: ./.github/actions/gradual-deploy-cloudflare
|
||||||
|
with:
|
||||||
|
apiToken: ${{ inputs.apiToken }}
|
||||||
|
accountId: ${{ inputs.accountId }}
|
||||||
|
opServiceAccount: ${{ inputs.opServiceAccount }}
|
||||||
|
opItem: ${{ inputs.opItem }}
|
||||||
|
environment: ${{ inputs.environment }}
|
||||||
|
serverVersionId: ${{ steps.extract_server_version_id.outputs.version_id }}
|
||||||
|
middlewareVersionId: ${{ steps.extract_middleware_version_id.outputs.version_id }}
|
||||||
|
deploy: ${{ inputs.deploy }}
|
||||||
|
|
||||||
|
|
||||||
- name: Outputs
|
- name: Outputs
|
||||||
shell: bash
|
shell: bash
|
||||||
env:
|
env:
|
||||||
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
|
DEPLOYMENT_URL: ${{ steps.upload_middleware.outputs.deployment-url }}
|
||||||
run: |
|
run: |
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
echo "URL: ${{ steps.upload_middleware.outputs.deployment-url }}"
|
||||||
|
echo "Output server: ${{ steps.upload_server.outputs.command-output }}"
|
||||||
@@ -49,17 +49,21 @@ runs:
|
|||||||
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
|
||||||
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
|
||||||
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
|
||||||
|
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
|
||||||
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
|
||||||
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
|
||||||
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
|
||||||
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
|
||||||
|
GITBOOK_IMAGE_RESIZE_MODE: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_MODE
|
||||||
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
|
||||||
|
GITBOOK_FONTS_URL: ${{ inputs.opItem }}/GITBOOK_FONTS_URL
|
||||||
- name: Build Project Artifacts
|
- name: Build Project Artifacts
|
||||||
run: bun run vercel build --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
|
run: bun run vercel build --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
|
||||||
shell: bash
|
shell: bash
|
||||||
env:
|
env:
|
||||||
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
|
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
|
||||||
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
|
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
|
||||||
|
GITBOOK_RUNTIME: vercel
|
||||||
- name: Deploy Project Artifacts to Vercel
|
- name: Deploy Project Artifacts to Vercel
|
||||||
id: deploy
|
id: deploy
|
||||||
shell: bash
|
shell: bash
|
||||||
@@ -73,3 +77,4 @@ runs:
|
|||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||||
|
|
||||||
|
|||||||
@@ -3,15 +3,6 @@ description: 'Install Bun and cache dependencies'
|
|||||||
runs:
|
runs:
|
||||||
using: 'composite'
|
using: 'composite'
|
||||||
steps:
|
steps:
|
||||||
# We cache dependencies even if Bun is fast to avoid downloading fontawesome too many times
|
|
||||||
# and impacting bandwidth there.
|
|
||||||
- name: Cache bun
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ~/.bun/install/cache
|
|
||||||
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}-${{ hashFiles('**/bun.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-bun-
|
|
||||||
- name: Setup bun
|
- name: Setup bun
|
||||||
uses: oven-sh/setup-bun@v2
|
uses: oven-sh/setup-bun@v2
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -7,66 +7,10 @@ on:
|
|||||||
env:
|
env:
|
||||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||||
jobs:
|
jobs:
|
||||||
deploy-v1-cloudflare:
|
|
||||||
name: Deploy v1 to Cloudflare Pages
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment:
|
|
||||||
name: ${{ github.ref == 'refs/heads/main' && '1c-production' || '1c-preview' }}
|
|
||||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
deployments: write
|
|
||||||
issues: write
|
|
||||||
pull-requests: write
|
|
||||||
checks: write
|
|
||||||
statuses: write
|
|
||||||
outputs:
|
|
||||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
|
||||||
deployment-alias-url: ${{ steps.deploy.outputs.deployment-alias-url }}
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: Setup Bun
|
|
||||||
uses: ./.github/composite/setup-bun
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
env:
|
|
||||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
|
||||||
- name: Sets env vars for production
|
|
||||||
if: github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
|
||||||
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
|
|
||||||
echo "GITBOOK_ASSETS_PREFIX=https://static.gitbook.com" >> $GITHUB_ENV
|
|
||||||
- name: Sets env vars for preview
|
|
||||||
if: github.ref != 'refs/heads/main'
|
|
||||||
run: |
|
|
||||||
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
|
|
||||||
- name: Build Next.js with next-on-pages
|
|
||||||
run: bun run turbo gitbook#build:cloudflare
|
|
||||||
env:
|
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
|
||||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
|
||||||
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
|
|
||||||
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
|
|
||||||
SENTRY_DSN: ${{ vars.SENTRY_DSN }}
|
|
||||||
SENTRY_RELEASE: ${{ github.sha }}
|
|
||||||
- id: deploy
|
|
||||||
name: Deploy to Cloudflare
|
|
||||||
uses: cloudflare/wrangler-action@v3.14.0
|
|
||||||
with:
|
|
||||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
||||||
workingDirectory: ./
|
|
||||||
wranglerVersion: '3.112.0'
|
|
||||||
command: pages deploy ./packages/gitbook/.vercel/output/static --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.ref == 'refs/heads/main' && 'main' || format('pr{0}', github.event.pull_request.number) }}
|
|
||||||
- name: Outputs
|
|
||||||
run: |
|
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
|
||||||
echo "Alias URL: ${{ steps.deploy.outputs.deployment-alias-url }}"
|
|
||||||
deploy-v2-vercel:
|
deploy-v2-vercel:
|
||||||
name: Deploy v2 to Vercel (preview)
|
name: Deploy v2 to Vercel (preview)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
environment:
|
environment:
|
||||||
name: 2v-preview
|
name: 2v-preview
|
||||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
outputs:
|
outputs:
|
||||||
@@ -87,11 +31,11 @@ jobs:
|
|||||||
deploy-v2-cloudflare:
|
deploy-v2-cloudflare:
|
||||||
name: Deploy v2 to Cloudflare Worker (preview)
|
name: Deploy v2 to Cloudflare Worker (preview)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
environment:
|
environment:
|
||||||
name: 2c-preview
|
name: 2c-preview
|
||||||
url: ${{ steps.deploy.outputs.deployment-url }}
|
url: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
outputs:
|
outputs:
|
||||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
deployment-url: ${{ steps.deploy.outputs.deployment-url || steps.extract-worker-id.outputs.worker-url }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -105,15 +49,24 @@ jobs:
|
|||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
opItem: op://gitbook-open/2c-preview
|
opItem: op://gitbook-open/2c-preview
|
||||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||||
|
commitTag: ${{ github.ref == 'refs/heads/main' && 'main' || format('pr{0}', github.event.pull_request.number) }}
|
||||||
|
commitMessage: ${{ github.sha }}
|
||||||
|
- name: Extract Worker ID
|
||||||
|
id: extract-worker-id
|
||||||
|
if: ${{ !steps.deploy.outputs.deployment-url }}
|
||||||
|
run: |
|
||||||
|
if [[ "${{ steps.deploy.outputs.command-output }}" =~ Worker\ Version\ ID:\ ([0-9a-f]{8})-([0-9a-f-]+) ]]; then
|
||||||
|
WORKER_ID_FIRST_PART="${BASH_REMATCH[1]}"
|
||||||
|
echo "worker-url=https://${WORKER_ID_FIRST_PART}-gitbook-open-v2-preview.gitbook.workers.dev/" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
- name: Outputs
|
- name: Outputs
|
||||||
run: |
|
run: |
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
echo "URL: ${{ steps.deploy.outputs.deployment-url || steps.extract-worker-id.outputs.worker-url }}"
|
||||||
comment-deployments:
|
comment-deployments:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Comment Deployments (preview)
|
name: Comment Deployments (preview)
|
||||||
if: always() && !startsWith(github.ref, 'refs/heads/main')
|
if: always() && !startsWith(github.ref, 'refs/heads/main')
|
||||||
needs:
|
needs:
|
||||||
- deploy-v1-cloudflare
|
|
||||||
- deploy-v2-vercel
|
- deploy-v2-vercel
|
||||||
- deploy-v2-cloudflare
|
- deploy-v2-cloudflare
|
||||||
steps:
|
steps:
|
||||||
@@ -133,15 +86,6 @@ jobs:
|
|||||||
body: |
|
body: |
|
||||||
Summary of the deployments:
|
Summary of the deployments:
|
||||||
|
|
||||||
### Version 1 (production)
|
|
||||||
|
|
||||||
| Version | URL | Status |
|
|
||||||
| --- | --- | --- |
|
|
||||||
| Latest commit | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}) | ${{ needs.deploy-v1-cloudflare.result == 'success' && '✅' || '❌' }} |
|
|
||||||
| PR | [${{ needs.deploy-v1-cloudflare.outputs.deployment-alias-url }}](${{ needs.deploy-v1-cloudflare.outputs.deployment-alias-url }}) | ${{ needs.deploy-v1-cloudflare.result == 'success' && '✅' || '❌' }} |
|
|
||||||
|
|
||||||
### Version 2 (experimental)
|
|
||||||
|
|
||||||
| Version | URL | Status |
|
| Version | URL | Status |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| Vercel | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}](${{ needs.deploy-v2-vercel.outputs.deployment-url }}) | ${{ needs.deploy-v2-vercel.result == 'success' && '✅' || '❌' }} |
|
| Vercel | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}](${{ needs.deploy-v2-vercel.outputs.deployment-url }}) | ${{ needs.deploy-v2-vercel.result == 'success' && '✅' || '❌' }} |
|
||||||
@@ -149,35 +93,16 @@ jobs:
|
|||||||
|
|
||||||
### Test content
|
### Test content
|
||||||
|
|
||||||
| Site | v1 | v2 |
|
| Site | `2v` | `2c` |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| GitBook | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/docs.gitbook.com](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/docs.gitbook.com) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com) |
|
| GitBook | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.com/docs](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.com/docs) | [${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/gitbook.com/docs](${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/gitbook.com/docs) |
|
||||||
| E2E | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) |
|
| E2E | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) |
|
||||||
edit-mode: replace
|
edit-mode: replace
|
||||||
visual-testing-v1:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
name: Visual Testing v1
|
|
||||||
needs: deploy-v1-cloudflare
|
|
||||||
timeout-minutes: 8
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: Setup Bun
|
|
||||||
uses: ./.github/composite/setup-bun
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
- name: Setup Playwright
|
|
||||||
uses: ./.github/actions/setup-playwright
|
|
||||||
- name: Run Playwright tests
|
|
||||||
run: bun e2e
|
|
||||||
env:
|
|
||||||
BASE_URL: ${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}
|
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
|
||||||
visual-testing-v2-vercel:
|
visual-testing-v2-vercel:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Visual Testing v2
|
name: Visual Testing v2
|
||||||
needs: deploy-v2-vercel
|
needs: deploy-v2-vercel
|
||||||
timeout-minutes: 10
|
timeout-minutes: 15
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -194,11 +119,11 @@ jobs:
|
|||||||
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
ARGOS_BUILD_NAME: 'v2-vercel'
|
ARGOS_BUILD_NAME: 'v2-vercel'
|
||||||
visual-testing-customers-v1:
|
visual-testing-v2-cloudflare:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Visual Testing Customers v1
|
name: Visual Testing v2 (Cloudflare)
|
||||||
needs: deploy-v1-cloudflare
|
needs: deploy-v2-cloudflare
|
||||||
timeout-minutes: 6
|
timeout-minutes: 15
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -209,16 +134,17 @@ jobs:
|
|||||||
- name: Setup Playwright
|
- name: Setup Playwright
|
||||||
uses: ./.github/actions/setup-playwright
|
uses: ./.github/actions/setup-playwright
|
||||||
- name: Run Playwright tests
|
- name: Run Playwright tests
|
||||||
run: bun e2e-customers
|
run: bun e2e
|
||||||
env:
|
env:
|
||||||
BASE_URL: ${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}
|
BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}
|
||||||
|
SITE_BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
ARGOS_BUILD_NAME: 'customers-v1'
|
ARGOS_BUILD_NAME: 'v2-cloudflare'
|
||||||
visual-testing-customers-v2:
|
visual-testing-customers-v2:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Visual Testing Customers v2
|
name: Visual Testing Customers v2
|
||||||
needs: deploy-v2-vercel
|
needs: deploy-v2-vercel
|
||||||
timeout-minutes: 6
|
timeout-minutes: 15
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -235,10 +161,31 @@ jobs:
|
|||||||
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
ARGOS_BUILD_NAME: 'customers-v2'
|
ARGOS_BUILD_NAME: 'customers-v2'
|
||||||
pagespeed-testing-v1:
|
visual-testing-customers-v2-cloudflare:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: Visual Testing Customers v2 (Cloudflare)
|
||||||
|
needs: deploy-v2-cloudflare
|
||||||
|
timeout-minutes: 15
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: ./.github/composite/setup-bun
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
- name: Setup Playwright
|
||||||
|
uses: ./.github/actions/setup-playwright
|
||||||
|
- name: Run Playwright tests
|
||||||
|
run: bun e2e-customers
|
||||||
|
env:
|
||||||
|
BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}
|
||||||
|
SITE_BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/
|
||||||
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
|
ARGOS_BUILD_NAME: 'customers-v2'
|
||||||
|
pagespeed-testing-v2:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: PageSpeed Testing v1
|
name: PageSpeed Testing v1
|
||||||
needs: deploy-v1-cloudflare
|
needs: deploy-v2-vercel
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -251,5 +198,5 @@ jobs:
|
|||||||
- name: Run pagespeed tests
|
- name: Run pagespeed tests
|
||||||
run: bun ./packages/gitbook/tests/pagespeed-testing.ts
|
run: bun ./packages/gitbook/tests/pagespeed-testing.ts
|
||||||
env:
|
env:
|
||||||
BASE_URL: ${{needs.deploy-v1-cloudflare.outputs.deployment-url}}
|
BASE_URL: ${{needs.deploy-v2-vercel.outputs.deployment-url}}
|
||||||
PAGESPEED_API_KEY: ${{ secrets.PAGESPEED_API_KEY }}
|
PAGESPEED_API_KEY: ${{ secrets.PAGESPEED_API_KEY }}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Deploy staging
|
- name: Deploy
|
||||||
id: deploy
|
id: deploy
|
||||||
uses: ./.github/composite/deploy-vercel
|
uses: ./.github/composite/deploy-vercel
|
||||||
with:
|
with:
|
||||||
@@ -48,6 +48,8 @@ jobs:
|
|||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
opItem: op://gitbook-open/2c-production
|
opItem: op://gitbook-open/2c-production
|
||||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||||
|
commitTag: main
|
||||||
|
commitMessage: ${{ github.sha }}
|
||||||
- name: Outputs
|
- name: Outputs
|
||||||
run: |
|
run: |
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Deploy staging
|
- name: Deploy
|
||||||
id: deploy
|
id: deploy
|
||||||
uses: ./.github/composite/deploy-vercel
|
uses: ./.github/composite/deploy-vercel
|
||||||
with:
|
with:
|
||||||
@@ -48,6 +48,8 @@ jobs:
|
|||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
opItem: op://gitbook-open/2c-staging
|
opItem: op://gitbook-open/2c-staging
|
||||||
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
|
||||||
|
commitTag: main
|
||||||
|
commitMessage: ${{ github.sha }}
|
||||||
- name: Outputs
|
- name: Outputs
|
||||||
run: |
|
run: |
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||||
@@ -5,6 +5,9 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
|
env:
|
||||||
|
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||||
|
|
||||||
concurrency: ${{ github.workflow }}-${{ github.ref }}
|
concurrency: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -36,22 +39,4 @@ jobs:
|
|||||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||||
release-preview:
|
|
||||||
# For now it releases the cache-do to both preview and production
|
|
||||||
# Once we changed to deploy the app only on release, we should change `release:preview` in `cache-do`
|
|
||||||
name: Release Preview
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout Repo
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: Setup Bun
|
|
||||||
uses: ./.github/composite/setup-bun
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
env:
|
|
||||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
|
||||||
- name: Release preview packages
|
|
||||||
run: bun run release:preview
|
|
||||||
env:
|
|
||||||
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
||||||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
.next
|
|
||||||
.vercel
|
|
||||||
.open-next
|
|
||||||
|
|
||||||
# Generated
|
|
||||||
packages/emoji-codepoints/index.ts
|
|
||||||
packages/gitbook/public/~gitbook/static/
|
|
||||||
packages/icons/src/data/*.json
|
|
||||||
|
|
||||||
# Build files
|
|
||||||
dist/
|
|
||||||
|
|
||||||
README.md
|
|
||||||
CHANGELOG.md
|
|
||||||
|
|
||||||
# v2
|
|
||||||
packages/gitbook-v2/public/~gitbook/static/
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"printWidth": 100,
|
|
||||||
"singleQuote": true,
|
|
||||||
"tabWidth": 4
|
|
||||||
}
|
|
||||||
Vendored
+4
@@ -9,9 +9,13 @@
|
|||||||
],
|
],
|
||||||
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"],
|
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"],
|
||||||
"prettier.enable": false,
|
"prettier.enable": false,
|
||||||
|
"editor.formatOnSave": true,
|
||||||
"editor.defaultFormatter": "biomejs.biome",
|
"editor.defaultFormatter": "biomejs.biome",
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.organizeImports.biome": "explicit",
|
"source.organizeImports.biome": "explicit",
|
||||||
"source.fixAll.biome": "explicit"
|
"source.fixAll.biome": "explicit"
|
||||||
|
},
|
||||||
|
"[typescript]": {
|
||||||
|
"editor.defaultFormatter": "biomejs.biome"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<h1 align="center">GitBook</h1>
|
<h1 align="center">GitBook</h1>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://docs.gitbook.com/">Docs</a> - <a href="https://github.com/GitbookIO/community">Community</a> - <a href="https://developer.gitbook.com/">Developer Docs</a> - <a href="https://changelog.gitbook.com/">Changelog</a> - <a href="https://github.com/GitbookIO/gitbook/issues/new?assignees=&labels=bug&template=bug_report.md">Bug reports</a>
|
<a href="https://gitbook.com/docs/">Docs</a> - <a href="https://github.com/GitbookIO/community">Community</a> - <a href="https://developer.gitbook.com/">Developer Docs</a> - <a href="https://changelog.gitbook.com/">Changelog</a> - <a href="https://github.com/GitbookIO/gitbook/issues/new?assignees=&labels=bug&template=bug_report.md">Bug reports</a> - <a href="https://github.com/orgs/GitbookIO/discussions/categories/feature-requests">Feature requests</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
@@ -35,10 +35,10 @@ To run a local version of this project, please follow these simple steps.
|
|||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
|
|
||||||
- Node.js (Version: >=20.6)
|
- Node.js (Version: >=20.6)
|
||||||
- Use nvm for easy Node management
|
- Use nvm for easy Node management
|
||||||
- Bun (Version: >=1.2.1)
|
- [Bun](https://bun.sh/) (Version: >=1.2.15)
|
||||||
- We use a text-based lockfile which isn't supported below 1.2.1
|
- We use a text-based lockfile which isn't supported below 1.2.15
|
||||||
|
|
||||||
### Set up
|
### Set up
|
||||||
|
|
||||||
@@ -62,20 +62,15 @@ bun install
|
|||||||
bun dev
|
bun dev
|
||||||
```
|
```
|
||||||
|
|
||||||
5. Open a published GitBook space in your web browser, prefixing it with `http://localhost:3000/`.
|
6. Open a published GitBook space in your web browser, prefixing it with `http://localhost:3000/url`.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
|
|
||||||
- http://localhost:3000/docs.gitbook.com
|
- http://localhost:3000/url/gitbook.com/docs
|
||||||
- http://localhost:3000/open-source.gitbook.io/midjourney
|
- http://localhost:3000/url/open-source.gitbook.io/midjourney
|
||||||
|
|
||||||
Any published GitBook site can be accessed through your local development instance, and any updates you make to the codebase will be reflected in your browser.
|
Any published GitBook site can be accessed through your local development instance, and any updates you make to the codebase will be reflected in your browser.
|
||||||
|
|
||||||
### Other development commands
|
|
||||||
|
|
||||||
- `bun format`: format the code
|
|
||||||
- `bun lint`: lint the code
|
|
||||||
|
|
||||||
### CI and testing
|
### CI and testing
|
||||||
|
|
||||||
All pull-requests will be tested against both visual and performances testing to prevent regressions.
|
All pull-requests will be tested against both visual and performances testing to prevent regressions.
|
||||||
@@ -150,11 +145,11 @@ See `LICENSE` for more information.
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
```md
|
```md
|
||||||
[](https://gitbook.com/)
|
[](https://www.gitbook.com/preview?utm_source=gitbook_readme_badge&utm_medium=organic&utm_campaign=preview_documentation&utm_content=link)
|
||||||
```
|
```
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<a href="https://gitbook.com">
|
<a href="https://www.gitbook.com/preview?utm_source=gitbook_readme_badge&utm_medium=organic&utm_campaign=preview_documentation&utm_content=link">
|
||||||
<img
|
<img
|
||||||
src="https://img.shields.io/static/v1?message=Documented%20on%20GitBook&logo=gitbook&logoColor=ffffff&label=%20&labelColor=5c5c5c&color=3F89A1"
|
src="https://img.shields.io/static/v1?message=Documented%20on%20GitBook&logo=gitbook&logoColor=ffffff&label=%20&labelColor=5c5c5c&color=3F89A1"
|
||||||
/>
|
/>
|
||||||
|
|||||||
+5
-2
@@ -18,7 +18,11 @@
|
|||||||
"**/.vercel/**/*",
|
"**/.vercel/**/*",
|
||||||
"**/.cache/**/*",
|
"**/.cache/**/*",
|
||||||
"**/.wrangler/**/*",
|
"**/.wrangler/**/*",
|
||||||
"packages/openapi-parser/src/fixtures/**/*"
|
"packages/openapi-parser/src/fixtures/**/*",
|
||||||
|
"packages/emoji-codepoints/index.ts",
|
||||||
|
"packages/icons/src/data/*.json",
|
||||||
|
"packages/gitbook/worker-configuration.d.ts",
|
||||||
|
"**/*.css"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"formatter": {
|
"formatter": {
|
||||||
@@ -124,7 +128,6 @@
|
|||||||
{
|
{
|
||||||
"include": [
|
"include": [
|
||||||
"packages/gitbook/**/*",
|
"packages/gitbook/**/*",
|
||||||
"packages/gitbook-v2/**/*",
|
|
||||||
"packages/react-openapi/**/*",
|
"packages/react-openapi/**/*",
|
||||||
"packages/react-math/**/*",
|
"packages/react-math/**/*",
|
||||||
"packages/react-contentkit/**/*",
|
"packages/react-contentkit/**/*",
|
||||||
|
|||||||
+11
-8
@@ -4,21 +4,20 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "^1.9.4",
|
"@biomejs/biome": "^1.9.4",
|
||||||
"@changesets/cli": "^2.27.12",
|
"@changesets/cli": "^2.27.12",
|
||||||
"turbo": "^2.4.4",
|
"turbo": "^2.5.0",
|
||||||
"vercel": "^39.3.0"
|
"vercel": "^39.3.0"
|
||||||
},
|
},
|
||||||
"packageManager": "bun@1.2.4",
|
"packageManager": "bun@1.2.15",
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"@codemirror/state": "6.4.1",
|
"@codemirror/state": "6.4.1",
|
||||||
"react": "18.3.1",
|
"react": "^19.0.0",
|
||||||
"react-dom": "18.3.1"
|
"react-dom": "^19.0.0",
|
||||||
|
"esbuild": "0.24.2"
|
||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "turbo run dev",
|
"dev": "turbo run dev",
|
||||||
"dev:v2": "turbo run dev:v2",
|
|
||||||
"build": "turbo run build",
|
"build": "turbo run build",
|
||||||
"build:v2": "turbo run build:v2",
|
|
||||||
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
|
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
|
||||||
"typecheck": "turbo run typecheck",
|
"typecheck": "turbo run typecheck",
|
||||||
"format": "biome check --write ./",
|
"format": "biome check --write ./",
|
||||||
@@ -29,11 +28,15 @@
|
|||||||
"changeset": "changeset",
|
"changeset": "changeset",
|
||||||
"changeset-version": "changeset version && bun run format",
|
"changeset-version": "changeset version && bun run format",
|
||||||
"release": "turbo run release && changeset publish",
|
"release": "turbo run release && changeset publish",
|
||||||
"release:preview": "turbo run release:preview",
|
|
||||||
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
|
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
|
||||||
"clean": "turbo run clean"
|
"clean": "turbo run clean"
|
||||||
},
|
},
|
||||||
"workspaces": ["packages/*"],
|
"workspaces": {
|
||||||
|
"packages": ["packages/*"],
|
||||||
|
"catalog": {
|
||||||
|
"@gitbook/api": "^0.136.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"patchedDependencies": {
|
"patchedDependencies": {
|
||||||
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
|
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
|
||||||
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch"
|
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch"
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# `@gitbook/browser-types`
|
||||||
|
|
||||||
|
Typescript types for the global variables available in a GitBook website. These types can be used by integrations embedding scripts.
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "@gitbook/browser-types",
|
||||||
|
"description": "Typescript types for the global variables available in a GitBook website. These types can be used by integrations embedding scripts.",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"version": "0.0.0",
|
||||||
|
"dependencies": {
|
||||||
|
"@gitbook/api": "catalog:",
|
||||||
|
"@gitbook/icons": "workspace:"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.5.3"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"typecheck": "tsc --noEmit"
|
||||||
|
},
|
||||||
|
"files": ["dist", "README.md", "CHANGELOG.md"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import type { AIToolCallResult, AIToolDefinition } from '@gitbook/api';
|
||||||
|
import type { IconName } from '@gitbook/icons';
|
||||||
|
|
||||||
|
export type GitBookIntegrationEvent = 'load' | 'unload';
|
||||||
|
|
||||||
|
export type GitBookIntegrationEventCallback = (...args: unknown[]) => void;
|
||||||
|
|
||||||
|
export type GitBookIntegrationTool = AIToolDefinition & {
|
||||||
|
/**
|
||||||
|
* Confirmation action to be displayed to the user before executing the tool.
|
||||||
|
*/
|
||||||
|
confirmation?: {
|
||||||
|
icon?: IconName;
|
||||||
|
label: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback when the tool is executed.
|
||||||
|
* The input is provided by the AI assistant following the input schema of the tool.
|
||||||
|
*/
|
||||||
|
execute: (input: object) => Promise<Pick<AIToolCallResult, 'output' | 'summary'>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GitBookAssistant = {
|
||||||
|
/**
|
||||||
|
* Name of the assistant displayed in the UI.
|
||||||
|
*/
|
||||||
|
label: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Icon of the assistant displayed in the UI.
|
||||||
|
* Any FontAwesome icon name is supported.
|
||||||
|
* @example 'sparkle'
|
||||||
|
*/
|
||||||
|
icon: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback when the assistant is opened.
|
||||||
|
*/
|
||||||
|
open: (query?: string) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to display the triggers for this assistant in the UI.
|
||||||
|
* @default true
|
||||||
|
*/
|
||||||
|
ui?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GitBookGlobal = {
|
||||||
|
/**
|
||||||
|
* Register an event listener.
|
||||||
|
*/
|
||||||
|
addEventListener: (
|
||||||
|
type: GitBookIntegrationEvent,
|
||||||
|
func: GitBookIntegrationEventCallback
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an event listener.
|
||||||
|
*/
|
||||||
|
removeEventListener: (
|
||||||
|
type: GitBookIntegrationEvent,
|
||||||
|
func: GitBookIntegrationEventCallback
|
||||||
|
) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a custom tool to be exposed to the AI assistant.
|
||||||
|
*/
|
||||||
|
registerTool: (tool: GitBookIntegrationTool) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a custom assistant to be available on the site.
|
||||||
|
* Returns a registration object that lets the integration signal readiness
|
||||||
|
* and dispose the assistant when no longer needed.
|
||||||
|
*/
|
||||||
|
registerAssistant: (assistant: GitBookAssistant) => GitBookAssistantRegistration;
|
||||||
|
};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
/**
|
||||||
|
* Global `window.GitBook` object accessible by integrations.
|
||||||
|
*/
|
||||||
|
GitBook?: GitBookGlobal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type GitBookAssistantRegistration = ((this: void) => void) & {
|
||||||
|
/**
|
||||||
|
* Call this when the integration has finished initializing and is ready to
|
||||||
|
* receive `open` calls.
|
||||||
|
*/
|
||||||
|
ready: () => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregister the assistant.
|
||||||
|
*/
|
||||||
|
dispose: () => void;
|
||||||
|
};
|
||||||
@@ -13,11 +13,12 @@
|
|||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"types": [
|
"types": [
|
||||||
"bun-types" // add Bun global
|
"bun-types" // add Bun global
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts"],
|
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
.wrangler
|
|
||||||
worker-configuration.d.ts
|
|
||||||
dist/
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# @gitbook/cache-do
|
|
||||||
|
|
||||||
## 0.1.1
|
|
||||||
|
|
||||||
### Patch Changes
|
|
||||||
|
|
||||||
- b7a5106: Disable cloudflare observability in production
|
|
||||||
|
|
||||||
## 0.1.0
|
|
||||||
|
|
||||||
### Minor Changes
|
|
||||||
|
|
||||||
- 9b8d519: Experiment with optimizing billable duration in Cloudflare by using multiple RPC sessions instead of one
|
|
||||||
- 636b868: First version of a new cache backend powered by Cloudflare Durable Objects
|
|
||||||
|
|
||||||
### Patch Changes
|
|
||||||
|
|
||||||
- 56f5fa1: Enable Workers observability with a sampling of 0.1
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# `@gitbook/cache-do`
|
|
||||||
|
|
||||||
Cache backend, powered by Cloudflare Durable Objects. The cache is optimized for GitBook use-cases.
|
|
||||||
|
|
||||||
### Performances
|
|
||||||
|
|
||||||
The cache backend is optimized for performances by being distributed and accessible close to the worker locations that are reading it.
|
|
||||||
|
|
||||||
### Geo-distribution
|
|
||||||
|
|
||||||
To achieve a good balance between **performances** and **consistency**, cache objects are distributed over 7 locations, representing continents.
|
|
||||||
|
|
||||||
It makes it possible to purge all 7 locations in one go and achieve fast consistency.
|
|
||||||
|
|
||||||
### Concepts
|
|
||||||
|
|
||||||
**Cache tag**: unique tag in the cache environment. A cache tag groups multiple keys that should be purged together in one operation.
|
|
||||||
Cache tags should not contain a large set of unique keys. Exceeding thousands could lead to performances or reliability issues.
|
|
||||||
|
|
||||||
**Cache key**: unique key in the cache environment. Each key should be assigned to a `tag`.
|
|
||||||
|
|
||||||
**Location**: cache is distributed over 7 unique locations, one for each continent.
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@gitbook/cache-do",
|
|
||||||
"type": "module",
|
|
||||||
"private": true,
|
|
||||||
"exports": {
|
|
||||||
".": {
|
|
||||||
"types": "./dist/index.d.ts",
|
|
||||||
"development": "./src/index.ts",
|
|
||||||
"default": "./dist/index.js"
|
|
||||||
},
|
|
||||||
"./api": {
|
|
||||||
"types": "./dist/api.d.ts",
|
|
||||||
"development": "./src/api.ts",
|
|
||||||
"default": "./dist/api.js"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"version": "0.1.1",
|
|
||||||
"dependencies": {
|
|
||||||
"@msgpack/msgpack": "^3.0.0-beta2",
|
|
||||||
"lru_map": "^0.4.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"typescript": "^5.5.3",
|
|
||||||
"wrangler": "^3.112.0"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"generate": "wrangler types --experimental-include-runtime",
|
|
||||||
"build": "tsc",
|
|
||||||
"typecheck": "tsc --noEmit",
|
|
||||||
"dev": "tsc -w",
|
|
||||||
"release": "wrangler deploy",
|
|
||||||
"release:preview": "wrangler deploy && wrangler deploy --env preview"
|
|
||||||
},
|
|
||||||
"files": ["dist", "src", "bin", "data", "README.md", "CHANGELOG.md"]
|
|
||||||
}
|
|
||||||
@@ -1,292 +0,0 @@
|
|||||||
import { DurableObject } from 'cloudflare:workers';
|
|
||||||
import { decode, encode } from '@msgpack/msgpack';
|
|
||||||
import { LRUMap } from 'lru_map';
|
|
||||||
|
|
||||||
export interface CacheObjectDescriptor {
|
|
||||||
get: <Value = unknown>(key: string) => Promise<Value | undefined>;
|
|
||||||
set: <Value = unknown>(key: string, value: Value, expiresAt: number) => Promise<void>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Value stored in a chunked binary msgpack format.
|
|
||||||
* Stored under the key `prop.${key}.${index}`.
|
|
||||||
*/
|
|
||||||
interface CacheObjectProp<Value = unknown> {
|
|
||||||
value: Value;
|
|
||||||
expiresAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Expiration clock stored under the key `exp.${expiresAt}.${key}`.
|
|
||||||
*/
|
|
||||||
interface CacheObjectExp {
|
|
||||||
/** Key of the property */
|
|
||||||
k: string;
|
|
||||||
/** Number of chunks */
|
|
||||||
c: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Durable Object class being deployed as a distributed cache.
|
|
||||||
*/
|
|
||||||
export class CacheObject extends DurableObject {
|
|
||||||
private lru = new LRUMap<string, { match: CacheObjectProp | undefined }>(500);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Open a descriptor to access the cache object.
|
|
||||||
* The goal is to minimize the amount of RPC sessions between the client and the cache object.
|
|
||||||
* One session is opened per request on the client side and used to perform multiple operations.
|
|
||||||
* https://developers.cloudflare.com/workers/runtime-apis/rpc/#return-functions-from-rpc-methods
|
|
||||||
*/
|
|
||||||
public open(): CacheObjectDescriptor {
|
|
||||||
return {
|
|
||||||
get: async <Value = unknown>(key: string) => {
|
|
||||||
return this.get<Value>(key);
|
|
||||||
},
|
|
||||||
set: async <Value = unknown>(key: string, value: Value, expiresAt: number) => {
|
|
||||||
await this.set(key, value, expiresAt);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the value of a property.
|
|
||||||
*/
|
|
||||||
public async get<Value = unknown>(key: string) {
|
|
||||||
return this.logOperation({ operation: 'get', key }, async (setLog) => {
|
|
||||||
// Try the memory state first.
|
|
||||||
const memoryEntry = this.lru.get(key);
|
|
||||||
if (memoryEntry) {
|
|
||||||
setLog({ memory: true });
|
|
||||||
setLog({ memoryMatch: !!memoryEntry.match });
|
|
||||||
if (!memoryEntry.match) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isExpired = memoryEntry.match.expiresAt < Date.now();
|
|
||||||
setLog({ memoryExpired: isExpired });
|
|
||||||
|
|
||||||
if (!isExpired) {
|
|
||||||
return memoryEntry.match.value as Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return await this.getFromStorage<Value>(key);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the value of a property from the DO storage.
|
|
||||||
*/
|
|
||||||
public async getFromStorage<Value = unknown>(key: string) {
|
|
||||||
return this.logOperation({ operation: 'getFromStorage', key }, async (setLog) => {
|
|
||||||
const entries = await this.ctx.storage.list<Uint8Array>({
|
|
||||||
prefix: getStoragePropKey(key),
|
|
||||||
noCache: true,
|
|
||||||
});
|
|
||||||
if (entries.size) {
|
|
||||||
const entry = decodeChunks<CacheObjectProp<Value>>(entries);
|
|
||||||
setLog({ chunks: entries.size, chunksSize: entry?.size ?? 0 });
|
|
||||||
if (entry && entry.value.expiresAt > Date.now()) {
|
|
||||||
// Found
|
|
||||||
this.lru.set(key, { match: entry.value });
|
|
||||||
return entry.value.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not found
|
|
||||||
this.lru.set(key, { match: undefined });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a value in the cache object.
|
|
||||||
*/
|
|
||||||
public async set<Value = unknown>(key: string, value: Value, expiresAt: number) {
|
|
||||||
return this.logOperation({ operation: 'set', key }, async (setLog) => {
|
|
||||||
const prop: CacheObjectProp<Value> = {
|
|
||||||
value,
|
|
||||||
expiresAt,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.lru.set(key, { match: prop });
|
|
||||||
await this.ctx.storage.transaction(async (tx) => {
|
|
||||||
const entries = encodeChunks(key, prop);
|
|
||||||
const chunks = Object.keys(entries).length;
|
|
||||||
setLog({ chunks });
|
|
||||||
|
|
||||||
const clockValue: CacheObjectExp = {
|
|
||||||
k: key,
|
|
||||||
c: chunks,
|
|
||||||
};
|
|
||||||
|
|
||||||
await tx.put(getGCClockKey(key, expiresAt), clockValue);
|
|
||||||
await tx.put(entries);
|
|
||||||
|
|
||||||
const currentAlarm = await tx.getAlarm();
|
|
||||||
if (!currentAlarm) {
|
|
||||||
// Set an alarm to garbage collect all entries that have expired in 12h.
|
|
||||||
await tx.setAlarm(Date.now() + 12 * 60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Purge all keys in the cache object.
|
|
||||||
*/
|
|
||||||
public async purge() {
|
|
||||||
return this.logOperation({ operation: 'purge' }, async (setLog) => {
|
|
||||||
const result = new Set<string>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// List all the keys in the cache object.
|
|
||||||
const entries = await this.ctx.storage.list<CacheObjectExp>({
|
|
||||||
prefix: 'exp.',
|
|
||||||
noCache: true,
|
|
||||||
});
|
|
||||||
setLog({ entries: entries.size });
|
|
||||||
entries.forEach((exp) => {
|
|
||||||
result.add(exp.k);
|
|
||||||
});
|
|
||||||
} catch (_error) {}
|
|
||||||
|
|
||||||
await this.reset();
|
|
||||||
return Array.from(result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Alarm to garbage collect all entries that have expired.
|
|
||||||
*/
|
|
||||||
async alarm() {
|
|
||||||
return this.logOperation({ operation: 'alarm' }, async (setLog) => {
|
|
||||||
try {
|
|
||||||
const entries = await this.ctx.storage.list<CacheObjectExp>({
|
|
||||||
prefix: 'exp.',
|
|
||||||
noCache: true,
|
|
||||||
});
|
|
||||||
setLog({ entries: entries.size });
|
|
||||||
const toDeleteSet = new Set<string>();
|
|
||||||
|
|
||||||
for (const [key, exp] of entries) {
|
|
||||||
const timestamp = Number.parseInt(key.split('.')[1]);
|
|
||||||
if (timestamp < Date.now()) {
|
|
||||||
toDeleteSet.add(key);
|
|
||||||
for (let i = 0; i < exp.c; i++) {
|
|
||||||
toDeleteSet.add(getStoragePropChunkKey(exp.k, i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the keys by batch of 128.
|
|
||||||
const toDelete = Array.from(toDeleteSet);
|
|
||||||
setLog({ toDelete: toDelete.length });
|
|
||||||
for (let i = 0; i < toDelete.length; i += 128) {
|
|
||||||
await this.ctx.storage.delete(toDelete.slice(i, i + 128));
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there are still keys to delete, set an alarm to continue the deletion in 12h.
|
|
||||||
if (toDelete.length) {
|
|
||||||
await this.ctx.storage.setAlarm(Date.now() + 12 * 60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
} catch (_error) {
|
|
||||||
await this.reset();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reset the cache object.
|
|
||||||
*/
|
|
||||||
async reset() {
|
|
||||||
return this.logOperation({ operation: 'reset' }, async () => {
|
|
||||||
this.lru.clear();
|
|
||||||
await this.ctx.storage.deleteAll();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Time and log an operation.
|
|
||||||
*/
|
|
||||||
async logOperation<T>(
|
|
||||||
log: Record<string, unknown>,
|
|
||||||
fn: (update: (log: Record<string, unknown>) => void) => Promise<T>
|
|
||||||
): Promise<T> {
|
|
||||||
const objectId = this.ctx.id.name ?? this.ctx.id.toString();
|
|
||||||
const update: Record<string, unknown> = {};
|
|
||||||
const start = performance.now();
|
|
||||||
try {
|
|
||||||
return await fn((arg) => {
|
|
||||||
Object.assign(update, arg);
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
const duration = performance.now() - start;
|
|
||||||
console.log({ ...log, ...update, objectId, duration });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStoragePropKey(key: string): string {
|
|
||||||
return `prop.${key}.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStoragePropChunkKey(key: string, index: number): string {
|
|
||||||
return `${getStoragePropKey(key)}${index}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getGCClockRootKey(timestamp: number): string {
|
|
||||||
return `exp.${timestamp}.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getGCClockKey(key: string, expiresAt: number): string {
|
|
||||||
return `${getGCClockRootKey(expiresAt)}${key}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function encodeChunks<T>(key: string, value: T): Record<string, Uint8Array> {
|
|
||||||
const buf = encode(value);
|
|
||||||
const entries: Record<string, Uint8Array> = {};
|
|
||||||
const chunks = chunkUint8Array(buf, 128 * 1024);
|
|
||||||
|
|
||||||
for (let index = 0; index < chunks.length; index++) {
|
|
||||||
entries[getStoragePropChunkKey(key, index)] = chunks[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
return entries;
|
|
||||||
}
|
|
||||||
|
|
||||||
function decodeChunks<T>(entries: Map<string, Uint8Array>): { value: T; size: number } | undefined {
|
|
||||||
const chunks = Array.from(entries.entries())
|
|
||||||
.map(([key, value]) => {
|
|
||||||
const index = Number.parseInt(key.split('.').pop()!);
|
|
||||||
return [index, value] as const;
|
|
||||||
})
|
|
||||||
.sort(([a], [b]) => a - b)
|
|
||||||
.map(([, value]) => value);
|
|
||||||
|
|
||||||
if (chunks.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const buf = mergeUint8Array(chunks);
|
|
||||||
return { value: decode(buf) as T, size: buf.length };
|
|
||||||
}
|
|
||||||
|
|
||||||
function chunkUint8Array(input: Uint8Array, chunkSize: number): Uint8Array[] {
|
|
||||||
const chunks: Uint8Array[] = [];
|
|
||||||
for (let i = 0; i < input.length; i += chunkSize) {
|
|
||||||
chunks.push(input.slice(i, i + chunkSize));
|
|
||||||
}
|
|
||||||
return chunks;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeUint8Array(chunks: Uint8Array[]): Uint8Array {
|
|
||||||
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
||||||
const result = new Uint8Array(totalLength);
|
|
||||||
let offset = 0;
|
|
||||||
for (const chunk of chunks) {
|
|
||||||
result.set(chunk, offset);
|
|
||||||
offset += chunk.length;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
import type { CacheObject } from './CacheObject';
|
|
||||||
|
|
||||||
export type CacheLocationId = ContinentCode;
|
|
||||||
const allLocations: CacheLocationId[] = ['AF', 'AS', 'NA', 'SA', 'AN', 'EU', 'OC'];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Location hint for the CacheObject durable object.
|
|
||||||
*/
|
|
||||||
const doLocationHints: {
|
|
||||||
[key in CacheLocationId]: DurableObjectLocationHint;
|
|
||||||
} = {
|
|
||||||
AF: 'afr',
|
|
||||||
AS: 'apac',
|
|
||||||
NA: 'wnam',
|
|
||||||
SA: 'sam',
|
|
||||||
AN: 'oc',
|
|
||||||
EU: 'weur',
|
|
||||||
OC: 'oc',
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Client to access a cache tag.
|
|
||||||
*/
|
|
||||||
export class CacheObjectStub {
|
|
||||||
private stub: DurableObjectStub<CacheObject>;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
/** Binding to the CacheObject durable object */
|
|
||||||
private doNamespace: DurableObjectNamespace<CacheObject>,
|
|
||||||
/** ID of the location to target */
|
|
||||||
private locationId: CacheLocationId,
|
|
||||||
/** Name of the tag */
|
|
||||||
private tag: string
|
|
||||||
) {
|
|
||||||
const groupId = getCacheObjectIdName(this.locationId, this.tag);
|
|
||||||
this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), {
|
|
||||||
// Initialize the object with a locaiton hint,
|
|
||||||
// as we might want to purge all locations before the object is created.
|
|
||||||
// https://developers.cloudflare.com/durable-objects/reference/data-location/
|
|
||||||
locationHint: doLocationHints[this.locationId],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Open a descriptor to the cache object.
|
|
||||||
* It can be used to perform multiple operations in a single RPC session.
|
|
||||||
* Ex:
|
|
||||||
* ```ts
|
|
||||||
* using desc = cache.open();
|
|
||||||
* await desc.set('key', 'value', Date.now() + 1000);
|
|
||||||
* await desc.get('key');
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async open() {
|
|
||||||
return await this.stub.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a value from the cache.
|
|
||||||
*/
|
|
||||||
async get<Value = unknown>(key: string) {
|
|
||||||
return (await this.stub.get(key)) as Value | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a value in the cache.
|
|
||||||
*/
|
|
||||||
async set<Value = unknown>(key: string, value: Value, expiresAt: number) {
|
|
||||||
return await this.stub.set(key, value, expiresAt);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Purge all keys in the cache tag.
|
|
||||||
*/
|
|
||||||
async purge() {
|
|
||||||
const keys = new Set<string>();
|
|
||||||
await Promise.all(
|
|
||||||
allLocations.map(async (locationId) => {
|
|
||||||
const groupId = getCacheObjectIdName(locationId, this.tag);
|
|
||||||
const cacheGroup = this.doNamespace.get(this.doNamespace.idFromName(groupId), {
|
|
||||||
// Initialize the object with a locaiton hint,
|
|
||||||
// as we might want to purge all locations before the object is created.
|
|
||||||
// https://developers.cloudflare.com/durable-objects/reference/data-location/
|
|
||||||
locationHint: doLocationHints[this.locationId],
|
|
||||||
});
|
|
||||||
const locationkeys = await cacheGroup.purge();
|
|
||||||
locationkeys.forEach((key) => keys.add(key));
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return keys;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCacheObjectIdName(locationId: CacheLocationId, tag: string): string {
|
|
||||||
return `${locationId}:${tag}`;
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * from './CacheObjectStub';
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { WorkerEntrypoint } from 'cloudflare:workers';
|
|
||||||
|
|
||||||
export * from './CacheObject';
|
|
||||||
|
|
||||||
export default class Worker extends WorkerEntrypoint {
|
|
||||||
fetch() {
|
|
||||||
return new Response('Hello, world!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
main = "./src/index.ts"
|
|
||||||
name = "gitbook-open-cache"
|
|
||||||
compatibility_date = "2024-09-02"
|
|
||||||
|
|
||||||
durable_objects.bindings = [
|
|
||||||
{name = "CACHE", class_name = "CacheObject"}
|
|
||||||
]
|
|
||||||
|
|
||||||
migrations = [
|
|
||||||
{tag = "v1", new_classes = ["CacheObject"]}
|
|
||||||
]
|
|
||||||
|
|
||||||
[observability]
|
|
||||||
enabled = false
|
|
||||||
|
|
||||||
[env.preview]
|
|
||||||
name = "gitbook-open-cache-preview"
|
|
||||||
durable_objects.bindings = [
|
|
||||||
{name = "CACHE", class_name = "CacheObject"}
|
|
||||||
]
|
|
||||||
migrations = [
|
|
||||||
{tag = "v1", new_classes = ["CacheObject"]}
|
|
||||||
]
|
|
||||||
|
|
||||||
[env.preview.observability]
|
|
||||||
enabled = true
|
|
||||||
head_sampling_rate = 0.1
|
|
||||||
@@ -1,7 +1,25 @@
|
|||||||
# @gitbook/cache-tags
|
# @gitbook/cache-tags
|
||||||
|
|
||||||
|
## 0.3.1
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 77397ca: Fix version of @gitbook/api referenced in package.json
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- 116575c: Improve typing of getComputedContentSourceCacheTags to match latest API specification
|
||||||
|
|
||||||
|
## 0.2.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- f32bf1f: Export function `getCacheTagForURL` to easily get the cache tag for a URL.
|
||||||
|
|
||||||
## 0.1.0
|
## 0.1.0
|
||||||
|
|
||||||
### Minor Changes
|
### Minor Changes
|
||||||
|
|
||||||
- 05ffd0e: Initial version of the package
|
- 05ffd0e: Initial version of the package
|
||||||
|
|||||||
@@ -8,9 +8,9 @@
|
|||||||
"default": "./dist/index.js"
|
"default": "./dist/index.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "0.1.0",
|
"version": "0.3.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@gitbook/api": "0.96.1",
|
"@gitbook/api": "catalog:",
|
||||||
"assert-never": "^1.2.1"
|
"assert-never": "^1.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ export function getCacheTag(
|
|||||||
| {
|
| {
|
||||||
tag: 'computed-document';
|
tag: 'computed-document';
|
||||||
space: string;
|
space: string;
|
||||||
integration: string;
|
sourceType: string;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* All data related to the URL of a content
|
* All data related to the URL of a content
|
||||||
@@ -84,6 +84,14 @@ export function getCacheTag(
|
|||||||
organization: string;
|
organization: string;
|
||||||
openAPISpec: string;
|
openAPISpec: string;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* All data related to a translation
|
||||||
|
*/
|
||||||
|
| {
|
||||||
|
tag: 'translation';
|
||||||
|
organization: string;
|
||||||
|
translation: string;
|
||||||
|
}
|
||||||
): string {
|
): string {
|
||||||
switch (spec.tag) {
|
switch (spec.tag) {
|
||||||
case 'user':
|
case 'user':
|
||||||
@@ -99,18 +107,31 @@ export function getCacheTag(
|
|||||||
case 'document':
|
case 'document':
|
||||||
return `space:${spec.space}:document:${spec.document}`;
|
return `space:${spec.space}:document:${spec.document}`;
|
||||||
case 'computed-document':
|
case 'computed-document':
|
||||||
return `space:${spec.space}:computed-document:${spec.integration}`;
|
return `space:${spec.space}:computed-document:${spec.sourceType}`;
|
||||||
case 'site':
|
case 'site':
|
||||||
return `site:${spec.site}`;
|
return `site:${spec.site}`;
|
||||||
case 'integration':
|
case 'integration':
|
||||||
return `integration:${spec.integration}`;
|
return `integration:${spec.integration}`;
|
||||||
case 'openapi':
|
case 'openapi':
|
||||||
return `organization:${spec.organization}:openapi:${spec.openAPISpec}`;
|
return `organization:${spec.organization}:openapi:${spec.openAPISpec}`;
|
||||||
|
case 'translation':
|
||||||
|
return `organization:${spec.organization}:translation:${spec.translation}`;
|
||||||
default:
|
default:
|
||||||
assertNever(spec);
|
assertNever(spec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache tag for a given URL.
|
||||||
|
*/
|
||||||
|
export function getCacheTagForURL(url: string | URL) {
|
||||||
|
const parsedURL = url instanceof URL ? url : new URL(url);
|
||||||
|
return getCacheTag({
|
||||||
|
tag: 'url',
|
||||||
|
hostname: parsedURL.hostname,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the tags for a computed content source.
|
* Get the tags for a computed content source.
|
||||||
*/
|
*/
|
||||||
@@ -123,6 +144,10 @@ export function getComputedContentSourceCacheTags(
|
|||||||
) {
|
) {
|
||||||
const tags: string[] = [];
|
const tags: string[] = [];
|
||||||
|
|
||||||
|
if (!('dependencies' in source)) {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
// We add the dependencies as tags, to ensure that the computed content is invalidated
|
// We add the dependencies as tags, to ensure that the computed content is invalidated
|
||||||
// when the dependencies are updated.
|
// when the dependencies are updated.
|
||||||
const dependencies = Object.values(source.dependencies ?? {});
|
const dependencies = Object.values(source.dependencies ?? {});
|
||||||
@@ -146,6 +171,15 @@ export function getComputedContentSourceCacheTags(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
case 'translation':
|
||||||
|
tags.push(
|
||||||
|
getCacheTag({
|
||||||
|
tag: 'translation',
|
||||||
|
organization: inContext.organizationId,
|
||||||
|
translation: dependency.ref.translation,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
// Do not throw for unknown dependency types
|
// Do not throw for unknown dependency types
|
||||||
// as it might mean we are lacking behind the API version
|
// as it might mean we are lacking behind the API version
|
||||||
@@ -158,18 +192,22 @@ export function getComputedContentSourceCacheTags(
|
|||||||
getCacheTag({
|
getCacheTag({
|
||||||
tag: 'computed-document',
|
tag: 'computed-document',
|
||||||
space: inContext.spaceId,
|
space: inContext.spaceId,
|
||||||
integration: source.integration,
|
sourceType: source.type,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We invalidate the computed content when a new version of the integration is deployed.
|
// We invalidate the computed content when a new version of the integration is deployed.
|
||||||
tags.push(
|
|
||||||
getCacheTag({
|
if (source.type.startsWith('integration:')) {
|
||||||
tag: 'integration',
|
const integration = source.type.split(':')[1];
|
||||||
integration: source.integration,
|
tags.push(
|
||||||
})
|
getCacheTag({
|
||||||
);
|
tag: 'integration',
|
||||||
|
integration,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,33 @@
|
|||||||
# @gitbook/colors
|
# @gitbook/colors
|
||||||
|
|
||||||
|
## 0.3.3
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- c3f6b8c: Update chroma ratio per step
|
||||||
|
- 5e975ab: Fix code highlighting for HTTP
|
||||||
|
- f7a3470: Change lightness check for color step 9 to allow input colors with a higher-than-needed contrast
|
||||||
|
|
||||||
|
## 0.3.2
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- cdffd7c: Desaturate text colors by decreasing chroma for the last steps of the color scale
|
||||||
|
|
||||||
|
## 0.3.1
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- fb90eb0: Reduce chroma of first color scale step
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- 4f0a772: Override tint lightness if supplied color is out of bounds
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
### Minor Changes
|
### Minor Changes
|
||||||
|
|
||||||
- 445baaa: Initial release
|
- 445baaa: Initial release
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
"default": "./dist/index.js"
|
"default": "./dist/index.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "0.2.0",
|
"version": "0.3.3",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5.5.3"
|
"typescript": "^5.5.3"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ export function colorScale(
|
|||||||
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
|
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
|
||||||
const foregroundColor = rgbToOklch(hexToRgbArray(foreground));
|
const foregroundColor = rgbToOklch(hexToRgbArray(foreground));
|
||||||
const backgroundColor = rgbToOklch(hexToRgbArray(background));
|
const backgroundColor = rgbToOklch(hexToRgbArray(background));
|
||||||
|
let mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light;
|
||||||
|
|
||||||
if (mixColor && mix?.ratio && mix.ratio > 0) {
|
if (mixColor && mix?.ratio && mix.ratio > 0) {
|
||||||
// If defined, we mix in a (tiny) bit of the mix color with the base color.
|
// If defined, we mix in a (tiny) bit of the mix color with the base color.
|
||||||
@@ -192,7 +193,20 @@ export function colorScale(
|
|||||||
baseColor.H = mix.color === DEFAULT_TINT_COLOR ? baseColor.H : mixColor.H;
|
baseColor.H = mix.color === DEFAULT_TINT_COLOR ? baseColor.H : mixColor.H;
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light;
|
if (
|
||||||
|
(darkMode && baseColor.L < backgroundColor.L) ||
|
||||||
|
(!darkMode && baseColor.L > backgroundColor.L)
|
||||||
|
) {
|
||||||
|
// If the supplied color is outside of our lightness bounds, use the supplied color's lightness.
|
||||||
|
// This is mostly used to allow darker-than-dark backgrounds for brands that specifically want that look.
|
||||||
|
const difference = (backgroundColor.L - baseColor.L) / backgroundColor.L;
|
||||||
|
backgroundColor.L = baseColor.L;
|
||||||
|
// At the edges of the scale, the subtle lightness changes stop being perceptible. We need to amp up our mapping to still stand out.
|
||||||
|
const amplifier = 1;
|
||||||
|
mapping = mapping.map((step, index) =>
|
||||||
|
index < 9 ? step + step * amplifier * difference : step
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
|
|
||||||
@@ -200,13 +214,31 @@ export function colorScale(
|
|||||||
const targetL =
|
const targetL =
|
||||||
foregroundColor.L * mapping[index] + backgroundColor.L * (1 - mapping[index]);
|
foregroundColor.L * mapping[index] + backgroundColor.L * (1 - mapping[index]);
|
||||||
|
|
||||||
if (index === 8 && !mix && Math.abs(baseColor.L - targetL) < 0.2) {
|
if (
|
||||||
|
index === 8 &&
|
||||||
|
!mix &&
|
||||||
|
(darkMode ? targetL - baseColor.L < 0.2 : baseColor.L - targetL < 0.2)
|
||||||
|
) {
|
||||||
// Original colour is close enough to target, so let's use the original colour as step 9.
|
// Original colour is close enough to target, so let's use the original colour as step 9.
|
||||||
result.push(hex);
|
result.push(hex);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chromaRatio = index < 8 ? (index + 1) * 0.05 : 1;
|
const chromaRatio = (() => {
|
||||||
|
switch (index) {
|
||||||
|
// Step 9 and 10 have max chroma, meaning they are fully saturated.
|
||||||
|
case 8:
|
||||||
|
case 9:
|
||||||
|
return 1;
|
||||||
|
// Step 11 and 12 have a reduced chroma
|
||||||
|
case 10:
|
||||||
|
return 0.4;
|
||||||
|
case 11:
|
||||||
|
return 0.1;
|
||||||
|
default:
|
||||||
|
return index * 0.05;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
const shade = {
|
const shade = {
|
||||||
L: targetL, // Blend lightness
|
L: targetL, // Blend lightness
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
dist/
|
||||||
|
src/data/*.json
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# @gitbook/fonts
|
||||||
|
|
||||||
|
## 0.1.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- fbfcca5: Initial version of the package
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# `@gitbook/fonts`
|
||||||
|
|
||||||
|
Utilities to lookup default fonts supported by GitBook.
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
import fs from 'node:fs/promises';
|
||||||
|
import path from 'node:path';
|
||||||
|
|
||||||
|
import { APIv2 } from 'google-font-metadata';
|
||||||
|
|
||||||
|
import { CustomizationDefaultFont } from '@gitbook/api';
|
||||||
|
|
||||||
|
import type { FontDefinitions } from '../src/types';
|
||||||
|
|
||||||
|
const googleFontsMap: { [fontName in CustomizationDefaultFont]: string } = {
|
||||||
|
[CustomizationDefaultFont.Inter]: 'inter',
|
||||||
|
[CustomizationDefaultFont.FiraSans]: 'fira-sans-extra-condensed',
|
||||||
|
[CustomizationDefaultFont.IBMPlexSerif]: 'ibm-plex-serif',
|
||||||
|
[CustomizationDefaultFont.Lato]: 'lato',
|
||||||
|
[CustomizationDefaultFont.Merriweather]: 'merriweather',
|
||||||
|
[CustomizationDefaultFont.NotoSans]: 'noto-sans',
|
||||||
|
[CustomizationDefaultFont.OpenSans]: 'open-sans',
|
||||||
|
[CustomizationDefaultFont.Overpass]: 'overpass',
|
||||||
|
[CustomizationDefaultFont.Poppins]: 'poppins',
|
||||||
|
[CustomizationDefaultFont.Raleway]: 'raleway',
|
||||||
|
[CustomizationDefaultFont.Roboto]: 'roboto',
|
||||||
|
[CustomizationDefaultFont.RobotoSlab]: 'roboto-slab',
|
||||||
|
[CustomizationDefaultFont.SourceSansPro]: 'source-sans-3',
|
||||||
|
[CustomizationDefaultFont.Ubuntu]: 'ubuntu',
|
||||||
|
[CustomizationDefaultFont.ABCFavorit]: 'inter',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scripts to generate the list of all icons.
|
||||||
|
*/
|
||||||
|
async function main() {
|
||||||
|
// @ts-expect-error - we build the object
|
||||||
|
const output: FontDefinitions = {};
|
||||||
|
|
||||||
|
for (const font of Object.values(CustomizationDefaultFont)) {
|
||||||
|
const googleFontName = googleFontsMap[font];
|
||||||
|
const fontMetadata = APIv2[googleFontName.toLowerCase()];
|
||||||
|
if (!fontMetadata) {
|
||||||
|
throw new Error(`Font ${googleFontName} not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
output[font] = {
|
||||||
|
font: googleFontName,
|
||||||
|
unicodeRange: fontMetadata.unicodeRange,
|
||||||
|
variants: {
|
||||||
|
'400': {},
|
||||||
|
'700': {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.keys(output[font].variants).forEach((weight) => {
|
||||||
|
const variants = fontMetadata.variants[weight];
|
||||||
|
const normalVariant = variants.normal;
|
||||||
|
if (!normalVariant) {
|
||||||
|
throw new Error(`Font ${googleFontName} has no normal variant`);
|
||||||
|
}
|
||||||
|
|
||||||
|
output[font].variants[weight] = {};
|
||||||
|
Object.entries(normalVariant).forEach(([script, url]) => {
|
||||||
|
output[font].variants[weight][script] = url.url.woff;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeDataFile('fonts', JSON.stringify(output, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We write both in dist and src as the build process might have happen already
|
||||||
|
* and tsc doesn't copy the files.
|
||||||
|
*/
|
||||||
|
async function writeDataFile(name, content) {
|
||||||
|
const srcData = path.resolve(__dirname, '../src/data');
|
||||||
|
const distData = path.resolve(__dirname, '../dist/data');
|
||||||
|
|
||||||
|
// Ensure the directories exists
|
||||||
|
await Promise.all([
|
||||||
|
fs.mkdir(srcData, { recursive: true }),
|
||||||
|
fs.mkdir(distData, { recursive: true }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
fs.writeFile(path.resolve(srcData, `${name}.json`), content),
|
||||||
|
fs.writeFile(path.resolve(distData, `${name}.json`), content),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((error) => {
|
||||||
|
console.error(`Error generating icons list: ${error}`);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "@gitbook/fonts",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"development": "./src/index.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"version": "0.1.0",
|
||||||
|
"dependencies": {
|
||||||
|
"@gitbook/api": "catalog:"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"google-font-metadata": "^6.0.3",
|
||||||
|
"typescript": "^5.5.3"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"generate": "bun ./bin/generate.js",
|
||||||
|
"build": "tsc --project tsconfig.build.json",
|
||||||
|
"typecheck": "tsc --noEmit",
|
||||||
|
"dev": "tsc -w",
|
||||||
|
"clean": "rm -rf ./dist && rm -rf ./src/data",
|
||||||
|
"unit": "bun test"
|
||||||
|
},
|
||||||
|
"files": ["dist", "src", "bin", "README.md", "CHANGELOG.md"],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
// Bun Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`getDefaultFont should return correct object for Latin text 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZ9hjp-Ek-_0ew.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should return correct object for Cyrillic text 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZthjp-Ek-_0ewmM.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should return correct object for Greek text 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZxhjp-Ek-_0ewmM.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should handle mixed script text 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZthjp-Ek-_0ewmM.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should handle different font weights: regular 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZ9hjp-Ek-_0ew.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should handle different font weights: bold 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuFuYAZ9hjp-Ek-_0ew.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should handle different fonts: inter 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Inter",
|
||||||
|
"url": "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfAZ9hjp-Ek-_0ew.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`getDefaultFont should handle different fonts: roboto 1`] = `
|
||||||
|
{
|
||||||
|
"font": "Roboto",
|
||||||
|
"url": "https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Mu4mxMKTU1Kg.woff",
|
||||||
|
}
|
||||||
|
`;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import type { FontDefinitions } from './types';
|
||||||
|
|
||||||
|
import rawFonts from './data/fonts.json' with { type: 'json' };
|
||||||
|
|
||||||
|
export const fonts: FontDefinitions = rawFonts;
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { CustomizationDefaultFont } from '@gitbook/api';
|
||||||
|
import { getDefaultFont } from './getDefaultFont';
|
||||||
|
|
||||||
|
describe('getDefaultFont', () => {
|
||||||
|
it('should return null for invalid font', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: 'invalid-font' as CustomizationDefaultFont,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null for invalid weight', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 999 as any,
|
||||||
|
});
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null for text not supported by any script', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: '😀', // Emoji not supported by Inter
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct object for Latin text', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello World',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result?.font).toBe(CustomizationDefaultFont.Inter);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct object for Cyrillic text', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Привет мир',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result?.font).toBe(CustomizationDefaultFont.Inter);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return correct object for Greek text', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Γεια σας',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result?.font).toBe(CustomizationDefaultFont.Inter);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mixed script text', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello Привет',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result?.font).toBe(CustomizationDefaultFont.Inter);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle different font weights', () => {
|
||||||
|
const regular = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
const bold = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 700,
|
||||||
|
});
|
||||||
|
expect(regular).not.toBeNull();
|
||||||
|
expect(bold).not.toBeNull();
|
||||||
|
expect(regular).toMatchSnapshot('regular');
|
||||||
|
expect(bold).toMatchSnapshot('bold');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty string', () => {
|
||||||
|
const result = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: '',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle different fonts', () => {
|
||||||
|
const inter = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Inter,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
const roboto = getDefaultFont({
|
||||||
|
font: CustomizationDefaultFont.Roboto,
|
||||||
|
text: 'Hello',
|
||||||
|
weight: 400,
|
||||||
|
});
|
||||||
|
expect(inter).not.toBeNull();
|
||||||
|
expect(roboto).not.toBeNull();
|
||||||
|
expect(inter).toMatchSnapshot('inter');
|
||||||
|
expect(roboto).toMatchSnapshot('roboto');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
import type { CustomizationDefaultFont } from '@gitbook/api';
|
||||||
|
import { fonts } from './fonts';
|
||||||
|
import type { FontWeight } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the URL to load a font for a text.
|
||||||
|
*/
|
||||||
|
export function getDefaultFont(input: {
|
||||||
|
/**
|
||||||
|
* GitBook font to use.
|
||||||
|
*/
|
||||||
|
font: CustomizationDefaultFont;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text to display with the font.
|
||||||
|
*/
|
||||||
|
text: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Font weight to use.
|
||||||
|
*/
|
||||||
|
weight: FontWeight;
|
||||||
|
}): { font: string; url: string } | null {
|
||||||
|
if (!input.text.trim()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fontDefinition = fonts[input.font];
|
||||||
|
if (!fontDefinition) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const variant = fontDefinition.variants[`${input.weight}`];
|
||||||
|
if (!variant) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const script = getBestUnicodeRange(input.text, fontDefinition.unicodeRange);
|
||||||
|
if (!script) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return variant[script]
|
||||||
|
? {
|
||||||
|
font: input.font,
|
||||||
|
url: variant[script],
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine which named @font-face unicode-range covers
|
||||||
|
* the greatest share of the characters in `text`.
|
||||||
|
*
|
||||||
|
* @param text The text you want to inspect.
|
||||||
|
* @param ranges An object whose keys are range names and whose
|
||||||
|
* values are CSS-style comma-separated unicode-range
|
||||||
|
* declarations (e.g. "U+0370-03FF,U+1F00-1FFF").
|
||||||
|
* @returns The key of the best-matching range, or `null`
|
||||||
|
* when nothing matches at all.
|
||||||
|
*/
|
||||||
|
function getBestUnicodeRange(text: string, ranges: Record<string, string>): string | null {
|
||||||
|
// ---------- helper: parse "U+XXXX" or "U+XXXX-YYYY" ----------
|
||||||
|
const parseOne = (token: string): [number, number] | null => {
|
||||||
|
token = token.trim().toUpperCase();
|
||||||
|
if (!token.startsWith('U+')) return null;
|
||||||
|
|
||||||
|
const body = token.slice(2); // drop "U+"
|
||||||
|
const [startHex, endHex] = body.split('-');
|
||||||
|
const start = Number.parseInt(startHex, 16);
|
||||||
|
const end = endHex ? Number.parseInt(endHex, 16) : start;
|
||||||
|
|
||||||
|
if (Number.isNaN(start) || Number.isNaN(end) || end < start) return null;
|
||||||
|
return [start, end];
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------- helper: build lookup table ----------
|
||||||
|
const parsed: Record<string, [number, number][]> = {};
|
||||||
|
for (const [label, list] of Object.entries(ranges)) {
|
||||||
|
parsed[label] = list
|
||||||
|
.split(',')
|
||||||
|
.map(parseOne)
|
||||||
|
.filter((x): x is [number, number] => x !== null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- tally code-point hits ----------
|
||||||
|
const hits: Record<string, number> = Object.fromEntries(Object.keys(parsed).map((k) => [k, 0]));
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; ) {
|
||||||
|
const cp = text.codePointAt(i)!;
|
||||||
|
i += cp > 0xffff ? 2 : 1; // advance by 1 UTF-16 code-unit (or 2 for surrogates)
|
||||||
|
|
||||||
|
for (const [label, rangesArr] of Object.entries(parsed)) {
|
||||||
|
if (rangesArr.some(([lo, hi]) => cp >= lo && cp <= hi)) {
|
||||||
|
hits[label]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- choose the "best" ----------
|
||||||
|
let winner: string | null = null;
|
||||||
|
let maxCount = 0;
|
||||||
|
|
||||||
|
for (const [label, count] of Object.entries(hits)) {
|
||||||
|
if (count > maxCount) {
|
||||||
|
maxCount = count;
|
||||||
|
winner = label;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxCount > 0 ? winner : null;
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './getDefaultFont';
|
||||||
|
export * from './types';
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { CustomizationDefaultFont } from '@gitbook/api';
|
||||||
|
|
||||||
|
export type FontWeight = 400 | 700;
|
||||||
|
|
||||||
|
export type FontDefinition = {
|
||||||
|
font: string;
|
||||||
|
unicodeRange: {
|
||||||
|
[script: string]: string;
|
||||||
|
};
|
||||||
|
variants: {
|
||||||
|
[weight in string]: {
|
||||||
|
[script: string]: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type FontDefinitions = { [fontName in CustomizationDefaultFont]: FontDefinition };
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"exclude": ["node_modules", "src/**/*.test.ts"]
|
||||||
|
}
|
||||||
@@ -1,20 +1,23 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"lib": ["es2023"],
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"module": "ESNext",
|
||||||
"allowJs": true,
|
"target": "es2022",
|
||||||
"skipLibCheck": true,
|
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowJs": true,
|
||||||
"noEmit": false,
|
"noEmit": false,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"esModuleInterop": true,
|
|
||||||
"module": "esnext",
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"types": ["./.wrangler/types/runtime.d.ts"]
|
"types": [
|
||||||
|
"bun-types" // add Bun global
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts"],
|
"include": ["src/**/*.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"extends": ["//"],
|
||||||
|
"tasks": {
|
||||||
|
"generate": {
|
||||||
|
"inputs": ["bin/**/*", "package.json"],
|
||||||
|
"outputs": ["src/data/*.json", "dist/data/*.json"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
# next.js
|
|
||||||
/.next/
|
|
||||||
|
|
||||||
# vercel
|
|
||||||
.vercel
|
|
||||||
|
|
||||||
# cloudflare
|
|
||||||
.open-next
|
|
||||||
|
|
||||||
# Symbolic links
|
|
||||||
public
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
# gitbook-v2
|
|
||||||
|
|
||||||
## 0.2.0
|
|
||||||
|
|
||||||
### Minor Changes
|
|
||||||
|
|
||||||
- 76c7974: Add route to revalidate cached data
|
|
||||||
|
|
||||||
## 0.1.2
|
|
||||||
|
|
||||||
### Patch Changes
|
|
||||||
|
|
||||||
- 05ffd0e: Improving data cache management for computed content
|
|
||||||
- Updated dependencies [05ffd0e]
|
|
||||||
- @gitbook/cache-tags@0.1.0
|
|
||||||
|
|
||||||
## 0.1.1
|
|
||||||
|
|
||||||
### Patch Changes
|
|
||||||
|
|
||||||
- 3e11678: fix: lost section groups
|
|
||||||
|
|
||||||
## 0.1.0
|
|
||||||
|
|
||||||
### Minor Changes
|
|
||||||
|
|
||||||
- cfccc44: Setup structure and deployment for new version
|
|
||||||
Vendored
-5
@@ -1,5 +0,0 @@
|
|||||||
/// <reference types="next" />
|
|
||||||
/// <reference types="next/image-types/global" />
|
|
||||||
|
|
||||||
// NOTE: This file should not be edited
|
|
||||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import { defineCloudflareConfig } from '@opennextjs/cloudflare';
|
|
||||||
import d1TagCache from '@opennextjs/cloudflare/d1-tag-cache';
|
|
||||||
import kvIncrementalCache from '@opennextjs/cloudflare/kv-cache';
|
|
||||||
import memoryQueue from '@opennextjs/cloudflare/memory-queue';
|
|
||||||
|
|
||||||
export default defineCloudflareConfig({
|
|
||||||
incrementalCache: kvIncrementalCache,
|
|
||||||
queue: memoryQueue,
|
|
||||||
tagCache: d1TagCache,
|
|
||||||
});
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "gitbook-v2",
|
|
||||||
"version": "0.2.0",
|
|
||||||
"private": true,
|
|
||||||
"dependencies": {
|
|
||||||
"next": "^15.2.1",
|
|
||||||
"react": "^19.0.0",
|
|
||||||
"react-dom": "^19.0.0",
|
|
||||||
"@gitbook/api": "0.96.1",
|
|
||||||
"@gitbook/cache-tags": "workspace:*",
|
|
||||||
"@sindresorhus/fnv1a": "^3.1.0",
|
|
||||||
"server-only": "^0.0.1",
|
|
||||||
"warn-once": "^0.1.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"gitbook": "*",
|
|
||||||
"@opennextjs/cloudflare": "https://pkg.pr.new/opennextjs/opennextjs-cloudflare/@opennextjs/cloudflare@236c84d",
|
|
||||||
"tailwindcss": "^3.4.0",
|
|
||||||
"postcss": "^8"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"generate": "rm -rf ./public && cp -r ../gitbook/public ./public",
|
|
||||||
"dev:v2": "env-cmd --silent -f ../../.env.local next --turbopack",
|
|
||||||
"build": "next build",
|
|
||||||
"build:v2": "next build",
|
|
||||||
"start": "next start",
|
|
||||||
"build:v2:cloudflare": "opennextjs-cloudflare",
|
|
||||||
"dev:v2:cloudflare": "wrangler dev --port 8771",
|
|
||||||
"unit": "bun test"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
plugins: {
|
|
||||||
tailwindcss: {},
|
|
||||||
autoprefixer: {},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import type { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
import { serveLLMsTxt } from '@/routes/llms';
|
|
||||||
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: NextRequest,
|
|
||||||
{ params }: { params: Promise<RouteLayoutParams> }
|
|
||||||
) {
|
|
||||||
const context = await getDynamicSiteContext(await params);
|
|
||||||
return serveLLMsTxt(context);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import type { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
import { serveRobotsTxt } from '@/routes/robots';
|
|
||||||
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: NextRequest,
|
|
||||||
{ params }: { params: Promise<RouteLayoutParams> }
|
|
||||||
) {
|
|
||||||
const context = await getDynamicSiteContext(await params);
|
|
||||||
return serveRobotsTxt(context);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import type { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
import { servePagesSitemap } from '@/routes/sitemap';
|
|
||||||
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: NextRequest,
|
|
||||||
{ params }: { params: Promise<RouteLayoutParams> }
|
|
||||||
) {
|
|
||||||
const context = await getDynamicSiteContext(await params);
|
|
||||||
return servePagesSitemap(context);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import type { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
import { serveRootSitemap } from '@/routes/sitemap';
|
|
||||||
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: NextRequest,
|
|
||||||
{ params }: { params: Promise<RouteLayoutParams> }
|
|
||||||
) {
|
|
||||||
const context = await getDynamicSiteContext(await params);
|
|
||||||
return serveRootSitemap(context);
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
import {
|
|
||||||
fetchSiteContextByURL,
|
|
||||||
fetchSiteContextByURLLookup,
|
|
||||||
getBaseContext,
|
|
||||||
} from '@v2/lib/context';
|
|
||||||
import { getSiteURLDataFromMiddleware } from '@v2/lib/middleware';
|
|
||||||
|
|
||||||
export type RouteParamMode = 'url-host' | 'url';
|
|
||||||
|
|
||||||
export type RouteLayoutParams = {
|
|
||||||
mode: string;
|
|
||||||
|
|
||||||
/** URL encoded site URL */
|
|
||||||
siteURL: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type RouteParams = RouteLayoutParams & {
|
|
||||||
pagePath: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the static context when rendering statically a site.
|
|
||||||
*/
|
|
||||||
export function getStaticSiteContext(params: RouteLayoutParams) {
|
|
||||||
const siteURL = getSiteURLFromParams(params);
|
|
||||||
return fetchSiteContextByURL(
|
|
||||||
getBaseContext({
|
|
||||||
siteURL,
|
|
||||||
urlMode: getModeFromParams(params.mode),
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
url: siteURL.toString(),
|
|
||||||
visitorAuthToken: null,
|
|
||||||
redirectOnError: false,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the site context when rendering dynamically.
|
|
||||||
* The context will depend on the request.
|
|
||||||
*/
|
|
||||||
export async function getDynamicSiteContext(params: RouteLayoutParams) {
|
|
||||||
const siteURL = getSiteURLFromParams(params);
|
|
||||||
const siteURLData = await getSiteURLDataFromMiddleware();
|
|
||||||
|
|
||||||
return fetchSiteContextByURLLookup(
|
|
||||||
getBaseContext({
|
|
||||||
siteURL,
|
|
||||||
urlMode: getModeFromParams(params.mode),
|
|
||||||
}),
|
|
||||||
siteURLData
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the decoded page path from the params.
|
|
||||||
*/
|
|
||||||
export function getPagePathFromParams(params: RouteParams) {
|
|
||||||
const decoded = decodeURIComponent(params.pagePath);
|
|
||||||
return decoded;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSiteURLFromParams(params: RouteLayoutParams) {
|
|
||||||
const decoded = decodeURIComponent(params.siteURL);
|
|
||||||
const url = new URL(`https://${decoded}`);
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getModeFromParams(mode: string): RouteParamMode {
|
|
||||||
if (mode === 'url-host') {
|
|
||||||
return 'url-host';
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'url';
|
|
||||||
}
|
|
||||||
-2
@@ -1,2 +0,0 @@
|
|||||||
import RootLayout from '@v2/app/~space/[spaceId]/~gitbook/pdf/layout';
|
|
||||||
export default RootLayout;
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
import PDFPage, { generateMetadata } from '@v2/app/~space/[spaceId]/~gitbook/pdf/page';
|
|
||||||
|
|
||||||
export default PDFPage;
|
|
||||||
export { generateMetadata };
|
|
||||||
-2
@@ -1,2 +0,0 @@
|
|||||||
import RootLayout from '@v2/app/~space/[spaceId]/~gitbook/pdf/layout';
|
|
||||||
export default RootLayout;
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
import PDFPage, { generateMetadata } from '@v2/app/~space/[spaceId]/~gitbook/pdf/page';
|
|
||||||
|
|
||||||
export default PDFPage;
|
|
||||||
export { generateMetadata };
|
|
||||||
@@ -1,554 +0,0 @@
|
|||||||
import { type ComputedContentSource, GitBookAPI } from '@gitbook/api';
|
|
||||||
import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-tags';
|
|
||||||
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
|
|
||||||
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
|
|
||||||
import { wrapDataFetcherError } from './errors';
|
|
||||||
import type { GitBookDataFetcher } from './types';
|
|
||||||
|
|
||||||
interface DataFetcherInput {
|
|
||||||
/**
|
|
||||||
* API host to use.
|
|
||||||
*/
|
|
||||||
apiEndpoint: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* API token.
|
|
||||||
*/
|
|
||||||
apiToken: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const commonInput: DataFetcherInput = {
|
|
||||||
apiEndpoint: GITBOOK_API_URL,
|
|
||||||
apiToken: GITBOOK_API_TOKEN,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a data fetcher using an API token.
|
|
||||||
* The data are being cached by Next.js built-in cache.
|
|
||||||
*/
|
|
||||||
export function createDataFetcher(input: DataFetcherInput = commonInput): GitBookDataFetcher {
|
|
||||||
return {
|
|
||||||
apiEndpoint: input.apiEndpoint,
|
|
||||||
|
|
||||||
async api() {
|
|
||||||
return getAPI(input);
|
|
||||||
},
|
|
||||||
|
|
||||||
withToken({ apiToken }) {
|
|
||||||
return createDataFetcher({
|
|
||||||
...input,
|
|
||||||
apiToken,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
//
|
|
||||||
// API that are tied to the token
|
|
||||||
//
|
|
||||||
getPublishedContentSite(params) {
|
|
||||||
return getPublishedContentSite(input, {
|
|
||||||
organizationId: params.organizationId,
|
|
||||||
siteId: params.siteId,
|
|
||||||
siteShareKey: params.siteShareKey,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getSiteRedirectBySource(params) {
|
|
||||||
return getSiteRedirectBySource(input, {
|
|
||||||
organizationId: params.organizationId,
|
|
||||||
siteId: params.siteId,
|
|
||||||
siteShareKey: params.siteShareKey,
|
|
||||||
source: params.source,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getRevision(params) {
|
|
||||||
return getRevision(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
revisionId: params.revisionId,
|
|
||||||
metadata: params.metadata,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getRevisionPages(params) {
|
|
||||||
return getRevisionPages(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
revisionId: params.revisionId,
|
|
||||||
metadata: params.metadata,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getRevisionFile(params) {
|
|
||||||
return getRevisionFile(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
revisionId: params.revisionId,
|
|
||||||
fileId: params.fileId,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getRevisionPageByPath(params) {
|
|
||||||
return getRevisionPageByPath(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
revisionId: params.revisionId,
|
|
||||||
path: params.path,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getReusableContent(params) {
|
|
||||||
return getReusableContent(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
revisionId: params.revisionId,
|
|
||||||
reusableContentId: params.reusableContentId,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getLatestOpenAPISpecVersionContent(params) {
|
|
||||||
return getLatestOpenAPISpecVersionContent(input, {
|
|
||||||
organizationId: params.organizationId,
|
|
||||||
slug: params.slug,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getSpace(params) {
|
|
||||||
return getSpace(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
shareKey: params.shareKey,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getChangeRequest(params) {
|
|
||||||
return getChangeRequest(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
changeRequestId: params.changeRequestId,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getDocument(params) {
|
|
||||||
return getDocument(input, {
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
documentId: params.documentId,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getComputedDocument(params) {
|
|
||||||
return getComputedDocument(input, {
|
|
||||||
organizationId: params.organizationId,
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
source: params.source,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getEmbedByUrl(params) {
|
|
||||||
return getEmbedByUrl(input, {
|
|
||||||
url: params.url,
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
searchSiteContent(params) {
|
|
||||||
return searchSiteContent(input, params);
|
|
||||||
},
|
|
||||||
|
|
||||||
//
|
|
||||||
// API that are not tied to the token
|
|
||||||
// where the data is the same for all users
|
|
||||||
//
|
|
||||||
getUserById(userId) {
|
|
||||||
return getUserById(commonInput, userId);
|
|
||||||
},
|
|
||||||
getPublishedContentByUrl(params) {
|
|
||||||
return getPublishedContentByUrl(commonInput, {
|
|
||||||
url: params.url,
|
|
||||||
visitorAuthToken: params.visitorAuthToken,
|
|
||||||
redirectOnError: params.redirectOnError,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getUserById(input: DataFetcherInput, userId: string) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).users.getUserById(userId);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getSpace(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
shareKey: string | undefined;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('days');
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'space',
|
|
||||||
space: params.spaceId,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getSpaceById(params.spaceId, {
|
|
||||||
shareKey: params.shareKey,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getChangeRequest(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
changeRequestId: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('minutes');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getChangeRequestById(
|
|
||||||
params.spaceId,
|
|
||||||
params.changeRequestId
|
|
||||||
);
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'change-request',
|
|
||||||
space: params.spaceId,
|
|
||||||
changeRequest: res.data.id,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getRevision(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
metadata: boolean;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getRevisionById(params.spaceId, params.revisionId, {
|
|
||||||
metadata: params.metadata,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getRevisionPages(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
metadata: boolean;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.listPagesInRevisionById(
|
|
||||||
params.spaceId,
|
|
||||||
params.revisionId,
|
|
||||||
{
|
|
||||||
metadata: params.metadata,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return res.data.pages;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getRevisionFile(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
fileId: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getFileInRevisionById(
|
|
||||||
params.spaceId,
|
|
||||||
params.revisionId,
|
|
||||||
params.fileId
|
|
||||||
);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getRevisionPageByPath(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
path: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
const encodedPath = encodeURIComponent(params.path);
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getPageInRevisionByPath(
|
|
||||||
params.spaceId,
|
|
||||||
params.revisionId,
|
|
||||||
encodedPath
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getDocument(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
documentId: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getDocumentById(params.spaceId, params.documentId);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getComputedDocument(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
organizationId: string;
|
|
||||||
source: ComputedContentSource;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
cacheTag(
|
|
||||||
...getComputedContentSourceCacheTags(
|
|
||||||
{
|
|
||||||
spaceId: params.spaceId,
|
|
||||||
organizationId: params.organizationId,
|
|
||||||
},
|
|
||||||
params.source
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getComputedDocument(params.spaceId, {
|
|
||||||
source: params.source,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getReusableContent(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
reusableContentId: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('max');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).spaces.getReusableContentInRevisionById(
|
|
||||||
params.spaceId,
|
|
||||||
params.revisionId,
|
|
||||||
params.reusableContentId
|
|
||||||
);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getLatestOpenAPISpecVersionContent(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
organizationId: string;
|
|
||||||
slug: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'openapi',
|
|
||||||
organization: params.organizationId,
|
|
||||||
openAPISpec: params.slug,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).orgs.getLatestOpenApiSpecVersionContent(
|
|
||||||
params.organizationId,
|
|
||||||
params.slug
|
|
||||||
);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getPublishedContentByUrl(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
url: string;
|
|
||||||
visitorAuthToken: string | null;
|
|
||||||
redirectOnError: boolean;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
const { url, visitorAuthToken, redirectOnError } = params;
|
|
||||||
|
|
||||||
const hostname = new URL(url).hostname;
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'url',
|
|
||||||
hostname,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).urls.getPublishedContentByUrl({
|
|
||||||
url,
|
|
||||||
visitorAuthToken: visitorAuthToken ?? undefined,
|
|
||||||
redirectOnError,
|
|
||||||
});
|
|
||||||
|
|
||||||
if ('site' in res.data) {
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'site',
|
|
||||||
site: res.data.site,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getPublishedContentSite(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
organizationId: string;
|
|
||||||
siteId: string;
|
|
||||||
siteShareKey: string | undefined;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'site',
|
|
||||||
site: params.siteId,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).orgs.getPublishedContentSite(
|
|
||||||
params.organizationId,
|
|
||||||
params.siteId,
|
|
||||||
{
|
|
||||||
shareKey: params.siteShareKey,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getSiteRedirectBySource(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
organizationId: string;
|
|
||||||
siteId: string;
|
|
||||||
siteShareKey: string | undefined;
|
|
||||||
source: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheTag(
|
|
||||||
getCacheTag({
|
|
||||||
tag: 'site',
|
|
||||||
site: params.siteId,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).orgs.getSiteRedirectBySource(
|
|
||||||
params.organizationId,
|
|
||||||
params.siteId,
|
|
||||||
{
|
|
||||||
shareKey: params.siteShareKey,
|
|
||||||
source: params.source,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getEmbedByUrl(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: {
|
|
||||||
url: string;
|
|
||||||
spaceId: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
cacheLife('weeks');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const api = getAPI(input);
|
|
||||||
const res = await api.spaces.getEmbedByUrlInSpace(params.spaceId, { url: params.url });
|
|
||||||
return res.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function searchSiteContent(
|
|
||||||
input: DataFetcherInput,
|
|
||||||
params: Parameters<GitBookDataFetcher['searchSiteContent']>[0]
|
|
||||||
) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
const { organizationId, siteId, query, scope } = params;
|
|
||||||
|
|
||||||
cacheLife('days');
|
|
||||||
|
|
||||||
return wrapDataFetcherError(async () => {
|
|
||||||
const res = await getAPI(input).orgs.searchSiteContent(organizationId, siteId, {
|
|
||||||
query,
|
|
||||||
...scope,
|
|
||||||
});
|
|
||||||
return res.data.items;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAPI(input: DataFetcherInput) {
|
|
||||||
const { apiEndpoint, apiToken } = input;
|
|
||||||
const api = new GitBookAPI({
|
|
||||||
authToken: apiToken ?? undefined,
|
|
||||||
endpoint: apiEndpoint,
|
|
||||||
userAgent: GITBOOK_USER_AGENT,
|
|
||||||
});
|
|
||||||
|
|
||||||
return api;
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
import { GitBookAPIError } from '@gitbook/api';
|
|
||||||
import type { DataFetcherErrorData, DataFetcherResponse } from './types';
|
|
||||||
|
|
||||||
export class DataFetcherError extends Error {
|
|
||||||
constructor(
|
|
||||||
message: string,
|
|
||||||
public readonly code: number
|
|
||||||
) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Throw an error if the response contains an error.
|
|
||||||
*/
|
|
||||||
export function throwIfDataError<T>(response: DataFetcherResponse<T>): T;
|
|
||||||
export function throwIfDataError<T>(response: Promise<DataFetcherResponse<T>>): Promise<T>;
|
|
||||||
export function throwIfDataError<T>(
|
|
||||||
response: DataFetcherResponse<T> | Promise<DataFetcherResponse<T>>
|
|
||||||
): T | Promise<T> {
|
|
||||||
if (response instanceof Promise) {
|
|
||||||
return response.then((result) => throwIfDataError(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.error) {
|
|
||||||
throw new DataFetcherError(response.error.message, response.error.code);
|
|
||||||
}
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the data from the response or null if there is an "Not found" error.
|
|
||||||
*/
|
|
||||||
export function getDataOrNull<T>(
|
|
||||||
response: DataFetcherResponse<T>,
|
|
||||||
ignoreErrors?: number[]
|
|
||||||
): T | null;
|
|
||||||
export function getDataOrNull<T>(
|
|
||||||
response: Promise<DataFetcherResponse<T>>,
|
|
||||||
ignoreErrors?: number[]
|
|
||||||
): Promise<T | null>;
|
|
||||||
export function getDataOrNull<T>(
|
|
||||||
response: DataFetcherResponse<T> | Promise<DataFetcherResponse<T>>,
|
|
||||||
ignoreErrors: number[] = [404]
|
|
||||||
): T | null | Promise<T | null> {
|
|
||||||
if (response instanceof Promise) {
|
|
||||||
return response.then((result) => getDataOrNull(result, ignoreErrors));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.error) {
|
|
||||||
if (ignoreErrors.includes(response.error.code)) return null;
|
|
||||||
throw new DataFetcherError(response.error.message, response.error.code);
|
|
||||||
}
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ignore error for an API or data call.
|
|
||||||
*/
|
|
||||||
export async function ignoreDataThrownError<T>(promise: Promise<T>): Promise<T | null> {
|
|
||||||
try {
|
|
||||||
return await promise;
|
|
||||||
} catch (error) {
|
|
||||||
getExposableError(error as Error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ignore all errors for an API or data call.
|
|
||||||
*/
|
|
||||||
export async function ignoreAllThrownError<T>(promise: Promise<T>): Promise<T | null> {
|
|
||||||
try {
|
|
||||||
return await promise;
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Ignoring error', error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrap an async execution to handle errors and return a DataFetcherResponse.
|
|
||||||
*/
|
|
||||||
export async function wrapDataFetcherError<T>(
|
|
||||||
fn: () => Promise<T>
|
|
||||||
): Promise<DataFetcherResponse<T>> {
|
|
||||||
try {
|
|
||||||
return { data: await fn() };
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
error: getExposableError(error as Error),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a data fetcher exposable error from a JS error.
|
|
||||||
*/
|
|
||||||
export function getExposableError(error: Error): DataFetcherErrorData {
|
|
||||||
if (error instanceof GitBookAPIError) {
|
|
||||||
return {
|
|
||||||
code: error.code,
|
|
||||||
message: error.errorMessage,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error instanceof DataFetcherError) {
|
|
||||||
return {
|
|
||||||
code: error.code,
|
|
||||||
message: error.message,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import type { JSONDocument, RevisionPageDocument, Space } from '@gitbook/api';
|
|
||||||
import { getDataOrNull } from './errors';
|
|
||||||
import type { GitBookDataFetcher } from './types';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the document for a page.
|
|
||||||
*/
|
|
||||||
export async function getPageDocument(
|
|
||||||
dataFetcher: GitBookDataFetcher,
|
|
||||||
space: Space,
|
|
||||||
page: RevisionPageDocument
|
|
||||||
): Promise<JSONDocument | null> {
|
|
||||||
if (page.documentId) {
|
|
||||||
return getDataOrNull(
|
|
||||||
dataFetcher.getDocument({ spaceId: space.id, documentId: page.documentId })
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (page.computed) {
|
|
||||||
return getDataOrNull(
|
|
||||||
dataFetcher.getComputedDocument({
|
|
||||||
organizationId: space.organization,
|
|
||||||
spaceId: space.id,
|
|
||||||
source: page.computed,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@@ -1,243 +0,0 @@
|
|||||||
import 'server-only';
|
|
||||||
|
|
||||||
import { GITBOOK_IMAGE_RESIZE_SIGNING_KEY, GITBOOK_IMAGE_RESIZE_URL } from '../env';
|
|
||||||
import type { GitBookSpaceLinker } from '../links';
|
|
||||||
import { type SignatureVersion, generateImageSignature } from './signatures';
|
|
||||||
import type { ImageResizer } from './types';
|
|
||||||
|
|
||||||
interface CloudflareImageJsonFormat {
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
original: {
|
|
||||||
file_size: number;
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
format: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* https://developers.cloudflare.com/images/image-resizing/resize-with-workers/
|
|
||||||
*/
|
|
||||||
export interface CloudflareImageOptions {
|
|
||||||
format?: 'webp' | 'avif' | 'json' | 'jpeg';
|
|
||||||
fit?: 'scale-down' | 'contain' | 'cover' | 'crop' | 'pad';
|
|
||||||
width?: number;
|
|
||||||
height?: number;
|
|
||||||
dpr?: number;
|
|
||||||
anim?: boolean;
|
|
||||||
quality?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an image resizer for a rendering context.
|
|
||||||
*/
|
|
||||||
export function createImageResizer({
|
|
||||||
host,
|
|
||||||
linker,
|
|
||||||
}: {
|
|
||||||
/** The linker to use to create URLs. */
|
|
||||||
linker: GitBookSpaceLinker;
|
|
||||||
/** The host name of the current site. */
|
|
||||||
host: string;
|
|
||||||
}): ImageResizer {
|
|
||||||
if (!GITBOOK_IMAGE_RESIZE_URL || !GITBOOK_IMAGE_RESIZE_SIGNING_KEY) {
|
|
||||||
return createNoopImageResizer();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
getResizedImageURL: (urlInput) => {
|
|
||||||
if (!checkIsSizableImageURL(urlInput)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let cachedSignature: {
|
|
||||||
signature: string;
|
|
||||||
version: SignatureVersion;
|
|
||||||
} | null = null;
|
|
||||||
|
|
||||||
return async (options) => {
|
|
||||||
cachedSignature ??= await generateImageSignature({
|
|
||||||
host,
|
|
||||||
url: urlInput,
|
|
||||||
});
|
|
||||||
|
|
||||||
const url = linker.toAbsoluteURL(linker.toPathInContent('/~gitbook/image'));
|
|
||||||
const searchParams = new URLSearchParams();
|
|
||||||
searchParams.set('url', getImageAPIUrl(urlInput));
|
|
||||||
|
|
||||||
if (options.width) {
|
|
||||||
searchParams.set('width', options.width.toString());
|
|
||||||
}
|
|
||||||
if (options.height) {
|
|
||||||
searchParams.set('height', options.height.toString());
|
|
||||||
}
|
|
||||||
if (options.dpr) {
|
|
||||||
searchParams.set('dpr', options.dpr.toString());
|
|
||||||
}
|
|
||||||
if (options.quality) {
|
|
||||||
searchParams.set('quality', options.quality.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
searchParams.set('sign', cachedSignature.signature);
|
|
||||||
searchParams.set('sv', cachedSignature.version);
|
|
||||||
|
|
||||||
return `${url}?${searchParams.toString()}`;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
getImageSize: async (input, options) => {
|
|
||||||
if (!checkIsSizableImageURL(input)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return getImageSize(input, options);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an image resizer that doesn't do any resizing.
|
|
||||||
*/
|
|
||||||
export function createNoopImageResizer(): ImageResizer {
|
|
||||||
return {
|
|
||||||
getResizedImageURL: () => null,
|
|
||||||
getImageSize: async (_input) => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a URL is an HTTP URL.
|
|
||||||
*/
|
|
||||||
export function checkIsHttpURL(input: string | URL): boolean {
|
|
||||||
if (!URL.canParse(input)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const parsed = new URL(input);
|
|
||||||
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if an image URL is resizable.
|
|
||||||
* Skip it for non-http(s) URLs (data, etc).
|
|
||||||
* Skip it for SVGs.
|
|
||||||
* Skip it for GitBook images (to avoid recursion).
|
|
||||||
*/
|
|
||||||
export function checkIsSizableImageURL(input: string): boolean {
|
|
||||||
if (!URL.canParse(input)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input.includes('/~gitbook/image')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsed = new URL(input);
|
|
||||||
if (parsed.pathname.endsWith('.svg') || parsed.pathname.endsWith('.avif')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!checkIsHttpURL(parsed)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the size of an image.
|
|
||||||
*/
|
|
||||||
export async function getImageSize(
|
|
||||||
input: string,
|
|
||||||
defaultSize: Partial<CloudflareImageOptions> = {}
|
|
||||||
): Promise<{ width: number; height: number } | null> {
|
|
||||||
if (!checkIsSizableImageURL(input)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await resizeImage(input, {
|
|
||||||
// Abort the request after 2 seconds to avoid blocking rendering for too long
|
|
||||||
signal: AbortSignal.timeout(2000),
|
|
||||||
// Measure size and resize it to the most common size
|
|
||||||
// to optimize caching
|
|
||||||
...defaultSize,
|
|
||||||
format: 'json',
|
|
||||||
anim: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const json = (await response.json()) as CloudflareImageJsonFormat;
|
|
||||||
return {
|
|
||||||
width: json.original.width,
|
|
||||||
height: json.original.height,
|
|
||||||
};
|
|
||||||
} catch (_error) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute a Cloudflare Image Resize operation on an image.
|
|
||||||
*/
|
|
||||||
export async function resizeImage(
|
|
||||||
input: string,
|
|
||||||
options: CloudflareImageOptions & {
|
|
||||||
signal?: AbortSignal;
|
|
||||||
}
|
|
||||||
): Promise<Response> {
|
|
||||||
const { signal, ...resizeOptions } = options;
|
|
||||||
|
|
||||||
const parsed = new URL(input);
|
|
||||||
if (parsed.protocol === 'data:') {
|
|
||||||
throw new Error('Cannot resize data: URLs');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.hostname === 'localhost') {
|
|
||||||
throw new Error('Cannot resize localhost URLs');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since Cloudflare Images options on fetch are not supported on Cloudflare Pages,
|
|
||||||
// we need to use the Cloudflare Image Resize API directly.
|
|
||||||
if (!GITBOOK_IMAGE_RESIZE_URL) {
|
|
||||||
throw new Error('GITBOOK_IMAGE_RESIZE_URL is not set');
|
|
||||||
}
|
|
||||||
|
|
||||||
return await fetch(
|
|
||||||
`${GITBOOK_IMAGE_RESIZE_URL}${stringifyOptions(
|
|
||||||
resizeOptions
|
|
||||||
)}/${encodeURIComponent(input)}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
|
|
||||||
Accept:
|
|
||||||
resizeOptions.format === 'json'
|
|
||||||
? 'application/json'
|
|
||||||
: `image/${resizeOptions.format || 'jpeg'}`,
|
|
||||||
},
|
|
||||||
signal,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringifyOptions(options: CloudflareImageOptions): string {
|
|
||||||
return Object.entries({ ...options }).reduce((rest, [key, value]) => {
|
|
||||||
return `${rest}${rest ? ',' : ''}${key}=${value}`;
|
|
||||||
}, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Because of a bug in Cloudflare, 127.0.0.1 is replaced by localhost.
|
|
||||||
* We protect against it by converting to a special token, and then parsing
|
|
||||||
* the token in the image API.
|
|
||||||
*/
|
|
||||||
const GITBOOK_LOCALHOST_TOKEN = '$GITBOOK_LOCALHOST$';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare a URL for the GitBook Open Image API.
|
|
||||||
*/
|
|
||||||
export function getImageAPIUrl(url: string): string {
|
|
||||||
return url.replaceAll('127.0.0.1', GITBOOK_LOCALHOST_TOKEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseImageAPIURL(url: string): string {
|
|
||||||
return url.replaceAll(GITBOOK_LOCALHOST_TOKEN, '127.0.0.1');
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
import { describe, expect, it } from 'bun:test';
|
|
||||||
import { appendBasePathToLinker, createLinker } from './links';
|
|
||||||
|
|
||||||
const root = createLinker({
|
|
||||||
host: 'docs.company.com',
|
|
||||||
pathname: '/',
|
|
||||||
});
|
|
||||||
|
|
||||||
const variantInSection = createLinker({
|
|
||||||
host: 'docs.company.com',
|
|
||||||
pathname: '/section/variant',
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('toPathInContent', () => {
|
|
||||||
it('should return the correct path', () => {
|
|
||||||
expect(root.toPathInContent('some/path')).toBe('/some/path');
|
|
||||||
expect(variantInSection.toPathInContent('some/path')).toBe('/section/variant/some/path');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle leading slash', () => {
|
|
||||||
expect(root.toPathInContent('/some/path')).toBe('/some/path');
|
|
||||||
expect(variantInSection.toPathInContent('/some/path')).toBe('/section/variant/some/path');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('toAbsoluteURL', () => {
|
|
||||||
it('should return the correct path', () => {
|
|
||||||
expect(root.toAbsoluteURL('some/path')).toBe('https://docs.company.com/some/path');
|
|
||||||
expect(variantInSection.toAbsoluteURL('some/path')).toBe(
|
|
||||||
'https://docs.company.com/some/path'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('appendBasePathToLinker', () => {
|
|
||||||
const prefixedRoot = appendBasePathToLinker(root, '/section/variant');
|
|
||||||
const prefixedVariantInSection = appendBasePathToLinker(variantInSection, '/base');
|
|
||||||
|
|
||||||
describe('toPathInContent', () => {
|
|
||||||
it('should return the correct path', () => {
|
|
||||||
expect(prefixedRoot.toPathInContent('some/path')).toBe('/section/variant/some/path');
|
|
||||||
expect(prefixedVariantInSection.toPathInContent('some/path')).toBe(
|
|
||||||
'/section/variant/base/some/path'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('toAbsoluteURL', () => {
|
|
||||||
it('should return the correct path', () => {
|
|
||||||
expect(prefixedRoot.toAbsoluteURL('some/path')).toBe(
|
|
||||||
'https://docs.company.com/some/path'
|
|
||||||
);
|
|
||||||
expect(prefixedVariantInSection.toAbsoluteURL('some/path')).toBe(
|
|
||||||
'https://docs.company.com/some/path'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
import { getPagePath } from '@/lib/pages';
|
|
||||||
import type { RevisionPage, RevisionPageDocument, RevisionPageGroup } from '@gitbook/api';
|
|
||||||
import warnOnce from 'warn-once';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic interface to generate links based on a given context.
|
|
||||||
*
|
|
||||||
* URL levels:
|
|
||||||
*
|
|
||||||
* https://docs.company.com/section/variant/page
|
|
||||||
*
|
|
||||||
* toPathInContent('some/path') => /section/variant/some/path
|
|
||||||
* toPathForPage({ pages, page }) => /section/variant/some/path
|
|
||||||
* toAbsoluteURL('some/path') => https://docs.company.com/some/path
|
|
||||||
*/
|
|
||||||
export interface GitBookSpaceLinker {
|
|
||||||
/**
|
|
||||||
* Generate an absolute path for a relative path to the current content.
|
|
||||||
*/
|
|
||||||
toPathInContent(relativePath: string): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate an absolute path for a page in the current content.
|
|
||||||
* The result should NOT be passed to `toPathInContent`.
|
|
||||||
*/
|
|
||||||
toPathForPage(input: {
|
|
||||||
pages: RevisionPage[];
|
|
||||||
page: RevisionPageDocument | RevisionPageGroup;
|
|
||||||
anchor?: string;
|
|
||||||
}): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate an absolute URL for a given path relative to the host of the current content.
|
|
||||||
*/
|
|
||||||
toAbsoluteURL(absolutePath: string): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a link (URL or path) for a GitBook content URL (url of another site)
|
|
||||||
*/
|
|
||||||
toLinkForContent(url: string): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a linker to resolve links in a context being served on a specific URL.
|
|
||||||
*/
|
|
||||||
export function createLinker(
|
|
||||||
/** Where the top of the space is served on */
|
|
||||||
servedOn: {
|
|
||||||
protocol?: string;
|
|
||||||
host?: string;
|
|
||||||
pathname: string;
|
|
||||||
}
|
|
||||||
): GitBookSpaceLinker {
|
|
||||||
warnOnce(!servedOn.host, 'No host provided to createLinker. It can lead to issues with links.');
|
|
||||||
|
|
||||||
const linker: GitBookSpaceLinker = {
|
|
||||||
toPathInContent(relativePath: string): string {
|
|
||||||
return joinPaths(servedOn.pathname, relativePath);
|
|
||||||
},
|
|
||||||
|
|
||||||
toAbsoluteURL(absolutePath: string): string {
|
|
||||||
if (!servedOn.host) {
|
|
||||||
return absolutePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`;
|
|
||||||
},
|
|
||||||
|
|
||||||
toPathForPage({ pages, page, anchor }) {
|
|
||||||
return linker.toPathInContent(getPagePath(pages, page)) + (anchor ? `#${anchor}` : '');
|
|
||||||
},
|
|
||||||
|
|
||||||
toLinkForContent(url: string): string {
|
|
||||||
return url;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return linker;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a prefix to a linker.
|
|
||||||
*/
|
|
||||||
export function appendBasePathToLinker(
|
|
||||||
linker: GitBookSpaceLinker,
|
|
||||||
basePath: string
|
|
||||||
): GitBookSpaceLinker {
|
|
||||||
const linkerWithPrefix: GitBookSpaceLinker = {
|
|
||||||
toPathInContent(relativePath: string): string {
|
|
||||||
return linker.toPathInContent(joinPaths(basePath, relativePath));
|
|
||||||
},
|
|
||||||
|
|
||||||
toAbsoluteURL(absolutePath: string): string {
|
|
||||||
return linker.toAbsoluteURL(absolutePath);
|
|
||||||
},
|
|
||||||
|
|
||||||
toPathForPage({ pages, page, anchor }) {
|
|
||||||
return (
|
|
||||||
linkerWithPrefix.toPathInContent(getPagePath(pages, page)) +
|
|
||||||
(anchor ? `#${anchor}` : '')
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
toLinkForContent(url: string): string {
|
|
||||||
return linker.toLinkForContent(url);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return linkerWithPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
function joinPaths(prefix: string, path: string): string {
|
|
||||||
const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`;
|
|
||||||
const suffixPath = path.startsWith('/') ? path.slice(1) : path;
|
|
||||||
return prefixPath + suffixPath;
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
import { CustomizationThemeMode, type PublishedSiteContent } from '@gitbook/api';
|
|
||||||
import { headers } from 'next/headers';
|
|
||||||
|
|
||||||
export enum MiddlewareHeaders {
|
|
||||||
/**
|
|
||||||
* Type of the route (static or dynamic)
|
|
||||||
*/
|
|
||||||
RouteType = 'x-gitbook-route-type',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The URL of the site (without the pathname)
|
|
||||||
*/
|
|
||||||
SiteURL = 'x-gitbook-site-url',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The data associated with the URL.
|
|
||||||
*/
|
|
||||||
SiteURLData = 'x-gitbook-site-url-data',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The mode of the URL (url or url-host)
|
|
||||||
*/
|
|
||||||
URLMode = 'x-gitbook-url-mode',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The theme of the page (light or dark)
|
|
||||||
*/
|
|
||||||
Theme = 'x-gitbook-theme',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The customization override to apply.
|
|
||||||
*/
|
|
||||||
Customization = 'x-gitbook-customization',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The visitor token used to access this content
|
|
||||||
*/
|
|
||||||
VisitorAuthToken = 'x-gitbook-visitor-token',
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The API token used to fetch the content.
|
|
||||||
* This should only be passed for non-site dynamic routes.
|
|
||||||
*/
|
|
||||||
APIToken = 'x-gitbook-api-token',
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the URL mode from the middleware headers.
|
|
||||||
* This function should only be called in a server action or a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getURLModeFromMiddleware(): Promise<'url' | 'url-host'> {
|
|
||||||
const headersList = await headers();
|
|
||||||
const mode = headersList.get(MiddlewareHeaders.URLMode);
|
|
||||||
if (!mode) {
|
|
||||||
throw new Error('URL mode is not set by the middleware');
|
|
||||||
}
|
|
||||||
|
|
||||||
return mode as 'url' | 'url-host';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the site URL data from the middleware headers.
|
|
||||||
* This function should only be called in a server action or a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getSiteURLDataFromMiddleware(): Promise<PublishedSiteContent> {
|
|
||||||
const headersList = await headers();
|
|
||||||
const siteURLData = headersList.get(MiddlewareHeaders.SiteURLData);
|
|
||||||
|
|
||||||
if (!siteURLData) {
|
|
||||||
throw new Error(
|
|
||||||
'Site URL data is not set by the middleware. This should only be called in a server action or a dynamic route.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return JSON.parse(siteURLData);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the URL from the middleware headers.
|
|
||||||
* This function should only be called in a server action or a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getSiteURLFromMiddleware(): Promise<string> {
|
|
||||||
const headersList = await headers();
|
|
||||||
const siteURL = headersList.get(MiddlewareHeaders.SiteURL);
|
|
||||||
if (!siteURL) {
|
|
||||||
throw new Error('URL mode is not set by the middleware');
|
|
||||||
}
|
|
||||||
|
|
||||||
return siteURL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For preview, the theme can be set via query string (?theme=light).
|
|
||||||
* This function should only be called in a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getThemeFromMiddleware() {
|
|
||||||
const headersList = await headers();
|
|
||||||
const queryStringTheme = headersList.get(MiddlewareHeaders.Theme);
|
|
||||||
if (!queryStringTheme) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return queryStringTheme === 'light'
|
|
||||||
? CustomizationThemeMode.Light
|
|
||||||
: CustomizationThemeMode.Dark;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the visitor auth token from the middleware headers.
|
|
||||||
* This function should only be called in a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getVisitorAuthTokenFromMiddleware(): Promise<string | null> {
|
|
||||||
const headersList = await headers();
|
|
||||||
const visitorAuthToken = headersList.get(MiddlewareHeaders.VisitorAuthToken);
|
|
||||||
if (!visitorAuthToken) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return visitorAuthToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the API token from the middleware headers.
|
|
||||||
* This function should only be called in a dynamic route.
|
|
||||||
*/
|
|
||||||
export async function getAPITokenFromMiddleware(): Promise<string> {
|
|
||||||
const headersList = await headers();
|
|
||||||
const apiToken = headersList.get(MiddlewareHeaders.APIToken);
|
|
||||||
if (!apiToken) {
|
|
||||||
throw new Error('API token is not set by the middleware');
|
|
||||||
}
|
|
||||||
|
|
||||||
return apiToken;
|
|
||||||
}
|
|
||||||
@@ -1,379 +0,0 @@
|
|||||||
import { CustomizationThemeMode } from '@gitbook/api';
|
|
||||||
import type { NextRequest } from 'next/server';
|
|
||||||
import { NextResponse } from 'next/server';
|
|
||||||
|
|
||||||
import { getContentSecurityPolicy } from '@/lib/csp';
|
|
||||||
import { validateSerializedCustomization } from '@/lib/customization';
|
|
||||||
import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
|
|
||||||
import {
|
|
||||||
type ResponseCookies,
|
|
||||||
getPathScopedCookieName,
|
|
||||||
getResponseCookiesForVisitorAuth,
|
|
||||||
getVisitorToken,
|
|
||||||
normalizeVisitorAuthURL,
|
|
||||||
} from '@/lib/visitor-token';
|
|
||||||
import { serveResizedImage } from '@/routes/image';
|
|
||||||
import { getLinkerForSiteURL } from '@v2/lib/context';
|
|
||||||
import {
|
|
||||||
DataFetcherError,
|
|
||||||
getPublishedContentByURL,
|
|
||||||
normalizeURL,
|
|
||||||
throwIfDataError,
|
|
||||||
} from '@v2/lib/data';
|
|
||||||
import { isGitBookAssetsHostURL, isGitBookHostURL } from '@v2/lib/env';
|
|
||||||
import { MiddlewareHeaders } from '@v2/lib/middleware';
|
|
||||||
|
|
||||||
export const config = {
|
|
||||||
matcher: [
|
|
||||||
'/((?!_next/static|_next/image|~gitbook/static|~gitbook/revalidate|~gitbook/monitoring|~scalar/proxy).*)',
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
|
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const requestURL = new URL(request.url);
|
|
||||||
|
|
||||||
// Redirect to normalize the URL
|
|
||||||
const normalized = normalizeURL(requestURL);
|
|
||||||
if (normalized.toString() !== requestURL.toString()) {
|
|
||||||
return NextResponse.redirect(normalized.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const handler of [serveSiteRoutes, servePreviewRoutes]) {
|
|
||||||
const result = await handler(requestURL, request);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.next();
|
|
||||||
} catch (error) {
|
|
||||||
return serveErrorResponse(error as Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle request that are targetting the site routes group.
|
|
||||||
*/
|
|
||||||
async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
|
|
||||||
const match = getSiteURLFromRequest(request);
|
|
||||||
if (!match) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { url: siteURL, mode } = match;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve image resizing requests (all requests containing `/~gitbook/image`).
|
|
||||||
* All URLs containing `/~gitbook/image` are rewritten to `/~gitbook/image`
|
|
||||||
* and serve from a single route handler.
|
|
||||||
*
|
|
||||||
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
|
|
||||||
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
|
|
||||||
*/
|
|
||||||
if (siteURL.pathname.endsWith('/~gitbook/image')) {
|
|
||||||
return await serveResizedImage(request, {
|
|
||||||
host: siteURL.host,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Detect and extract the visitor authentication token from the request
|
|
||||||
//
|
|
||||||
// @ts-ignore - request typing
|
|
||||||
const visitorToken = getVisitorToken(request, siteURL);
|
|
||||||
|
|
||||||
const data = await throwIfDataError(
|
|
||||||
getPublishedContentByURL({
|
|
||||||
url: siteURL.toString(),
|
|
||||||
visitorAuthToken: visitorToken?.token ?? null,
|
|
||||||
// When the visitor auth token is pulled from the cookie, set redirectOnError when calling getPublishedContentByUrl to allow
|
|
||||||
// redirecting when the token is invalid as we could be dealing with stale token stored in the cookie.
|
|
||||||
// For example when the VA backend signature has changed but the token stored in the cookie is not yet expired.
|
|
||||||
redirectOnError: visitorToken?.source === 'visitor-auth-cookie',
|
|
||||||
})
|
|
||||||
);
|
|
||||||
let cookies: ResponseCookies = {};
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle redirects
|
|
||||||
//
|
|
||||||
if ('redirect' in data) {
|
|
||||||
// biome-ignore lint/suspicious/noConsole: we want to log the redirect
|
|
||||||
console.log('redirect', data.redirect);
|
|
||||||
if (data.target === 'content') {
|
|
||||||
// For content redirects, we use the linker to redirect the optimal URL
|
|
||||||
// during development and testing in 'url' mode.
|
|
||||||
const linker = getLinkerForSiteURL({
|
|
||||||
siteURL,
|
|
||||||
urlMode: mode,
|
|
||||||
});
|
|
||||||
|
|
||||||
const contentRedirect = new URL(linker.toLinkForContent(data.redirect), request.url);
|
|
||||||
|
|
||||||
// Keep the same search params as the original request
|
|
||||||
// as it might contain a VA token
|
|
||||||
contentRedirect.search = request.nextUrl.search;
|
|
||||||
|
|
||||||
return NextResponse.redirect(contentRedirect);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.redirect(data.redirect);
|
|
||||||
}
|
|
||||||
|
|
||||||
cookies = {
|
|
||||||
...cookies,
|
|
||||||
...getResponseCookiesForVisitorAuth(data.basePath, visitorToken),
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// Make sure the URL is clean of any va token after a successful lookup
|
|
||||||
// The token is stored in a cookie that is set on the redirect response
|
|
||||||
//
|
|
||||||
const requestURLWithoutToken = normalizeVisitorAuthURL(requestURL);
|
|
||||||
if (requestURLWithoutToken.toString() !== requestURL.toString()) {
|
|
||||||
return writeResponseCookies(
|
|
||||||
NextResponse.redirect(requestURLWithoutToken.toString()),
|
|
||||||
cookies
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Render and serve the content
|
|
||||||
//
|
|
||||||
|
|
||||||
// When visitor has authentication (adaptive content or VA), we serve dynamic routes.
|
|
||||||
let routeType = visitorToken ? 'dynamic' : 'static';
|
|
||||||
|
|
||||||
const requestHeaders = new Headers(request.headers);
|
|
||||||
requestHeaders.set(MiddlewareHeaders.RouteType, routeType);
|
|
||||||
requestHeaders.set(MiddlewareHeaders.URLMode, mode);
|
|
||||||
requestHeaders.set(MiddlewareHeaders.SiteURL, `${siteURL.origin}${data.basePath}`);
|
|
||||||
requestHeaders.set(MiddlewareHeaders.SiteURLData, JSON.stringify(data));
|
|
||||||
|
|
||||||
// Preview of customization/theme
|
|
||||||
const customization = siteURL.searchParams.get('customization');
|
|
||||||
if (customization && validateSerializedCustomization(customization)) {
|
|
||||||
routeType = 'dynamic';
|
|
||||||
requestHeaders.set(MiddlewareHeaders.Customization, customization);
|
|
||||||
}
|
|
||||||
const theme = siteURL.searchParams.get('theme');
|
|
||||||
if (theme === CustomizationThemeMode.Dark || theme === CustomizationThemeMode.Light) {
|
|
||||||
routeType = 'dynamic';
|
|
||||||
requestHeaders.set(MiddlewareHeaders.Theme, theme);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We support forcing dynamic routes by setting a `gitbook-dynamic-route` cookie
|
|
||||||
// This is useful for testing dynamic routes.
|
|
||||||
if (request.cookies.has('gitbook-dynamic-route')) {
|
|
||||||
routeType = 'dynamic';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass a x-forwarded-host and origin that are equal to ensure Next doesn't block server actions when proxied
|
|
||||||
requestHeaders.set('x-forwarded-host', request.nextUrl.host);
|
|
||||||
requestHeaders.set('origin', request.nextUrl.origin);
|
|
||||||
|
|
||||||
const siteURLWithoutProtocol = `${siteURL.host}${data.basePath}`;
|
|
||||||
const { pathname, routeType: routeTypeFromPathname } = encodePathInSiteContent(data.pathname);
|
|
||||||
routeType = routeTypeFromPathname ?? routeType;
|
|
||||||
|
|
||||||
const route = [
|
|
||||||
'sites',
|
|
||||||
routeType,
|
|
||||||
mode,
|
|
||||||
encodeURIComponent(siteURLWithoutProtocol),
|
|
||||||
pathname,
|
|
||||||
].join('/');
|
|
||||||
|
|
||||||
console.log(`rewriting ${request.nextUrl.toString()} to ${route}`);
|
|
||||||
|
|
||||||
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
|
|
||||||
const response = NextResponse.rewrite(rewrittenURL, {
|
|
||||||
request: {
|
|
||||||
headers: requestHeaders,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add Content Security Policy header
|
|
||||||
response.headers.set('content-security-policy', getContentSecurityPolicy());
|
|
||||||
// Basic security headers
|
|
||||||
response.headers.set('strict-transport-security', 'max-age=31536000');
|
|
||||||
response.headers.set('referrer-policy', 'no-referrer-when-downgrade');
|
|
||||||
response.headers.set('x-content-type-options', 'nosniff');
|
|
||||||
// Debug header
|
|
||||||
response.headers.set('x-gitbook-route-type', routeType);
|
|
||||||
response.headers.set('x-gitbook-route-site', siteURLWithoutProtocol);
|
|
||||||
|
|
||||||
return writeResponseCookies(response, cookies);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve routes for previewing unpublished content.
|
|
||||||
* Routes are:
|
|
||||||
* - PDF export for a space: /~space/:spaceId/~gitbook/pdf
|
|
||||||
* - Preview of an unpublished site: /~site/:siteId/
|
|
||||||
*/
|
|
||||||
async function servePreviewRoutes(requestURL: URL, request: NextRequest) {
|
|
||||||
const pathnameParts = requestURL.pathname.slice(1).split('/');
|
|
||||||
|
|
||||||
if (pathnameParts[0] !== '~space' && pathnameParts[0] !== '~site') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We store the API token in a cookie that is scoped to the specific preview route
|
|
||||||
// to avoid errors when multiple previews are opened in different tabs.
|
|
||||||
const cookieName = getPathScopedCookieName(
|
|
||||||
'gitbook-api-token',
|
|
||||||
pathnameParts.slice(0, 2).join('/')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Extract a potential GitBook API token passed in the request
|
|
||||||
// If found, we redirect to the same URL but with the token in the cookie
|
|
||||||
const queryAPIToken = requestURL.searchParams.get('token');
|
|
||||||
if (queryAPIToken) {
|
|
||||||
requestURL.searchParams.delete('token');
|
|
||||||
return writeResponseCookies(NextResponse.redirect(requestURL.toString()), {
|
|
||||||
[cookieName]: {
|
|
||||||
value: queryAPIToken,
|
|
||||||
options: {
|
|
||||||
httpOnly: true,
|
|
||||||
sameSite: process.env.NODE_ENV === 'production' ? 'none' : undefined,
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
maxAge: 60 * 60, // 1 hour
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const apiToken = request.cookies.get(cookieName)?.value;
|
|
||||||
if (!apiToken) {
|
|
||||||
throw new DataFetcherError('Missing API token', 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the rest with the router default logic
|
|
||||||
return NextResponse.next({
|
|
||||||
headers: {
|
|
||||||
[MiddlewareHeaders.APIToken]: apiToken,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serve an error response.
|
|
||||||
*/
|
|
||||||
function serveErrorResponse(error: Error) {
|
|
||||||
if (error instanceof DataFetcherError) {
|
|
||||||
return new Response(error.message, {
|
|
||||||
status: error.code,
|
|
||||||
headers: { 'content-type': 'text/plain' },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The URL of the GitBook content can be passed in 3 different ways (in order of priority):
|
|
||||||
* - The request has a `X-GitBook-URL` header:
|
|
||||||
* URL is taken from the header.
|
|
||||||
* - The request has a `X-Forwarded-Host` header:
|
|
||||||
* Host is taken from the header, pathname is taken from the request URL.
|
|
||||||
* - The request URL is matching `/url/:url`:
|
|
||||||
* URL is taken from the pathname.
|
|
||||||
*/
|
|
||||||
function getSiteURLFromRequest(request: NextRequest): URLWithMode | null {
|
|
||||||
const xGitbookUrl = request.headers.get('x-gitbook-url');
|
|
||||||
if (xGitbookUrl) {
|
|
||||||
return {
|
|
||||||
url: appendQueryParams(new URL(xGitbookUrl), request.nextUrl.searchParams),
|
|
||||||
mode: 'url-host',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const isMainHost = isGitBookHostURL(request.url);
|
|
||||||
const isAssetsHost = isGitBookAssetsHostURL(request.url);
|
|
||||||
|
|
||||||
// /url/:url requests on the main host
|
|
||||||
const prefix = '/url/';
|
|
||||||
if (isMainHost && request.nextUrl.pathname.startsWith(prefix)) {
|
|
||||||
return {
|
|
||||||
url: appendQueryParams(
|
|
||||||
new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
|
|
||||||
request.nextUrl.searchParams
|
|
||||||
),
|
|
||||||
mode: 'url',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip other requests to main hosts
|
|
||||||
if (isMainHost || isAssetsHost) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const xForwardedHost = request.headers.get('x-forwarded-host');
|
|
||||||
// The x-forwarded-host is set by Vercel for all requests
|
|
||||||
// so we ignore it if the hostname is the same as the instance one.
|
|
||||||
if (xForwardedHost) {
|
|
||||||
return {
|
|
||||||
url: appendQueryParams(
|
|
||||||
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
|
|
||||||
request.nextUrl.searchParams
|
|
||||||
),
|
|
||||||
mode: 'url-host',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode path in a site content.
|
|
||||||
* Special paths are not encoded and passed to be handled by the route handlers.
|
|
||||||
*/
|
|
||||||
function encodePathInSiteContent(rawPathname: string): {
|
|
||||||
pathname: string;
|
|
||||||
routeType?: 'static' | 'dynamic';
|
|
||||||
} {
|
|
||||||
const pathname = removeLeadingSlash(removeTrailingSlash(rawPathname));
|
|
||||||
|
|
||||||
if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) {
|
|
||||||
return { pathname };
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (pathname) {
|
|
||||||
case '~gitbook/icon':
|
|
||||||
case 'llms.txt':
|
|
||||||
case 'sitemap.xml':
|
|
||||||
case 'robots.txt':
|
|
||||||
return { pathname };
|
|
||||||
case '~gitbook/pdf':
|
|
||||||
// PDF routes are always dynamic as they depend on the search params.
|
|
||||||
return { pathname, routeType: 'dynamic' };
|
|
||||||
default:
|
|
||||||
return { pathname: encodeURIComponent(pathname || '/') };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append all the query params from a URL to another URL.
|
|
||||||
*/
|
|
||||||
function appendQueryParams(url: URL, from: URLSearchParams) {
|
|
||||||
for (const [key, value] of from.entries()) {
|
|
||||||
url.searchParams.set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the cookies to a response.
|
|
||||||
*/
|
|
||||||
function writeResponseCookies<R extends NextResponse>(response: R, cookies: ResponseCookies): R {
|
|
||||||
Object.entries(cookies).forEach(([key, { value, options }]) => {
|
|
||||||
response.cookies.set(key, value, options);
|
|
||||||
});
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import config from '../gitbook/tailwind.config';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
...config,
|
|
||||||
content: [
|
|
||||||
'../gitbook/src/pages/**/*.{js,ts,jsx,tsx,mdx}',
|
|
||||||
'../gitbook/src/components/**/*.{js,ts,jsx,tsx,mdx}',
|
|
||||||
'../gitbook/src/app/**/*.{js,ts,jsx,tsx,mdx}',
|
|
||||||
],
|
|
||||||
};
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "esnext",
|
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
|
||||||
"allowJs": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"strict": true,
|
|
||||||
"noEmit": true,
|
|
||||||
"incremental": true,
|
|
||||||
"module": "esnext",
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"resolveJsonModule": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"jsx": "preserve",
|
|
||||||
"plugins": [
|
|
||||||
{
|
|
||||||
"name": "next"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"paths": {
|
|
||||||
"@v2/*": ["./src/*"],
|
|
||||||
"@/*": ["../gitbook/src/*"]
|
|
||||||
},
|
|
||||||
"types": [
|
|
||||||
"bun-types" // add Bun global
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
"next-env.d.ts",
|
|
||||||
".next/types/**/*.ts",
|
|
||||||
"**/*.ts",
|
|
||||||
"**/*.tsx",
|
|
||||||
"../gitbook/types/**/*.d.ts",
|
|
||||||
"../gitbook/cf-env.d.ts"
|
|
||||||
],
|
|
||||||
"exclude": [
|
|
||||||
"node_modules",
|
|
||||||
"packages/openapi-parser",
|
|
||||||
"packages/react-openapi",
|
|
||||||
"packages/react-math",
|
|
||||||
"packages/gitbook"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["//"],
|
|
||||||
"tasks": {
|
|
||||||
"generate": {
|
|
||||||
"outputs": ["public"]
|
|
||||||
},
|
|
||||||
"typecheck": {
|
|
||||||
"dependsOn": ["^typecheck"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
main = ".open-next/worker.js"
|
|
||||||
name = "gitbook-open-v2"
|
|
||||||
compatibility_date = "2024-09-23"
|
|
||||||
compatibility_flags = ["nodejs_compat"]
|
|
||||||
assets = { directory = ".open-next/assets", binding = "ASSETS" }
|
|
||||||
observability = { enabled = true }
|
|
||||||
|
|
||||||
[env.preview]
|
|
||||||
kv_namespaces = [
|
|
||||||
{ binding = "NEXT_CACHE_WORKERS_KV", id = "b7dd9cf58bf2458f84812a2d83b3760c" } # gitbook-open-v2-cache-preview
|
|
||||||
]
|
|
||||||
d1_databases = [
|
|
||||||
{ binding = "NEXT_CACHE_D1", database_id = "f59ddb40-ad72-4312-9395-0ac6a129af8e", database_name = "gitbook-open-v2-tags-preview" }
|
|
||||||
]
|
|
||||||
|
|
||||||
[env.staging]
|
|
||||||
routes = [
|
|
||||||
{ pattern = "open-2c.gitbook-staging.com/*", zone_name = "gitbook-staging.com" },
|
|
||||||
{ pattern = "static-2c.gitbook-staging.com/*", zone_name = "gitbook-staging.com" }
|
|
||||||
]
|
|
||||||
kv_namespaces = [
|
|
||||||
{ binding = "NEXT_CACHE_WORKERS_KV", id = "a446e25f12b741afb185f1e5b4474f0a" } # gitbook-open-v2-cache-staging
|
|
||||||
]
|
|
||||||
d1_databases = [
|
|
||||||
{ binding = "NEXT_CACHE_D1", database_id = "9df62e39-1f35-4066-83aa-e9b8ed3ac8d5", database_name = "gitbook-open-v2-tags-staging" }
|
|
||||||
]
|
|
||||||
|
|
||||||
[env.production]
|
|
||||||
routes = [
|
|
||||||
{ pattern = "open-2c.gitbook.com/*", zone_name = "gitbook.com" },
|
|
||||||
{ pattern = "static-2c.gitbook.com/*", zone_name = "gitbook.com" }
|
|
||||||
]
|
|
||||||
kv_namespaces = [
|
|
||||||
{ binding = "NEXT_CACHE_WORKERS_KV", id = "72379746280d4e79acf24440eea950dc" } # gitbook-open-v2-cache-production
|
|
||||||
]
|
|
||||||
d1_databases = [
|
|
||||||
{ binding = "NEXT_CACHE_D1", database_id = "a6f16fce-5f45-43a9-89a4-7b83ddf25b77", database_name = "gitbook-open-v2-tags-production" }
|
|
||||||
]
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# Configurations
|
|
||||||
|
|
||||||
### API ###
|
|
||||||
|
|
||||||
## API token to use when looking up the published content
|
|
||||||
# GITBOOK_API_URL=https://api.gitbook.com
|
|
||||||
# GITBOOK_TOKEN=xxx
|
|
||||||
|
|
||||||
## User agent to use when making requests to the API
|
|
||||||
# GITBOOK_USER_AGENT=GitBook/1.0.0
|
|
||||||
# GITBOOK_USER_AGENT_COMMENT=something
|
|
||||||
|
|
||||||
### URL of the application ###
|
|
||||||
# NEXT_PUBLIC_GITBOOK_APP_URL=https://app.gitbook.com
|
|
||||||
|
|
||||||
### Image resizing ###
|
|
||||||
# GITBOOK_IMAGE_RESIZE_SIGNING_KEY=1234567890
|
|
||||||
# GITBOOK_IMAGE_RESIZE_URL=https://mycompany.com/cdn-cgi/image/
|
|
||||||
|
|
||||||
### SEO ###
|
|
||||||
# GITBOOK_BLOCK_SEARCH_INDEXATION=true
|
|
||||||
|
|
||||||
## Caching
|
|
||||||
# GITBOOK_OUTPUT_CACHE=true
|
|
||||||
|
|
||||||
### Sentry ###
|
|
||||||
# SENTRY_DSN=xxx
|
|
||||||
|
|
||||||
### Silent logs
|
|
||||||
# SILENT=true
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
# testing
|
# testing
|
||||||
@@ -23,9 +24,6 @@ next-env.d.ts
|
|||||||
# visual tests
|
# visual tests
|
||||||
screenshots/
|
screenshots/
|
||||||
|
|
||||||
# Sentry Config File
|
|
||||||
.sentryclirc
|
|
||||||
|
|
||||||
/test-results/
|
/test-results/
|
||||||
/playwright-report/
|
/playwright-report/
|
||||||
/blob-report/
|
/blob-report/
|
||||||
@@ -34,3 +32,8 @@ screenshots/
|
|||||||
# Generated public files
|
# Generated public files
|
||||||
/public/~gitbook/static/*
|
/public/~gitbook/static/*
|
||||||
!/public/~gitbook/static/images
|
!/public/~gitbook/static/images
|
||||||
|
|
||||||
|
# cloudflare
|
||||||
|
.open-next
|
||||||
|
.wrangler
|
||||||
|
worker-configuration.d.ts
|
||||||
+771
-356
File diff suppressed because it is too large
Load Diff
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"version": 1,
|
|
||||||
"exclude": ["/~gitbook/static/*"]
|
|
||||||
}
|
|
||||||
Vendored
-7
@@ -1,7 +0,0 @@
|
|||||||
import type { CacheObject } from '@gitbook/cache-do';
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
interface CloudflareEnv {
|
|
||||||
CACHE?: DurableObjectNamespace<CacheObject>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user