mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
Compare commits
359 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 50cafe9ec6 | |||
| 2f05549c72 | |||
| 2da0ccf4ba | |||
| 9e062b10d3 | |||
| 74f89316d9 | |||
| 9e1d7b296b | |||
| b49caafa9c | |||
| f372eec393 | |||
| 65ae941687 | |||
| 75b8efa155 | |||
| 34b0c67c91 | |||
| e0ace99707 | |||
| 3553db0989 | |||
| 5c14948cf3 | |||
| 8e4e2a2333 | |||
| 0f4c53cfbd | |||
| c156e946b0 | |||
| 461e15fd00 | |||
| 2989f79708 | |||
| 344842f5ce | |||
| c51076efc4 | |||
| 38e34dcce3 | |||
| 04a4175678 | |||
| 3e40b4db50 | |||
| 854e612e5d | |||
| 27a461be9d | |||
| 3d83dadb12 | |||
| 8dc5c18b9f | |||
| e4756c23a7 | |||
| 79ad7bcb59 | |||
| 5b0917a13a | |||
| 6d6f4ef6dd | |||
| fa69a64a2b | |||
| d1a85d1f91 | |||
| 05bf9d7a59 | |||
| 2e0b3370c8 | |||
| 10995e09ca | |||
| e7cc3ff61f | |||
| 59d259074b | |||
| 96c4ae4f2d | |||
| 64f3596df2 | |||
| 27d6c3af39 | |||
| 1de4eb7973 | |||
| df1966df25 | |||
| 1001aaf94e | |||
| 155e448c8f | |||
| ca2a94ee1a | |||
| a512c90bda | |||
| 9cbefa89c7 | |||
| c8bba7bfe1 | |||
| 4e04db8a1b | |||
| b45feafeff | |||
| 3876f7081e | |||
| b4a021a192 | |||
| 1d92dc1350 | |||
| e4ba35fdae | |||
| 35dcaca971 | |||
| 2aa4f2e0b8 | |||
| 6d56df7c10 | |||
| 4534ea83c3 | |||
| 9022d87ec7 | |||
| a49781e200 | |||
| 065a5e6e55 | |||
| e27986b17a | |||
| 8606f222c9 | |||
| 2c3066e3ae | |||
| 55c0b03a41 | |||
| 3e548e4186 | |||
| 3aea302648 | |||
| 1d1f0a7c6a | |||
| 590729cce5 | |||
| b554d7d13f | |||
| 9568b8a7c8 | |||
| 683bb8f634 | |||
| 7d6a31627f | |||
| c31e5648f5 | |||
| 0c01832684 | |||
| 6b1c9fef86 | |||
| cc976995cc | |||
| ebbb476589 | |||
| 89655cb428 | |||
| 3676d64bdc | |||
| f682df1e01 | |||
| 6c9893945d | |||
| a2fb627ae1 | |||
| f9f80117f0 | |||
| 63bf4dbb16 | |||
| b3fa503aff | |||
| 0ae7cfd790 | |||
| 8fdc5c89dc | |||
| 758b34d46b | |||
| 1b0613e482 | |||
| 3164f1a99d | |||
| 6b588af07d | |||
| d436b04ff4 | |||
| d159788d0b | |||
| 3355960d95 | |||
| c32d4494f2 | |||
| 1dcf4df51f | |||
| 3c0e49d56b | |||
| c35d0e8954 | |||
| c0d73d2f76 | |||
| 454175ab86 | |||
| 6feb8fcd6a | |||
| b8d3cb7ee5 | |||
| 8ce73229c3 | |||
| 8eae6e970b | |||
| a48b886dfd | |||
| 17de0838c5 | |||
| baf3496980 | |||
| ae33c09aa7 | |||
| 7ff69c3b72 | |||
| 6142d6bf80 | |||
| 75543b140f | |||
| 511f1cfbe5 | |||
| 46fa9d954c | |||
| 5e1ed5358e | |||
| 295f03db49 | |||
| bdde3929fc | |||
| 4f4210d24a | |||
| c038ec8f81 | |||
| 75b52eac28 | |||
| ad4d07dc06 | |||
| 75086740f8 | |||
| eea8f1e00f | |||
| 4f9abfb6f2 | |||
| 252afceab0 | |||
| bef191660a | |||
| 145625142d | |||
| f70a2aa310 | |||
| 56e46ce8ab | |||
| 754cc11e0b | |||
| 3551d147e9 | |||
| 162cfe4163 | |||
| 6815dd197e | |||
| d74f8a7098 | |||
| 1e53f46521 | |||
| 1a6216df4f | |||
| a629900324 | |||
| a4c33997fe | |||
| badf3a92f6 | |||
| e0fb9ac3ca | |||
| f3e40410c4 | |||
| 8e99871004 | |||
| 1e4e54e752 | |||
| 50c1be3447 | |||
| 319a1e536c | |||
| 50cb0c6e15 | |||
| ceb32b1a2e | |||
| f8c54b4d34 | |||
| c06ad70f8a | |||
| 0ef5dc8538 | |||
| 28adc9b5f1 | |||
| 912d1693f5 | |||
| c23d4ef331 | |||
| c21693fe50 | |||
| c06b3dd60d | |||
| f173e014db | |||
| 1b458c436e | |||
| b42b0b3910 | |||
| 2fc21272ff | |||
| 379d486488 | |||
| cba583a31c | |||
| 7a504b7cc5 | |||
| 8ec5fd0986 | |||
| 529f940a45 | |||
| 8a3c15911f | |||
| d7948e34b2 | |||
| 9f42211993 | |||
| 65e62307bf | |||
| 469b332da2 | |||
| e434442f1e | |||
| 9344431bfc | |||
| 622ecbec1d | |||
| fa1382b289 | |||
| b8388e5a2a | |||
| 2bce12798f | |||
| f89e889d18 | |||
| 2b8a2d24b6 | |||
| b85eccdff5 | |||
| d924e655c0 | |||
| b932e4e2e9 | |||
| ca4a3e8c9a | |||
| b498521196 | |||
| 8a8cf2025d | |||
| ed7d47d9b8 | |||
| b5494cc04b | |||
| 229f2ba9d5 | |||
| 6da36558f6 | |||
| ca71c1fad6 | |||
| 262afa357b | |||
| 7375d3c597 | |||
| 3548fa6dff | |||
| cb73040e0f | |||
| 872d36b64f | |||
| e1ff17e655 | |||
| f3010bd28a | |||
| 8ff1e3b619 | |||
| d7596bf454 | |||
| aea5eb10ae | |||
| 1165a81cf5 | |||
| 61d1a0192e | |||
| f9a2977621 | |||
| 24f601d594 | |||
| 7b34537bbe | |||
| 145f385fdb | |||
| 360aa1c6d6 | |||
| ccc0975240 | |||
| 791135040a | |||
| 12c9d765ff | |||
| 45207288a1 | |||
| 177afa5828 | |||
| d51b79edbb | |||
| a2720ac49d | |||
| 5f6853d812 | |||
| 17dd382dc3 | |||
| 98e42cfe79 | |||
| c890e01004 | |||
| 659be551bb | |||
| 78a632b7fe | |||
| 4927e964b7 | |||
| 262a9b198b | |||
| 4f3588240c | |||
| abbae3ec4d | |||
| 1b8609ac60 | |||
| 193d591e9f | |||
| 1edc5d622a | |||
| 61b8507996 | |||
| 6f368b5cf3 | |||
| 9201e2cf52 | |||
| 2e0d706d43 | |||
| 7fefe4919c | |||
| 729921f338 | |||
| 1839ea2678 | |||
| f08dd29364 | |||
| ca2dfa3065 | |||
| 10ded437f0 | |||
| d1fdc13441 | |||
| a8fca0e033 | |||
| 9251b33959 | |||
| 81a6bd756a | |||
| ada195d329 | |||
| 1211ebea9b | |||
| 2e6e28eb54 | |||
| 854c448bad | |||
| 64a0b0f169 | |||
| 13ff22b6a8 | |||
| 8927e8fbbb | |||
| 25e2b40d47 | |||
| a60af8ff9c | |||
| 27b328cfd7 | |||
| fa6ef0c01c | |||
| 6217a2ed3b | |||
| ffa866cb99 | |||
| 011aa931cf | |||
| 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 |
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@gitbook/openapi-parser': patch
|
||||
'@gitbook/react-openapi': patch
|
||||
---
|
||||
|
||||
Add x-gitbook-prefix and x-gitbook-token-placeholder for OpenAPI security scheme
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@gitbook/react-openapi': patch
|
||||
---
|
||||
|
||||
Fix OpenAPI basic auth placeholder
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Fix links to other spaces and root page in embeddable view.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": minor
|
||||
---
|
||||
|
||||
Add a RSS feed to all pages with update blocks.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'gitbook': patch
|
||||
---
|
||||
|
||||
Remove hidden site spaces from navigation
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'gitbook': patch
|
||||
---
|
||||
|
||||
Fix hidden section not found
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@gitbook/react-openapi': patch
|
||||
'gitbook': patch
|
||||
---
|
||||
|
||||
Improve OpenAPI circular references
|
||||
@@ -54,10 +54,9 @@ After forking this repository, you'll want to [create a branch](https://docs.git
|
||||
#### 3. Install dependencies and run the project locally
|
||||
|
||||
##### 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
|
||||
|
||||
- [Node.js](https://nodejs.org/en) (see "engines" in `package.json`)
|
||||
- [Bun](https://bun.sh/) (see "packageManager" in `package.json`)
|
||||
|
||||
##### Setup steps:
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: deployments status --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||
|
||||
@@ -49,7 +49,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.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
|
||||
|
||||
@@ -61,7 +61,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: versions deploy ${{ inputs.middlewareVersionId }}@100% -y --config ./packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc
|
||||
|
||||
@@ -71,7 +71,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: versions deploy ${{ inputs.serverVersionId }}@100% -y --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
name: 'Setup Playwright'
|
||||
description: 'Install Playwright and dependencies'
|
||||
runs:
|
||||
using: 'composite'
|
||||
steps:
|
||||
# Run npm ci and get Playwright version
|
||||
- name: 🏗 Prepare Playwright env
|
||||
shell: bash
|
||||
run: |
|
||||
PLAYWRIGHT_VERSION=$(npm ls --json @playwright/test | jq --raw-output '.dependencies["gitbook"].dependencies["@playwright/test"].version')
|
||||
echo "PLAYWRIGHT_VERSION=$PLAYWRIGHT_VERSION" >> $GITHUB_ENV
|
||||
|
||||
# Cache browser binaries, cache key is based on Playwright version and OS
|
||||
- name: 🧰 Cache Playwright browser binaries
|
||||
uses: actions/cache@v4
|
||||
id: playwright-cache
|
||||
with:
|
||||
path: '~/.cache/ms-playwright'
|
||||
key: '${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}'
|
||||
restore-keys: |
|
||||
${{ runner.os }}-playwright-
|
||||
|
||||
# Install browser binaries & OS dependencies if cache missed
|
||||
- name: 🏗 Install Playwright browser binaries & OS dependencies
|
||||
if: steps.playwright-cache.outputs.cache-hit != 'true'
|
||||
shell: bash
|
||||
working-directory: packages/gitbook
|
||||
run: |
|
||||
bun x playwright install --with-deps chromium
|
||||
|
||||
# Install only the OS dependencies if cache hit
|
||||
- name: 🏗 Install Playwright OS dependencies
|
||||
if: steps.playwright-cache.outputs.cache-hit == 'true'
|
||||
shell: bash
|
||||
working-directory: packages/gitbook
|
||||
run: |
|
||||
bun x playwright install-deps
|
||||
@@ -70,7 +70,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: deploy --config ./packages/gitbook/openNext/customWorkers/doWrangler.jsonc
|
||||
|
||||
@@ -81,7 +81,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc
|
||||
|
||||
@@ -104,7 +104,7 @@ runs:
|
||||
apiToken: ${{ inputs.apiToken }}
|
||||
accountId: ${{ inputs.accountId }}
|
||||
workingDirectory: ./
|
||||
wranglerVersion: '4.10.0'
|
||||
wranglerVersion: '4.43.0'
|
||||
environment: ${{ inputs.environment }}
|
||||
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook/openNext/customWorkers/middlewareWrangler.jsonc
|
||||
|
||||
|
||||
@@ -4,8 +4,9 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- changeset-release/main
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
BUN_NPM_TOKEN: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
jobs:
|
||||
format:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -41,7 +42,7 @@ jobs:
|
||||
name: Build (Open Source)
|
||||
timeout-minutes: 6
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ''
|
||||
BUN_NPM_TOKEN: ''
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
@@ -5,7 +5,7 @@ on:
|
||||
branches:
|
||||
- main
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
BUN_NPM_TOKEN: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (preview)
|
||||
@@ -102,7 +102,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Visual Testing v2
|
||||
needs: deploy-v2-vercel
|
||||
timeout-minutes: 10
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@@ -110,8 +110,8 @@ jobs:
|
||||
uses: ./.github/composite/setup-bun
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile
|
||||
- name: Setup Playwright
|
||||
uses: ./.github/actions/setup-playwright
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Run Playwright tests
|
||||
run: bun e2e
|
||||
env:
|
||||
@@ -123,7 +123,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Visual Testing v2 (Cloudflare)
|
||||
needs: deploy-v2-cloudflare
|
||||
timeout-minutes: 10
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@@ -131,8 +131,8 @@ jobs:
|
||||
uses: ./.github/composite/setup-bun
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile
|
||||
- name: Setup Playwright
|
||||
uses: ./.github/actions/setup-playwright
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Run Playwright tests
|
||||
run: bun e2e
|
||||
env:
|
||||
@@ -144,7 +144,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Visual Testing Customers v2
|
||||
needs: deploy-v2-vercel
|
||||
timeout-minutes: 10
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@@ -152,8 +152,8 @@ jobs:
|
||||
uses: ./.github/composite/setup-bun
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile
|
||||
- name: Setup Playwright
|
||||
uses: ./.github/actions/setup-playwright
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Run Playwright tests
|
||||
run: bun e2e-customers
|
||||
env:
|
||||
@@ -165,7 +165,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Visual Testing Customers v2 (Cloudflare)
|
||||
needs: deploy-v2-cloudflare
|
||||
timeout-minutes: 10
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@@ -173,8 +173,8 @@ jobs:
|
||||
uses: ./.github/composite/setup-bun
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile
|
||||
- name: Setup Playwright
|
||||
uses: ./.github/actions/setup-playwright
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Run Playwright tests
|
||||
run: bun e2e-customers
|
||||
env:
|
||||
@@ -182,9 +182,9 @@ jobs:
|
||||
SITE_BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/
|
||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||
ARGOS_BUILD_NAME: 'customers-v2'
|
||||
pagespeed-testing-v2:
|
||||
browserless-testing-v2-vercel:
|
||||
runs-on: ubuntu-latest
|
||||
name: PageSpeed Testing v1
|
||||
name: Browserless Testing v2 (Vercel)
|
||||
needs: deploy-v2-vercel
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -195,8 +195,26 @@ jobs:
|
||||
run: bun install --frozen-lockfile
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Run pagespeed tests
|
||||
run: bun ./packages/gitbook/tests/pagespeed-testing.ts
|
||||
- name: Run tests
|
||||
run: cd ./packages/gitbook && bun e2e-browserless
|
||||
env:
|
||||
BASE_URL: ${{needs.deploy-v2-vercel.outputs.deployment-url}}
|
||||
PAGESPEED_API_KEY: ${{ secrets.PAGESPEED_API_KEY }}
|
||||
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
||||
# browserless-testing-v2-cloudflare:
|
||||
# runs-on: ubuntu-latest
|
||||
# name: Browserless Testing v2 (Cloudflare)
|
||||
# needs: deploy-v2-cloudflare
|
||||
# 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: Run tests
|
||||
# run: cd ./packages/gitbook && bun e2e-browserless
|
||||
# env:
|
||||
# BASE_URL: ${{needs.deploy-v2-cloudflare.outputs.deployment-url}}
|
||||
# SITE_BASE_URL: ${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}/url/
|
||||
@@ -4,7 +4,7 @@ on:
|
||||
branches:
|
||||
- main
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
BUN_NPM_TOKEN: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (production)
|
||||
|
||||
@@ -4,7 +4,7 @@ on:
|
||||
branches:
|
||||
- main
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
BUN_NPM_TOKEN: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
jobs:
|
||||
deploy-v2-vercel:
|
||||
name: Deploy v2 to Vercel (staging)
|
||||
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
- main
|
||||
|
||||
env:
|
||||
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
BUN_NPM_TOKEN: ${{ secrets.NPM_TOKEN_READONLY }}
|
||||
|
||||
concurrency: ${{ github.workflow }}-${{ github.ref }}
|
||||
|
||||
@@ -14,29 +14,38 @@ jobs:
|
||||
publish:
|
||||
name: Publish
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
id-token: write
|
||||
pull-requests: write
|
||||
contents: write
|
||||
steps:
|
||||
- name: Checkout Repo
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
|
||||
fetch-depth: 0
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
# Ensure npm 11.5.1 or later is installed
|
||||
- name: Update npm
|
||||
run: npm install -g npm@latest
|
||||
- name: Setup Bun
|
||||
uses: ./.github/composite/setup-bun
|
||||
- name: Install dependencies
|
||||
run: bun install --frozen-lockfile
|
||||
env:
|
||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||
- name: Create Release Pull Request or Publish to npm
|
||||
- name: Create Release Pull Request
|
||||
id: changesets
|
||||
uses: changesets/action@v1
|
||||
with:
|
||||
publish: npm run release
|
||||
version: npm run changeset-version
|
||||
publish: bun run publish-all-packages
|
||||
version: bun run changeset-version
|
||||
env:
|
||||
# Using a PAT instead of GITHUB_TOKEN because we need to run workflows when releases are created
|
||||
# https://github.com/orgs/community/discussions/26875#discussioncomment-3253761
|
||||
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
|
||||
|
||||
@@ -25,3 +25,7 @@ yarn-error.log*
|
||||
|
||||
# TypeScript
|
||||
*.tsbuildinfo
|
||||
.npmrc
|
||||
|
||||
# Bun pack artifacts
|
||||
packages/*/*.tgz
|
||||
|
||||
@@ -35,7 +35,7 @@ To run a local version of this project, please follow these simple steps.
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js (Version: >=20.6)
|
||||
- Node.js (Version: >= 22.3)
|
||||
- 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
|
||||
@@ -90,7 +90,7 @@ If you are GitBook staff, you'll need our NPM token in your local environment.
|
||||
```
|
||||
.env.local
|
||||
|
||||
NPM_TOKEN_READONLY=xxx
|
||||
BUN_NPM_TOKEN=xxx
|
||||
```
|
||||
|
||||
and then reinstall dependencies.
|
||||
|
||||
+3
-1
@@ -18,10 +18,12 @@
|
||||
"**/.vercel/**/*",
|
||||
"**/.cache/**/*",
|
||||
"**/.wrangler/**/*",
|
||||
"packages/embed/standalone/**/*",
|
||||
"packages/openapi-parser/src/fixtures/**/*",
|
||||
"packages/emoji-codepoints/index.ts",
|
||||
"packages/icons/src/data/*.json",
|
||||
"packages/gitbook/worker-configuration.d.ts"
|
||||
"packages/gitbook/worker-configuration.d.ts",
|
||||
"**/*.css"
|
||||
]
|
||||
},
|
||||
"formatter": {
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
[install.scopes]
|
||||
"gitbook" = { token = "$NPM_TOKEN_READONLY", url = "https://registry.npmjs.org" }
|
||||
"gitbook" = { token = "$BUN_NPM_TOKEN", url = "https://registry.npmjs.org" }
|
||||
|
||||
+29
-10
@@ -1,22 +1,27 @@
|
||||
{
|
||||
"name": "gitbook",
|
||||
"version": "0.1.0",
|
||||
"engines": {
|
||||
"node": "^22.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^1.9.4",
|
||||
"@changesets/cli": "^2.27.12",
|
||||
"turbo": "^2.5.0",
|
||||
"vercel": "^39.3.0"
|
||||
"@changesets/cli": "^2.29.7",
|
||||
"turbo": "^2.6.1",
|
||||
"vercel": "^39.4.2"
|
||||
},
|
||||
"packageManager": "bun@1.2.15",
|
||||
"packageManager": "bun@1.3.2",
|
||||
"overrides": {
|
||||
"@codemirror/state": "6.4.1",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0",
|
||||
"@types/react": "catalog:",
|
||||
"@types/react-dom": "catalog:",
|
||||
"react": "catalog:",
|
||||
"react-dom": "catalog:",
|
||||
"esbuild": "0.24.2"
|
||||
},
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "turbo run dev",
|
||||
"dev": "turbo run dev --concurrency 20",
|
||||
"build": "turbo run build",
|
||||
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
|
||||
"typecheck": "turbo run typecheck",
|
||||
@@ -26,15 +31,29 @@
|
||||
"e2e": "turbo run e2e",
|
||||
"e2e-customers": "turbo run e2e-customers",
|
||||
"changeset": "changeset",
|
||||
"changeset-version": "changeset version && bun run format",
|
||||
"release": "turbo run release && changeset publish",
|
||||
"changeset-version": "changeset version && bun run format && bun update",
|
||||
"publish-all-packages": "turbo run publish-to-npm --continue=dependencies-successful",
|
||||
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
|
||||
"clean": "turbo run clean"
|
||||
},
|
||||
"workspaces": {
|
||||
"packages": ["packages/*"],
|
||||
"catalog": {
|
||||
"@gitbook/api": "^0.128.0"
|
||||
"@tsconfig/strictest": "^2.0.6",
|
||||
"@tsconfig/node20": "^20.1.6",
|
||||
"@gitbook/api": "0.151.0",
|
||||
"@scalar/api-client-react": "^1.3.46",
|
||||
"@types/react": "^19.0.0",
|
||||
"@types/react-dom": "^19.0.0",
|
||||
"assert-never": "^1.4.0",
|
||||
"bidc": "^0.0.2",
|
||||
"bun-types": "^1.1.20",
|
||||
"classnames": "^2.5.1",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0",
|
||||
"tsdown": "^0.15.6",
|
||||
"typescript": "^5.5.3",
|
||||
"usehooks-ts": "^3.1.1"
|
||||
}
|
||||
},
|
||||
"patchedDependencies": {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
dist/
|
||||
@@ -0,0 +1,37 @@
|
||||
# @gitbook/browser-types
|
||||
|
||||
## 0.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
- Updated dependencies [10995e0]
|
||||
- @gitbook/icons@0.3.4
|
||||
|
||||
## 0.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
- Updated dependencies [6142d6b]
|
||||
- @gitbook/icons@0.3.3
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
- Updated dependencies [295f03d]
|
||||
- @gitbook/icons@0.3.2
|
||||
|
||||
## 0.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- cbc71a5: First version of the public package for typing script integrations.
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 854c448: Custom assistants followup
|
||||
- Updated dependencies [25e2b40]
|
||||
- @gitbook/icons@0.3.0
|
||||
@@ -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,36 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "0.1.3",
|
||||
"dependencies": {
|
||||
"@gitbook/api": "catalog:",
|
||||
"@gitbook/icons": "workspace:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bun-types": "catalog:",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "README.md", "CHANGELOG.md"],
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { AIToolCallResult, AIToolDefinition } from '@gitbook/api';
|
||||
import type { IconName } from '@gitbook/icons';
|
||||
|
||||
export type GitBookIntegrationEvent = 'load' | 'unload';
|
||||
|
||||
export type GitBookIntegrationEventCallback = (...args: any[]) => 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.
|
||||
*/
|
||||
registerAssistant: (assistant: GitBookAssistant) => () => void;
|
||||
};
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
/**
|
||||
* Global `window.GitBook` object accessible by integrations.
|
||||
*/
|
||||
GitBook?: GitBookGlobal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noEmit": false,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "react-jsx",
|
||||
"incremental": true,
|
||||
"types": [
|
||||
"bun-types" // add Bun global
|
||||
]
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -1,5 +1,23 @@
|
||||
# @gitbook/cache-tags
|
||||
|
||||
## 0.3.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
|
||||
## 0.3.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
|
||||
## 0.3.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
|
||||
## 0.3.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -4,22 +4,32 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"development": "./src/index.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"version": "0.3.1",
|
||||
"sideEffects": false,
|
||||
"version": "0.3.4",
|
||||
"dependencies": {
|
||||
"@gitbook/api": "catalog:",
|
||||
"assert-never": "^1.2.1"
|
||||
"assert-never": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.3"
|
||||
"bun-types": "catalog:",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"build": "tsdown",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"dev": "tsc -w"
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "src", "README.md", "CHANGELOG.md"]
|
||||
"files": ["dist", "README.md", "CHANGELOG.md"],
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ export function getCacheTag(
|
||||
| {
|
||||
tag: 'translation';
|
||||
organization: string;
|
||||
translationSettings: string;
|
||||
translation: string;
|
||||
}
|
||||
): string {
|
||||
switch (spec.tag) {
|
||||
@@ -115,23 +115,12 @@ export function getCacheTag(
|
||||
case 'openapi':
|
||||
return `organization:${spec.organization}:openapi:${spec.openAPISpec}`;
|
||||
case 'translation':
|
||||
return `organization:${spec.organization}:translation:${spec.translationSettings}`;
|
||||
return `organization:${spec.organization}:translation:${spec.translation}`;
|
||||
default:
|
||||
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.
|
||||
*/
|
||||
@@ -144,6 +133,10 @@ export function getComputedContentSourceCacheTags(
|
||||
) {
|
||||
const tags: string[] = [];
|
||||
|
||||
if (!('dependencies' in source)) {
|
||||
return tags;
|
||||
}
|
||||
|
||||
// We add the dependencies as tags, to ensure that the computed content is invalidated
|
||||
// when the dependencies are updated.
|
||||
const dependencies = Object.values(source.dependencies ?? {});
|
||||
@@ -167,18 +160,18 @@ export function getComputedContentSourceCacheTags(
|
||||
})
|
||||
);
|
||||
break;
|
||||
case 'translation-language':
|
||||
case 'translation':
|
||||
tags.push(
|
||||
getCacheTag({
|
||||
tag: 'translation',
|
||||
organization: inContext.organizationId,
|
||||
translationSettings: dependency.ref.translationSettings,
|
||||
translation: dependency.ref.translation,
|
||||
})
|
||||
);
|
||||
break;
|
||||
default:
|
||||
// Do not throw for unknown dependency types
|
||||
// as it might mean we are lacking behind the API version
|
||||
// as it might mean we are lagging behind the API version
|
||||
break;
|
||||
}
|
||||
});
|
||||
@@ -194,9 +187,8 @@ export function getComputedContentSourceCacheTags(
|
||||
}
|
||||
|
||||
// We invalidate the computed content when a new version of the integration is deployed.
|
||||
|
||||
if (source.type.startsWith('integration:')) {
|
||||
const integration = source.type.split(':')[1];
|
||||
const integration = source.type.split(':')[1]!;
|
||||
tags.push(
|
||||
getCacheTag({
|
||||
tag: 'integration',
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noEmit": false,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
|
||||
@@ -1,5 +1,33 @@
|
||||
# @gitbook/colors
|
||||
|
||||
## 0.4.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
|
||||
## 0.4.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
|
||||
## 0.4.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
|
||||
## 0.4.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 17dd382: Add `original` background color step
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 193d591: Fix return type for `colorContrast`
|
||||
|
||||
## 0.3.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -4,18 +4,28 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"development": "./src/index.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"version": "0.3.3",
|
||||
"sideEffects": false,
|
||||
"version": "0.4.3",
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.3"
|
||||
"bun-types": "catalog:",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"build": "tsdown",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"dev": "tsc -w"
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "src", "README.md", "CHANGELOG.md"]
|
||||
"files": ["dist", "README.md", "CHANGELOG.md"],
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ type RGBColor = [number, number, number];
|
||||
type OKLABColor = { L: number; A: number; B: number };
|
||||
type OKLCHColor = { L: number; C: number; H: number };
|
||||
|
||||
const D65 = [95.047, 100.0, 108.883]; // Reference white (D65)
|
||||
const D65 = [95.047, 100.0, 108.883] as const; // Reference white (D65)
|
||||
|
||||
export enum ColorCategory {
|
||||
backgrounds = 'backgrounds',
|
||||
@@ -19,7 +19,7 @@ export enum ColorCategory {
|
||||
}
|
||||
|
||||
type ColorSubScale = {
|
||||
[key: string]: number;
|
||||
[key: string]: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -56,6 +56,8 @@ export const scale: Record<ColorCategory, ColorSubScale> = {
|
||||
solid: 9,
|
||||
/** Hovered solid backgrounds */
|
||||
'solid-hover': 10,
|
||||
/** Original color */
|
||||
original: 'original',
|
||||
},
|
||||
[ColorCategory.text]: {
|
||||
/** Very low-contrast text
|
||||
@@ -211,8 +213,8 @@ export function colorScale(
|
||||
const result = [];
|
||||
|
||||
for (let index = 0; index < mapping.length; index++) {
|
||||
const targetL =
|
||||
foregroundColor.L * mapping[index] + backgroundColor.L * (1 - mapping[index]);
|
||||
const step = mapping[index]!;
|
||||
const targetL = foregroundColor.L * step + backgroundColor.L * (1 - step);
|
||||
|
||||
if (
|
||||
index === 8 &&
|
||||
@@ -295,7 +297,7 @@ export function rgbArrayToHex(rgb: RGBColor): string {
|
||||
|
||||
export function getColor(percentage: number, start: RGBColor, end: RGBColor) {
|
||||
const rgb = end.map((channel, index) => {
|
||||
return Math.round(channel + percentage * (start[index] - channel));
|
||||
return Math.round(channel + percentage * (start[index]! - channel));
|
||||
});
|
||||
|
||||
return rgbArrayToHex(rgb as RGBColor);
|
||||
@@ -392,14 +394,14 @@ export function xyzToLab65(xyz: [number, number, number]): {
|
||||
B: number;
|
||||
} {
|
||||
const [x, y, z] = xyz.map((v, i) => {
|
||||
const scaled = v / D65[i];
|
||||
const scaled = v / D65[i]!;
|
||||
return scaled > 0.008856 ? Math.cbrt(scaled) : 7.787 * scaled + 16 / 116;
|
||||
});
|
||||
|
||||
return {
|
||||
L: 116 * y - 16,
|
||||
A: 500 * (x - y),
|
||||
B: 200 * (y - z),
|
||||
L: 116 * y! - 16,
|
||||
A: 500 * (x! - y!),
|
||||
B: 200 * (y! - z!),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -419,7 +421,10 @@ export function dpsContrast(a: RGBColor, b: RGBColor) {
|
||||
return contrast < 7.5 ? 0 : contrast;
|
||||
}
|
||||
|
||||
export function colorContrast(background: string, foreground: string[] = [LIGHT_BASE, DARK_BASE]) {
|
||||
export function colorContrast(
|
||||
background: string,
|
||||
foreground: string[] = [LIGHT_BASE, DARK_BASE]
|
||||
): string {
|
||||
const bg = hexToRgbArray(background);
|
||||
|
||||
const best: { color?: RGBColor; contrast: number } = {
|
||||
@@ -436,5 +441,5 @@ export function colorContrast(background: string, foreground: string[] = [LIGHT_
|
||||
}
|
||||
}
|
||||
|
||||
return best.color ? rgbArrayToHex(best.color) : foreground[0];
|
||||
return best.color ? rgbArrayToHex(best.color) : foreground[0] || LIGHT_BASE;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noEmit": false,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
dist/
|
||||
standalone/
|
||||
@@ -0,0 +1,49 @@
|
||||
# @gitbook/embed
|
||||
|
||||
## 0.1.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
- Updated dependencies [10995e0]
|
||||
- @gitbook/icons@0.3.4
|
||||
|
||||
## 0.1.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
- Updated dependencies [6142d6b]
|
||||
- @gitbook/icons@0.3.3
|
||||
|
||||
## 0.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
- Updated dependencies [295f03d]
|
||||
- @gitbook/icons@0.3.2
|
||||
|
||||
## 0.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 7508674: Fix bundling of packages
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6f368b5: Fix embed assistant window width on small screens
|
||||
|
||||
## 0.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 81a6bd7: Improve API to control the GitBook embed
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8927e8f: Initial version of the embed SDK.
|
||||
- Updated dependencies [25e2b40]
|
||||
- @gitbook/icons@0.3.0
|
||||
@@ -0,0 +1,49 @@
|
||||
# `@gitbook/embed`
|
||||
|
||||
Embed the GitBook Docs Assistant in your product or website.
|
||||
|
||||
# Usage
|
||||
|
||||
## As a script from your docs site
|
||||
|
||||
All GitBook docs site includes a script to easily embed the docs assistant as a widget on your website.
|
||||
|
||||
The script is served at `https://docs.company.com/~gitbook/embed/script.js`.
|
||||
|
||||
You can find the embed script from your docs site settings, or you can copy the following and replace the `docs.company.com` by your docs site hostname.
|
||||
|
||||
```html
|
||||
<script src="https://docs.company.com/~gitbook/embed/script.js"></script>
|
||||
<script>
|
||||
window.GitBook('show');
|
||||
</script>
|
||||
```
|
||||
|
||||
## As a package from NPM
|
||||
|
||||
Install the package: `npm install @gitbook/embed` and import it in your web application:
|
||||
|
||||
```tsx
|
||||
import { createGitBook } from '@gitbook/embed';
|
||||
|
||||
const gitbook = createGitBook({
|
||||
siteURL: 'https://docs.company.com'
|
||||
});
|
||||
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.src = gitbook.getFrameURL();
|
||||
|
||||
const frame = gitbook.createFrame(iframe);
|
||||
```
|
||||
|
||||
## As React components
|
||||
|
||||
After installing the NPM package, you can import prebuilt React components:
|
||||
|
||||
```tsx
|
||||
import { GitBookProvider, GitBookAssistantFrame } from '@gitbook/embed/react';
|
||||
|
||||
<GitBookProvider siteURL="https://docs.company.com">
|
||||
<GitBookAssistantFrame />
|
||||
</GitBookProvider>
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "@gitbook/embed",
|
||||
"description": "Embeddable components for GitBook",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./react": {
|
||||
"types": "./dist/react/index.d.ts",
|
||||
"default": "./dist/react/index.js"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "0.1.5",
|
||||
"dependencies": {
|
||||
"@gitbook/api": "catalog:",
|
||||
"@gitbook/icons": "workspace:",
|
||||
"bidc": "catalog:"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "catalog:",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:",
|
||||
"react": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bun run build-lib && bun run build-standalone",
|
||||
"build-lib": "tsdown",
|
||||
"build-standalone": "bun build src/standalone/index.ts --bundle --minify --outdir=standalone",
|
||||
"clean": "rm -rf ./dist",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "README.md", "CHANGELOG.md"],
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { type GitBookFrameClient, createGitBookFrame } from './createGitBookFrame';
|
||||
|
||||
export type CreateGitBookOptions = {
|
||||
/**
|
||||
* URL of the GitBook site to embed.
|
||||
*/
|
||||
siteURL: string;
|
||||
};
|
||||
|
||||
export type GetFrameURLOptions = {
|
||||
/**
|
||||
* Authentication to use for the frame.
|
||||
*/
|
||||
visitor?: {
|
||||
/**
|
||||
* Signed JWT token for Adaptive Content or Visitor Authentication to use.
|
||||
*/
|
||||
token?: string;
|
||||
|
||||
/**
|
||||
* Unsigned claims to pass to the frame.
|
||||
* You can use these claims in dynamic expressions using `visitor.claims.unsigned.<claim-name>`.
|
||||
*/
|
||||
unsignedClaims?: Record<string, unknown>;
|
||||
};
|
||||
};
|
||||
|
||||
export type GitBookClient = {
|
||||
/**
|
||||
* Get the URL for a GitBook frame.
|
||||
*/
|
||||
getFrameURL: (options: GetFrameURLOptions) => string;
|
||||
/**
|
||||
* Create a new GitBook frame.
|
||||
*/
|
||||
createFrame: (iframe: HTMLIFrameElement) => GitBookFrameClient;
|
||||
};
|
||||
|
||||
export function createGitBook(options: CreateGitBookOptions) {
|
||||
const client: GitBookClient = {
|
||||
getFrameURL: (frameOptions) => {
|
||||
const url = new URL(options.siteURL);
|
||||
url.pathname = `${url.pathname.endsWith('/') ? url.pathname : `${url.pathname}/`}~gitbook/embed/assistant`;
|
||||
|
||||
if (frameOptions.visitor?.token) {
|
||||
url.searchParams.set('token', frameOptions.visitor.token);
|
||||
}
|
||||
|
||||
if (frameOptions.visitor?.unsignedClaims) {
|
||||
Object.entries(frameOptions.visitor.unsignedClaims).forEach(([key, value]) => {
|
||||
url.searchParams.set(`visitor.${key}`, String(value));
|
||||
});
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
},
|
||||
createFrame: (iframe) => createGitBookFrame(iframe),
|
||||
};
|
||||
|
||||
return client;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { createChannel } from 'bidc';
|
||||
import type {
|
||||
FrameToParentMessage,
|
||||
GitBookEmbeddableConfiguration,
|
||||
ParentToFrameMessage,
|
||||
} from './protocol';
|
||||
|
||||
export type GitBookFrameClient = {
|
||||
/**
|
||||
* Navigate to a page by its path.
|
||||
*/
|
||||
navigateToPage: (path: string) => void;
|
||||
|
||||
/**
|
||||
* Navigate to the assistant.
|
||||
*/
|
||||
navigateToAssistant: () => void;
|
||||
|
||||
/**
|
||||
* Post a message to the chat.
|
||||
*/
|
||||
postUserMessage: (message: string) => void;
|
||||
|
||||
/**
|
||||
* Clear the chat.
|
||||
*/
|
||||
clearChat: () => void;
|
||||
|
||||
/**
|
||||
* Set the placeholder settings.
|
||||
*/
|
||||
configure: (settings: Partial<GitBookEmbeddableConfiguration>) => void;
|
||||
|
||||
/**
|
||||
* Register an event listener.
|
||||
*/
|
||||
on: (event: string, listener: (...args: any[]) => void) => () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a client to communicate with the GitBook Assistant frame.
|
||||
*/
|
||||
export function createGitBookFrame(iframe: HTMLIFrameElement): GitBookFrameClient {
|
||||
if (!iframe.contentWindow) {
|
||||
throw new Error('Iframe must have a content window');
|
||||
}
|
||||
const channel = createChannel(iframe.contentWindow);
|
||||
|
||||
channel.receive((message: FrameToParentMessage) => {
|
||||
console.log('[gitbook:embed] received message', message);
|
||||
if (message.type === 'close') {
|
||||
const listeners = events.get('close') || [];
|
||||
if (listeners) {
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const sendToFrame = (message: ParentToFrameMessage) => {
|
||||
console.log('[gitbook:embed] send message', message);
|
||||
channel.send(message);
|
||||
};
|
||||
|
||||
const events = new Map<string, Array<(...args: any[]) => void>>();
|
||||
|
||||
const configuration: GitBookEmbeddableConfiguration = {
|
||||
buttons: [],
|
||||
welcomeMessage: '',
|
||||
suggestions: [],
|
||||
tools: [],
|
||||
};
|
||||
|
||||
return {
|
||||
navigateToPage: (pagePath) => {
|
||||
sendToFrame({ type: 'navigateToPage', pagePath });
|
||||
},
|
||||
navigateToAssistant: () => {
|
||||
sendToFrame({ type: 'navigateToAssistant' });
|
||||
},
|
||||
postUserMessage: (message) => sendToFrame({ type: 'postUserMessage', message }),
|
||||
configure: (settings) => {
|
||||
Object.assign(configuration, settings);
|
||||
sendToFrame({ type: 'configure', settings: configuration });
|
||||
},
|
||||
clearChat: () => sendToFrame({ type: 'clearChat' }),
|
||||
on: (event, listener) => {
|
||||
const listeners = events.get(event) || [];
|
||||
listeners.push(listener);
|
||||
events.set(event, listeners);
|
||||
return () => {
|
||||
events.set(
|
||||
event,
|
||||
listeners.filter((l) => l !== listener)
|
||||
);
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './createGitBook';
|
||||
export * from './createGitBookFrame';
|
||||
export * from './protocol';
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { AIToolCallResult, AIToolDefinition } from '@gitbook/api';
|
||||
import type { IconName } from '@gitbook/icons';
|
||||
|
||||
/**
|
||||
* Custom tool definition to be passed to the AI assistant.
|
||||
*/
|
||||
export type GitBookToolDefinition = 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'>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Custom button definition to be passed to the embeddable GitBook.
|
||||
*/
|
||||
export type GitBookEmbeddableButtonDefinition = {
|
||||
/**
|
||||
* Icon to be displayed in the button.
|
||||
*/
|
||||
icon: IconName;
|
||||
|
||||
/**
|
||||
* Label to be displayed in the button.
|
||||
*/
|
||||
label: string;
|
||||
|
||||
/**
|
||||
* Callback when the button is clicked.
|
||||
*/
|
||||
onClick: () => void | Promise<void>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Overall configuration for the layout of the embeddable GitBook.
|
||||
*/
|
||||
export type GitBookEmbeddableConfiguration = {
|
||||
/**
|
||||
* Buttons to be displayed in the header of the embeddable GitBook.
|
||||
*/
|
||||
buttons: GitBookEmbeddableButtonDefinition[];
|
||||
|
||||
/** Message to be displayed in the welcome page. */
|
||||
welcomeMessage: string;
|
||||
|
||||
/** Suggestions of questions to be displayed in the welcome page. */
|
||||
suggestions: string[];
|
||||
|
||||
/** Tools to be provided to the assistant. */
|
||||
tools: GitBookToolDefinition[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Messages sent from the parent to the frame.
|
||||
*/
|
||||
export type ParentToFrameMessage =
|
||||
| {
|
||||
type: 'postUserMessage';
|
||||
message: string;
|
||||
}
|
||||
| {
|
||||
type: 'clearChat';
|
||||
}
|
||||
| {
|
||||
type: 'configure';
|
||||
settings: GitBookEmbeddableConfiguration;
|
||||
}
|
||||
| {
|
||||
type: 'navigateToPage';
|
||||
pagePath: string;
|
||||
}
|
||||
| {
|
||||
type: 'navigateToAssistant';
|
||||
};
|
||||
|
||||
/**
|
||||
* Messages sent from the frame to the parent.
|
||||
*/
|
||||
export type FrameToParentMessage = {
|
||||
type: 'close';
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export * from './client';
|
||||
@@ -0,0 +1,53 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import type {
|
||||
GetFrameURLOptions,
|
||||
GitBookEmbeddableConfiguration,
|
||||
GitBookFrameClient,
|
||||
} from '../client';
|
||||
import { useGitBook } from './GitBookProvider';
|
||||
|
||||
export type GitBookFrameProps = {
|
||||
className?: string;
|
||||
} & GetFrameURLOptions &
|
||||
GitBookEmbeddableConfiguration;
|
||||
|
||||
/**
|
||||
* Render a frame with the GitBook Assistant in it.
|
||||
*/
|
||||
export function GitBookFrame(props: GitBookFrameProps) {
|
||||
const { className, visitor, buttons, welcomeMessage, suggestions, tools } = props;
|
||||
|
||||
const frameRef = useRef<HTMLIFrameElement>(null);
|
||||
const gitbook = useGitBook();
|
||||
const [gitbookFrame, setGitbookFrame] = useState<GitBookFrameClient | null>(null);
|
||||
|
||||
const frameURL = useMemo(() => gitbook.getFrameURL({ visitor }), [gitbook, visitor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (frameRef.current) {
|
||||
setGitbookFrame(gitbook.createFrame(frameRef.current));
|
||||
}
|
||||
}, [gitbook]);
|
||||
|
||||
useEffect(() => {
|
||||
gitbookFrame?.configure({
|
||||
buttons,
|
||||
welcomeMessage,
|
||||
suggestions,
|
||||
tools,
|
||||
});
|
||||
}, [gitbookFrame, buttons, welcomeMessage, suggestions, tools]);
|
||||
|
||||
return (
|
||||
<iframe
|
||||
title="GitBook"
|
||||
ref={frameRef}
|
||||
src={frameURL}
|
||||
width="100%"
|
||||
height="100%"
|
||||
className={className}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { type CreateGitBookOptions, createGitBook } from '../client';
|
||||
import { GitBookContext } from './context';
|
||||
|
||||
/**
|
||||
* Provider for the GitBook client.
|
||||
*/
|
||||
export function GitBookProvider(props: React.PropsWithChildren<CreateGitBookOptions>) {
|
||||
const { siteURL, children } = props;
|
||||
|
||||
const options = React.useMemo(
|
||||
() => ({
|
||||
siteURL,
|
||||
}),
|
||||
[siteURL]
|
||||
);
|
||||
|
||||
const client = React.useMemo(() => createGitBook(options), [options]);
|
||||
|
||||
return <GitBookContext.Provider value={client}>{children}</GitBookContext.Provider>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to access the GitBook client.
|
||||
*/
|
||||
export function useGitBook() {
|
||||
const context = React.useContext(GitBookContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error('This component must be used within a <GitBookProvider />');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import type { GitBookClient } from '../client';
|
||||
|
||||
export const GitBookContext = React.createContext<GitBookClient | null>(null);
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './GitBookProvider';
|
||||
export * from './GitBookFrame';
|
||||
@@ -0,0 +1,178 @@
|
||||
import './style.css';
|
||||
|
||||
import {
|
||||
type CreateGitBookOptions,
|
||||
type GetFrameURLOptions,
|
||||
type GitBookClient,
|
||||
type GitBookEmbeddableConfiguration,
|
||||
type GitBookFrameClient,
|
||||
createGitBook,
|
||||
} from '../client';
|
||||
|
||||
export type GitBook = () => void;
|
||||
|
||||
type StandaloneCalls =
|
||||
// Initialize the widget
|
||||
| ['init', CreateGitBookOptions, GetFrameURLOptions]
|
||||
// Unload the widget
|
||||
| ['unload']
|
||||
// Show the widget
|
||||
| ['show']
|
||||
// Hide the widget
|
||||
| ['hide']
|
||||
// Open the window
|
||||
| ['open']
|
||||
// Close the window
|
||||
| ['close']
|
||||
// Toggle the window
|
||||
| ['toggle']
|
||||
// Post a user message
|
||||
| ['postUserMessage', string]
|
||||
// Clear the chat
|
||||
| ['clearChat']
|
||||
// Configure the embed
|
||||
| ['configure', Partial<GitBookEmbeddableConfiguration>]
|
||||
// Navigate to a page
|
||||
| ['navigateToPage', string]
|
||||
// Navigate to the assistant
|
||||
| ['navigateToAssistant'];
|
||||
|
||||
export type GitBookStandalone = ((...args: StandaloneCalls) => void) & {
|
||||
q?: StandaloneCalls[];
|
||||
};
|
||||
|
||||
const widgetButton = document.createElement('button');
|
||||
widgetButton.id = 'gitbook-widget-button';
|
||||
widgetButton.addEventListener('click', () => {
|
||||
GitBook('toggle');
|
||||
});
|
||||
widgetButton.innerHTML = `
|
||||
<span id="gitbook-widget-button-icon"></span>
|
||||
<span id="gitbook-widget-button-label">Ask</span>
|
||||
`;
|
||||
|
||||
const widgetWindow = document.createElement('div');
|
||||
widgetWindow.id = 'gitbook-widget-window';
|
||||
widgetWindow.classList.add('hidden');
|
||||
|
||||
document.body.appendChild(widgetButton);
|
||||
document.body.appendChild(widgetWindow);
|
||||
|
||||
let widgetIframe: HTMLIFrameElement | undefined;
|
||||
let _client: GitBookClient | undefined;
|
||||
let _frame: GitBookFrameClient | undefined;
|
||||
let frameOptions: GetFrameURLOptions | undefined;
|
||||
let frameConfiguration: GitBookEmbeddableConfiguration = {
|
||||
buttons: [],
|
||||
welcomeMessage: '',
|
||||
suggestions: [],
|
||||
tools: [],
|
||||
};
|
||||
|
||||
function getClient() {
|
||||
if (!_client) {
|
||||
throw new Error(
|
||||
'GitBook client not initialized. Call GitBook("init", { siteURL: "..." }) first.'
|
||||
);
|
||||
}
|
||||
return _client;
|
||||
}
|
||||
|
||||
function getIframe() {
|
||||
if (!widgetIframe || !_frame) {
|
||||
const client = getClient();
|
||||
|
||||
widgetIframe?.remove();
|
||||
widgetIframe = document.createElement('iframe');
|
||||
widgetIframe.id = 'gitbook-widget-iframe';
|
||||
widgetIframe.src = client.getFrameURL({
|
||||
...frameOptions,
|
||||
});
|
||||
widgetWindow.appendChild(widgetIframe);
|
||||
|
||||
_frame = client.createFrame(widgetIframe);
|
||||
}
|
||||
return { iframe: widgetIframe, frame: _frame };
|
||||
}
|
||||
|
||||
const GitBook = (...args: StandaloneCalls) => {
|
||||
switch (args[0]) {
|
||||
case 'init':
|
||||
if (_client) {
|
||||
throw new Error(
|
||||
'GitBook client already initialized. Call GitBook("unload") first.'
|
||||
);
|
||||
}
|
||||
_client = createGitBook(args[1]);
|
||||
frameOptions = args[2];
|
||||
break;
|
||||
case 'unload':
|
||||
_client = undefined;
|
||||
_frame = undefined;
|
||||
widgetIframe?.remove();
|
||||
widgetWindow.classList.add('hidden');
|
||||
break;
|
||||
case 'show':
|
||||
widgetButton.classList.remove('hidden');
|
||||
break;
|
||||
case 'hide':
|
||||
widgetButton.classList.add('hidden');
|
||||
break;
|
||||
case 'open':
|
||||
widgetWindow.classList.remove('hidden');
|
||||
widgetButton.classList.add('open');
|
||||
getIframe();
|
||||
break;
|
||||
case 'toggle':
|
||||
widgetWindow.classList.toggle('hidden');
|
||||
widgetButton.classList.toggle('open');
|
||||
getIframe();
|
||||
break;
|
||||
case 'close':
|
||||
widgetWindow.classList.add('hidden');
|
||||
widgetButton.classList.remove('open');
|
||||
break;
|
||||
case 'postUserMessage':
|
||||
getIframe().frame.postUserMessage(args[1]);
|
||||
break;
|
||||
case 'configure':
|
||||
frameConfiguration = {
|
||||
...frameConfiguration,
|
||||
...args[1],
|
||||
};
|
||||
getIframe().frame.configure({
|
||||
...frameConfiguration,
|
||||
buttons: [
|
||||
...frameConfiguration.buttons,
|
||||
|
||||
// Always include a close button
|
||||
{
|
||||
icon: 'close',
|
||||
label: 'Close',
|
||||
onClick: () => {
|
||||
GitBook('close');
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
break;
|
||||
case 'clearChat':
|
||||
getIframe().frame.clearChat();
|
||||
break;
|
||||
case 'navigateToPage':
|
||||
getIframe().frame.navigateToPage(args[1]);
|
||||
break;
|
||||
case 'navigateToAssistant':
|
||||
getIframe().frame.navigateToAssistant();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
// @ts-expect-error - GitBook is not defined in the global scope
|
||||
const precalls = (window.GitBook as GitBookStandalone | undefined)?.q ?? [];
|
||||
|
||||
// @ts-expect-error - GitBook is not defined in the global scope
|
||||
window.GitBook = GitBook;
|
||||
precalls.forEach((call) => GitBook(...call));
|
||||
|
||||
GitBook('configure', {});
|
||||
@@ -0,0 +1,182 @@
|
||||
:root {
|
||||
--gitbook-widget-top: 1rem;
|
||||
--gitbook-widget-bottom: 1rem;
|
||||
--gitbook-widget-right: 1rem;
|
||||
--gitbook-widget-left: 1rem;
|
||||
|
||||
--gitbook-widget-button-height: 46px;
|
||||
|
||||
--gitbook-widget-radius: .5rem;
|
||||
--gitbook-widget-text-size: 1rem;
|
||||
--gitbook-widget-text-color: #656973;
|
||||
--gitbook-widget-border-color: #e5e5e5;
|
||||
|
||||
--gitbook-widget-background-translucent: rgba(255, 255, 255, 0.9);
|
||||
--gitbook-widget-background-translucent-hover: rgba(250, 250, 250, 0.9);
|
||||
--gitbook-widget-background-solid: #FFFFFF;
|
||||
--gitbook-widget-background-solid-hover: #FBFBFB;
|
||||
|
||||
--gitbook-widget-icon-size: 1.25rem;
|
||||
|
||||
--gitbook-widget-window-width: 28rem; /* 448px */
|
||||
--gitbook-widget-window-height: 40rem; /* 640px */
|
||||
--gitbook-widget-window-spacing: .5rem; /* Spacing between the button and the window */
|
||||
--gitbook-widget-window-bottom: calc(var(--gitbook-widget-bottom) + var(--gitbook-widget-button-height) + var(--gitbook-widget-window-spacing));
|
||||
|
||||
--gitbook-widget-transition-duration-fast: 0.2s;
|
||||
--gitbook-widget-transition-duration-slow: 0.5s;
|
||||
--gitbook-widget-easing: cubic-bezier(0.25, 1, 0.5, 1);
|
||||
--gitbook-widget-easing-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--gitbook-widget-text-color: #FFFFFF;
|
||||
--gitbook-widget-border-color: #202020;
|
||||
--gitbook-widget-background-translucent: rgba(15, 15, 15, 0.9);
|
||||
--gitbook-widget-background-translucent-hover: rgba(20, 20, 20, 0.9);
|
||||
--gitbook-widget-background-solid: #f0f0f0;
|
||||
}
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
/* Button */
|
||||
#gitbook-widget-button {
|
||||
position: fixed;
|
||||
bottom: var(--gitbook-widget-bottom);
|
||||
right: var(--gitbook-widget-right);
|
||||
height: var(--gitbook-widget-button-height);
|
||||
z-index: 9999;
|
||||
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-direction: row-reverse;
|
||||
gap: .5rem;
|
||||
padding: .75rem 1rem;
|
||||
|
||||
border-radius: 100px;
|
||||
border: 1px solid var(--gitbook-widget-border-color);
|
||||
background-color: var(--gitbook-widget-background-translucent);
|
||||
backdrop-filter: blur(16px);
|
||||
|
||||
font-size: var(--gitbook-widget-text-size);
|
||||
color: var(--gitbook-widget-text-color);
|
||||
|
||||
box-shadow: 0 1px 3px 0 rgba(0,0,0,0.05), 0 1px 2px -1px rgba(0,0,0,0.05);
|
||||
transition: all var(--gitbook-widget-transition-duration-fast) var(--gitbook-widget-easing);
|
||||
|
||||
cursor:pointer;
|
||||
animation: gitbook-widget-present var(--gitbook-widget-transition-duration-slow) var(--gitbook-widget-easing-bounce);
|
||||
}
|
||||
|
||||
#gitbook-widget-button:hover, #gitbook-widget-button:focus-visible {
|
||||
background-color: var(--gitbook-widget-background-translucent-hover);
|
||||
}
|
||||
#gitbook-widget-button:hover, #gitbook-widget-button:focus-visible {
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
|
||||
}
|
||||
#gitbook-widget-button:active {
|
||||
transform: translateY(0);
|
||||
box-shadow: 0 1px 3px 0 rgba(0,0,0,0.05), 0 1px 2px -1px rgba(0,0,0,0.05);
|
||||
}
|
||||
|
||||
@media (prefers-contrast: more) {
|
||||
#gitbook-widget-button {
|
||||
background-color: var(--gitbook-widget-background-solid);
|
||||
}
|
||||
#gitbook-widget-button:hover, #gitbook-widget-button:focus-visible {
|
||||
background-color: var(--gitbook-widget-background-solid-hover);
|
||||
}
|
||||
}
|
||||
|
||||
#gitbook-widget-button.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#gitbook-widget-button.open {
|
||||
padding: .75rem;
|
||||
gap: 0;
|
||||
}
|
||||
|
||||
/* Button: Icon */
|
||||
#gitbook-widget-button-icon {
|
||||
width: var(--gitbook-widget-icon-size);
|
||||
height: var(--gitbook-widget-icon-size);
|
||||
mask-image: url("https://static-2v.gitbook.com/~gitbook/static/icons/svgs/custom-icons/gitbook-assistant.svg?v=2");
|
||||
mask-size: contain;
|
||||
mask-repeat: no-repeat;
|
||||
mask-position: center;
|
||||
background-color: currentColor;
|
||||
}
|
||||
|
||||
#gitbook-widget-button.open #gitbook-widget-button-icon {
|
||||
mask-image: url('https://ka-p.fontawesome.com/releases/v6.6.0/svgs/regular/close.svg?v=2&token=a463935e93');
|
||||
}
|
||||
|
||||
/* Button: Label */
|
||||
#gitbook-widget-button.open #gitbook-widget-button-label {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Window */
|
||||
#gitbook-widget-window {
|
||||
position: fixed;
|
||||
bottom: var(--gitbook-widget-window-bottom);
|
||||
right: var(--gitbook-widget-right);
|
||||
z-index: 9998;
|
||||
width: calc(min(var(--gitbook-widget-window-width), calc(100vw - var(--gitbook-widget-right) - var(--gitbook-widget-left))));
|
||||
height: calc(min(var(--gitbook-widget-window-height), calc(100vh - var(--gitbook-widget-window-bottom) - var(--gitbook-widget-top))));
|
||||
background-color: var(--gitbook-widget-background-translucent);
|
||||
backdrop-filter: blur(48px);
|
||||
border: 1px solid var(--gitbook-widget-border-color);
|
||||
border-radius: var(--gitbook-widget-radius);
|
||||
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
||||
overflow: hidden;
|
||||
transition-property: transform, opacity, display;
|
||||
transition-duration: var(--gitbook-widget-transition-duration-slow);
|
||||
transition-timing-function: var(--gitbook-widget-easing-bounce);
|
||||
transform-origin: bottom right;
|
||||
transition-behavior: allow-discrete;
|
||||
}
|
||||
|
||||
@starting-style {
|
||||
#gitbook-widget-window {
|
||||
opacity: 0;
|
||||
transform: scale(0.9);
|
||||
}
|
||||
}
|
||||
|
||||
body:has(#gitbook-widget-button.hidden) #gitbook-widget-window {
|
||||
bottom: var(--gitbook-widget-bottom);
|
||||
}
|
||||
|
||||
#gitbook-widget-window.hidden {
|
||||
transition-property: transform, opacity, display;
|
||||
transition-duration: var(--gitbook-widget-transition-duration-fast);
|
||||
transition-timing-function: var(--gitbook-widget-easing);
|
||||
transition-behavior: allow-discrete;
|
||||
display: none;
|
||||
opacity: 0;
|
||||
transform: scale(0.9);
|
||||
}
|
||||
|
||||
#gitbook-widget-iframe {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: none;
|
||||
}
|
||||
|
||||
@keyframes gitbook-widget-present {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: scale(0.9);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: scale(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": false,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "react-jsx",
|
||||
"incremental": true,
|
||||
"types": []
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineConfig } from 'tsdown';
|
||||
|
||||
export default defineConfig([
|
||||
{
|
||||
entry: 'src/index.ts',
|
||||
outDir: 'dist',
|
||||
},
|
||||
{
|
||||
entry: 'src/react/index.ts',
|
||||
outDir: 'dist/react',
|
||||
unbundle: true,
|
||||
},
|
||||
]);
|
||||
@@ -1,13 +1,25 @@
|
||||
# @gitbook/emoji-codepoints
|
||||
|
||||
## 0.2.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
|
||||
## 0.2.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
|
||||
## 0.2.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 57adb3e: Second release to fix publishing with changeset
|
||||
- 57adb3e: Second release to fix publishing with changeset
|
||||
|
||||
## 0.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 5f8a8fe: Initial release
|
||||
- 5f8a8fe: Initial release
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@gitbook/emoji-codepoints",
|
||||
"description": "Optimized mapping of codepoints to the fully qualified emoji codepoints",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.2",
|
||||
"private": true,
|
||||
"exports": "./dist/index.ts",
|
||||
"dependencies": {},
|
||||
"sideEffects": false,
|
||||
"devDependencies": {
|
||||
"emoji-assets": "^8.0.0"
|
||||
"emoji-assets": "^9.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"generate": "bun ./build.ts",
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
dist
|
||||
@@ -0,0 +1,54 @@
|
||||
# @gitbook/expr
|
||||
|
||||
## 1.2.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
- 10995e0: Use NPM dependency for eval-estree-expression
|
||||
|
||||
## 1.2.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
|
||||
## 1.2.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
|
||||
## 1.2.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- a629900: Add dev script for @gitbook/expr
|
||||
|
||||
## 1.2.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 6da3655: Fix exports in gitbook/expr package.json
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 3548fa6: Fix eval-estree-expr named import
|
||||
|
||||
## 1.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- e1ff17e: Fix bundling of gitbook/expr package
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8ff1e3b: Add support for every/some array methods
|
||||
|
||||
## 1.0.0
|
||||
|
||||
### Major Changes
|
||||
|
||||
- ada195d: Publish gitbook/expr package to help evaluate user defined expressions.
|
||||
@@ -0,0 +1,3 @@
|
||||
# `@gitbook/expr`
|
||||
|
||||
Safely evaluate & parse user-defined GitBook expressions.
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "@gitbook/expr",
|
||||
"description": "Safely evaluate & parse user-defined GitBook expressions.",
|
||||
"version": "1.2.4",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"dependencies": {
|
||||
"acorn": "^8.14.0",
|
||||
"acorn-loose": "8.4.0",
|
||||
"acorn-walk": "^8.3.4",
|
||||
"assert-never": "catalog:",
|
||||
"escodegen": "^2.1.0",
|
||||
"eval-estree-expression": "github:jonschlinkert/eval-estree-expression#fb0246a"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/strictest": "catalog:",
|
||||
"@tsconfig/node20": "catalog:",
|
||||
"@types/escodegen": "^0.0.10",
|
||||
"@types/estree": "^1.0.6",
|
||||
"@types/json-schema": "^7.0.15",
|
||||
"bun-types": "catalog:",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"unit": "bun test",
|
||||
"clean": "rm -rf ./dist",
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "README.md", "CHANGELOG.md"],
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,889 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { ExpressionRuntime } from '../runtime';
|
||||
import {
|
||||
SymbolArray,
|
||||
SymbolBoolean,
|
||||
SymbolNumber,
|
||||
SymbolObject,
|
||||
SymbolString,
|
||||
SymbolType,
|
||||
SymbolsTable,
|
||||
} from '../symbols';
|
||||
import {
|
||||
type AutocompleteSuggestions,
|
||||
type AutocompleteSymbolSuggestion,
|
||||
SUPPORTED_BINARY_OPERATORS,
|
||||
SUPPORTED_CONDITIONAL_OPERATORS,
|
||||
SUPPORTED_LOGICAL_OPERATORS,
|
||||
} from '../types';
|
||||
|
||||
describe('autocomplete', () => {
|
||||
const runtime = new ExpressionRuntime();
|
||||
const visitorClaimsHelloArraySymbol = SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
});
|
||||
const symbols = {
|
||||
visitor: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: visitorClaimsHelloArraySymbol,
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
};
|
||||
const context = new SymbolsTable(symbols);
|
||||
const SCENARIOS: Array<{
|
||||
expressionWithCursor: string;
|
||||
expectedSuggestions: AutocompleteSuggestions;
|
||||
}> = [
|
||||
{
|
||||
expressionWithCursor: 'visit<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor',
|
||||
parentRef: undefined,
|
||||
childrenRefs: ['visitor.claims'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor.claims',
|
||||
parentRef: 'visitor',
|
||||
childrenRefs: [
|
||||
'visitor.claims.key',
|
||||
'visitor.claims.flags',
|
||||
'visitor.claims.hello',
|
||||
'visitor.claims.role',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolString({ name: 'key' }),
|
||||
ref: 'visitor.claims.key',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.key.length',
|
||||
'visitor.claims.key.at',
|
||||
'visitor.claims.key.endsWith',
|
||||
'visitor.claims.key.includes',
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor.claims.flags',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1',
|
||||
'visitor.claims.flags.FLAG2',
|
||||
'visitor.claims.flags.FLAG3',
|
||||
'visitor.claims.flags.FLAG4',
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
ref: 'visitor.claims.hello',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.hello.length',
|
||||
'visitor.claims.hello.at',
|
||||
'visitor.claims.hello.includes',
|
||||
'visitor.claims.hello.some',
|
||||
'visitor.claims.hello.every',
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
ref: 'visitor.claims.role',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.role.length',
|
||||
'visitor.claims.role.at',
|
||||
'visitor.claims.role.endsWith',
|
||||
'visitor.claims.role.includes',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.ke<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolString({ name: 'key' }),
|
||||
ref: 'visitor.claims.key',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.key.length',
|
||||
'visitor.claims.key.at',
|
||||
'visitor.claims.key.endsWith',
|
||||
'visitor.claims.key.includes',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.h<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
ref: 'visitor.claims.hello',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.hello.length',
|
||||
'visitor.claims.hello.at',
|
||||
'visitor.claims.hello.includes',
|
||||
'visitor.claims.hello.some',
|
||||
'visitor.claims.hello.every',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.hello.<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolNumber({
|
||||
name: 'length',
|
||||
description: `The length data property of an Array instance represents the number of elements in that array.
|
||||
The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length',
|
||||
}),
|
||||
ref: 'visitor.claims.hello.length',
|
||||
parentRef: 'visitor.claims.hello',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
...visitorClaimsHelloArraySymbol.methods.map<AutocompleteSymbolSuggestion>(
|
||||
(method) => ({
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: method,
|
||||
ref: `visitor.claims.hello.${method.name}`,
|
||||
parentRef: 'visitor.claims.hello',
|
||||
childrenRefs: [],
|
||||
},
|
||||
})
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.f<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor.claims.flags',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1',
|
||||
'visitor.claims.flags.FLAG2',
|
||||
'visitor.claims.flags.FLAG3',
|
||||
'visitor.claims.flags.FLAG4',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.fl<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor.claims.flags',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1',
|
||||
'visitor.claims.flags.FLAG2',
|
||||
'visitor.claims.flags.FLAG3',
|
||||
'visitor.claims.flags.FLAG4',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG1' }),
|
||||
ref: 'visitor.claims.flags.FLAG1',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG2' }),
|
||||
ref: 'visitor.claims.flags.FLAG2',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG3' }),
|
||||
ref: 'visitor.claims.flags.FLAG3',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG4' }),
|
||||
ref: 'visitor.claims.flags.FLAG4',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FL<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG1' }),
|
||||
ref: 'visitor.claims.flags.FLAG1',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG2' }),
|
||||
ref: 'visitor.claims.flags.FLAG2',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG3' }),
|
||||
ref: 'visitor.claims.flags.FLAG3',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolBoolean({ name: 'FLAG4' }),
|
||||
ref: 'visitor.claims.flags.FLAG4',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.key <cur>',
|
||||
expectedSuggestions: [...SUPPORTED_BINARY_OPERATORS].map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 <cur>',
|
||||
expectedSuggestions: [
|
||||
...[...SUPPORTED_BINARY_OPERATORS, ...SUPPORTED_LOGICAL_OPERATORS].filter((op) =>
|
||||
['==', '!=', '===', '!==', '&&', '||'].includes(op.operator)
|
||||
),
|
||||
...SUPPORTED_CONDITIONAL_OPERATORS,
|
||||
].map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 =<cur>',
|
||||
expectedSuggestions: SUPPORTED_BINARY_OPERATORS.filter((op) =>
|
||||
['==', '==='].includes(op.operator)
|
||||
).map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 ==<cur>',
|
||||
expectedSuggestions: SUPPORTED_BINARY_OPERATORS.filter(
|
||||
(op) => op.operator === '==='
|
||||
).map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 !<cur>',
|
||||
expectedSuggestions: SUPPORTED_BINARY_OPERATORS.filter((op) =>
|
||||
['!=', '!=='].includes(op.operator)
|
||||
).map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 !=<cur>',
|
||||
expectedSuggestions: SUPPORTED_BINARY_OPERATORS.filter(
|
||||
(op) => op.operator === '!=='
|
||||
).map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == <cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: true },
|
||||
},
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: false },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == t<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == tr<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == true<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == f<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: false },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == fa<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: false },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == non<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == false<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == <cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'admin' },
|
||||
},
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'editor' },
|
||||
},
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'reader' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == ad<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'admin' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == admin<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'admin' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == "ad<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'admin' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == "admin<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'admin' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == "admin"<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == edit<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'editor' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == "edit<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'editor' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == editor<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.String, data: 'editor' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.role == "editor"<cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.hello == <cur>',
|
||||
expectedSuggestions: [],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.hello[1] == <cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: {
|
||||
kind: 'in-array',
|
||||
srcSymbol: visitorClaimsHelloArraySymbol,
|
||||
matchedLiteralString: '',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.hello[1] == "test<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: {
|
||||
kind: 'in-array',
|
||||
srcSymbol: visitorClaimsHelloArraySymbol,
|
||||
matchedLiteralString: '"test',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 == true <cur>',
|
||||
expectedSuggestions: [
|
||||
...SUPPORTED_LOGICAL_OPERATORS.filter((op) => ['&&', '||'].includes(op.operator)),
|
||||
...SUPPORTED_CONDITIONAL_OPERATORS,
|
||||
].map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})),
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 !== true && v<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor',
|
||||
parentRef: undefined,
|
||||
childrenRefs: ['visitor.claims'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 ? <cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor',
|
||||
parentRef: undefined,
|
||||
childrenRefs: ['visitor.claims'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 ? visit<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
role: SymbolString({
|
||||
name: 'role',
|
||||
enum: ['admin', 'editor', 'reader'],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor',
|
||||
parentRef: undefined,
|
||||
childrenRefs: ['visitor.claims'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
expressionWithCursor: 'visitor.claims.flags.FLAG1 ? visitor.claims.fl<cur>',
|
||||
expectedSuggestions: [
|
||||
{
|
||||
type: 'symbol',
|
||||
symbol: {
|
||||
definition: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolBoolean({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolBoolean({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolBoolean({ name: 'FLAG3' }),
|
||||
FLAG4: SymbolBoolean({ name: 'FLAG4' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
ref: 'visitor.claims.flags',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1',
|
||||
'visitor.claims.flags.FLAG2',
|
||||
'visitor.claims.flags.FLAG3',
|
||||
'visitor.claims.flags.FLAG4',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
it.each(SCENARIOS)(
|
||||
'should provide matching suggestion for expression with cursor: $expressionWithCursor',
|
||||
({ expressionWithCursor, expectedSuggestions }) => {
|
||||
const { expression, cursorOffset } = extractCursorPosition(
|
||||
expressionWithCursor,
|
||||
'<cur>'
|
||||
);
|
||||
const { suggestions } = runtime.autocomplete(expression, cursorOffset, context);
|
||||
expect(suggestions).toStrictEqual(expectedSuggestions);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
function extractCursorPosition(
|
||||
expressionWithCursor: string,
|
||||
cursorPlaceholder: string
|
||||
): { expression: string; cursorOffset: number } {
|
||||
const cursorOffset = expressionWithCursor.indexOf(cursorPlaceholder);
|
||||
if (cursorOffset === -1) {
|
||||
throw new Error(
|
||||
`Cursor position (${cursorPlaceholder}) not found in the expression string.`
|
||||
);
|
||||
}
|
||||
|
||||
const expression = expressionWithCursor.replace(cursorPlaceholder, '');
|
||||
return { expression, cursorOffset };
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { inferDefaultInputValuesFromObjectJSONSchema } from '../input-values';
|
||||
|
||||
describe('inferDefaultInputValuesFromObjectJSONSchema', () => {
|
||||
it('should infer properly the default input value based on the JSON schema of an object', () => {
|
||||
const defaultInputValues = inferDefaultInputValuesFromObjectJSONSchema({
|
||||
type: 'object',
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
FLAG1: { type: 'string' },
|
||||
FLAG2: { type: 'string' },
|
||||
FLAG3: { type: 'string' },
|
||||
},
|
||||
},
|
||||
isAlphaUser: {
|
||||
type: 'boolean',
|
||||
},
|
||||
hello: {
|
||||
type: 'string',
|
||||
enum: ['enumValue1', 'enumValue2', 'enumValue3'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(defaultInputValues).toMatchObject({
|
||||
claims: {
|
||||
key: 'default',
|
||||
flags: {
|
||||
FLAG1: 'default',
|
||||
FLAG2: 'default',
|
||||
FLAG3: 'default',
|
||||
},
|
||||
isAlphaUser: true,
|
||||
hello: 'enumValue1',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,178 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { ExpressionError } from '../errors';
|
||||
import { ExpressionRuntime } from '../runtime';
|
||||
import type { Logger } from '../types';
|
||||
|
||||
const SILENT_LOGGER: Logger = {
|
||||
debug: () => {},
|
||||
info: () => {},
|
||||
error: () => {},
|
||||
};
|
||||
|
||||
describe('ExpressionRuntime', () => {
|
||||
const runtime = new ExpressionRuntime(SILENT_LOGGER);
|
||||
|
||||
describe('evaluate', () => {
|
||||
it.each([
|
||||
{
|
||||
scenario: 'simple condition',
|
||||
condition: 'isBetaUser === true',
|
||||
inputs: { isBetaUser: false },
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
scenario: 'simple condition with multiple inputs variables',
|
||||
condition: 'useProductA && !isBetaUser',
|
||||
inputs: {
|
||||
useProductA: true,
|
||||
isBetaUser: false,
|
||||
},
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
scenario: 'condition with objects in inputs variables',
|
||||
condition: 'products.includes("productA") && userSegments.alpha',
|
||||
inputs: {
|
||||
products: ['productA', 'productB'],
|
||||
userSegments: {
|
||||
alpha: true,
|
||||
beta: false,
|
||||
},
|
||||
},
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
scenario: 'array method',
|
||||
condition: 'reviews.every(review => !!review.status)',
|
||||
inputs: { reviews: [{ status: 'approved' }, { status: 'approved' }] },
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
scenario: 'array every',
|
||||
condition: 'reviews.every(review => review.status === "approved")',
|
||||
inputs: { reviews: [{ status: 'approved' }, { status: 'approved' }] },
|
||||
expectedResult: true,
|
||||
},
|
||||
{
|
||||
scenario: 'array map',
|
||||
condition: '[1, 2, 3].map(n => n * x)',
|
||||
inputs: { x: 2 },
|
||||
expectedResult: [2, 4, 6],
|
||||
},
|
||||
])(
|
||||
'should properly evaluate/safeEvaluate a valid conditional expression: $scenario',
|
||||
({ condition, inputs, expectedResult }) => {
|
||||
expect(runtime.evaluate(condition, inputs)).toEqual(expectedResult);
|
||||
expect(runtime.safeEvaluate(condition, inputs).value).toEqual(expectedResult);
|
||||
}
|
||||
);
|
||||
|
||||
const INVALID_EXPRESSSIONS = [
|
||||
{
|
||||
scenario: 'invalid syntax',
|
||||
condition: 't}=d',
|
||||
inputs: {},
|
||||
},
|
||||
{
|
||||
scenario: 'non conditional expression',
|
||||
condition: 'const a = 1;',
|
||||
inputs: {},
|
||||
},
|
||||
{
|
||||
scenario: 'unsafe expression',
|
||||
condition: 'while (1) {}',
|
||||
inputs: {},
|
||||
},
|
||||
{
|
||||
scenario: 'unsafe expression',
|
||||
condition: '[1, 2, 3].map(() => { while (1) {}})',
|
||||
inputs: {},
|
||||
},
|
||||
];
|
||||
|
||||
it.each(INVALID_EXPRESSSIONS)(
|
||||
'should return an object with the error for non conditional expression or syntax errors when using safeEvaluate is on (default): $scenario',
|
||||
({ condition, inputs }) => {
|
||||
const result = runtime.safeEvaluate(condition, inputs);
|
||||
expect(result.value).toBeUndefined();
|
||||
expect(result.error instanceof ExpressionError).toBe(true);
|
||||
}
|
||||
);
|
||||
|
||||
it.each(INVALID_EXPRESSSIONS)(
|
||||
'should throw an error when using evaluate with invalid expressions',
|
||||
({ condition, inputs }) => {
|
||||
expect(() => runtime.evaluate(condition, inputs)).toThrowError(ExpressionError);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('parse', () => {
|
||||
it('should produce a valid ESTree compatible AST node for conditional expressions', () => {
|
||||
const ast = runtime.parse('isBetaUser === true');
|
||||
|
||||
expect(ast.result).toEqual({
|
||||
type: 'BinaryExpression',
|
||||
start: 0,
|
||||
end: 19,
|
||||
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 19 } },
|
||||
left: {
|
||||
type: 'Identifier',
|
||||
start: 0,
|
||||
end: 10,
|
||||
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 10 } },
|
||||
name: 'isBetaUser',
|
||||
},
|
||||
operator: '===',
|
||||
right: {
|
||||
type: 'Literal',
|
||||
start: 15,
|
||||
end: 19,
|
||||
loc: { start: { line: 1, column: 15 }, end: { line: 1, column: 19 } },
|
||||
value: true,
|
||||
raw: 'true',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
scenario: 'invalid syntax',
|
||||
condition: 't}=d',
|
||||
},
|
||||
{
|
||||
scenario: 'non conditional expression',
|
||||
condition: 'const a = 1;',
|
||||
},
|
||||
])(
|
||||
'should throw an error for non conditional expressions or syntax errors: $scenario',
|
||||
({ condition }) => {
|
||||
expect(() => runtime.parse(condition)).toThrowError(ExpressionError);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe.skip('generate', () => {
|
||||
it.each([
|
||||
{
|
||||
scenario: 'simple condition',
|
||||
condition: 'isBetaUser === true',
|
||||
},
|
||||
{
|
||||
scenario: 'simple condition with multiple inputs variables',
|
||||
condition: 'useProductA && !isBetaUser',
|
||||
},
|
||||
{
|
||||
scenario: 'condition with objects in inputs variables',
|
||||
condition: 'products.includes("productA") && userSegments.alpha',
|
||||
},
|
||||
])(
|
||||
'should produce the original expression using an AST node produced by parse: $scenario',
|
||||
({ condition }) => {
|
||||
const { result } = runtime.parse(condition);
|
||||
expect(runtime.generate(result)).toStrictEqual(condition);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { ExpressionRuntime, parseTemplate } from '../';
|
||||
|
||||
describe('template expressions', () => {
|
||||
it('should parse template into parts', () => {
|
||||
const parts = parseTemplate('Hello {{ user.name }}!');
|
||||
expect(parts).toEqual([
|
||||
{ type: 'text', value: 'Hello ', start: 0, end: 6 },
|
||||
{ type: 'expression', value: 'user.name', start: 8, end: 19 },
|
||||
{ type: 'text', value: '!', start: 21, end: 22 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should parse template starting with an expression', () => {
|
||||
const parts = parseTemplate('{{ user.name }} is cool');
|
||||
expect(parts).toEqual([
|
||||
{ type: 'expression', value: 'user.name', start: 2, end: 13 },
|
||||
{ type: 'text', value: ' is cool', start: 15, end: 23 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should parse template without expressions', () => {
|
||||
const parts = parseTemplate('Hello world');
|
||||
expect(parts).toEqual([{ type: 'text', value: 'Hello world', start: 0, end: 11 }]);
|
||||
});
|
||||
|
||||
it('should evaluate template', () => {
|
||||
const runtime = new ExpressionRuntime();
|
||||
const result = runtime.evaluateTemplate('Hello {{ user.name }}!', {
|
||||
user: { name: 'John' },
|
||||
});
|
||||
expect(result).toBe('Hello John!');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,568 @@
|
||||
import type {
|
||||
Node as AcornNode,
|
||||
AnyNode,
|
||||
BinaryExpression,
|
||||
Expression,
|
||||
Identifier,
|
||||
Literal,
|
||||
MemberExpression,
|
||||
PrivateIdentifier,
|
||||
Super,
|
||||
} from 'acorn';
|
||||
import { isDummy } from 'acorn-loose';
|
||||
import * as walk from 'acorn-walk';
|
||||
|
||||
import assertNever from 'assert-never';
|
||||
|
||||
import { type ExtractSymbolDef, SymbolType, SymbolsTable } from './symbols';
|
||||
import {
|
||||
type AutocompleteLiteralValueSuggestion,
|
||||
type AutocompleteOperatorSuggestion,
|
||||
type AutocompleteSuggestions,
|
||||
type AutocompleteSymbolSuggestion,
|
||||
type DirectLiteralValueSuggestion,
|
||||
type ExpressionParserResult,
|
||||
type Logger,
|
||||
SUPPORTED_BINARY_OPERATORS,
|
||||
SUPPORTED_CONDITIONAL_OPERATORS,
|
||||
SUPPORTED_LOGICAL_OPERATORS,
|
||||
} from './types';
|
||||
|
||||
interface ExpressionParser {
|
||||
parse(expr: string, options: { loose?: boolean }): ExpressionParserResult;
|
||||
}
|
||||
|
||||
export class AutoComplete {
|
||||
#parser: ExpressionParser;
|
||||
#logger: Logger;
|
||||
|
||||
constructor(parser: ExpressionParser, logger: Logger = console) {
|
||||
this.#parser = parser;
|
||||
this.#logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates autocomplete suggestions based on the input expression and cursor offset position.
|
||||
*/
|
||||
public getSuggestions(
|
||||
expr: string,
|
||||
cursorOffset: number,
|
||||
context: SymbolsTable
|
||||
): AutocompleteSuggestions {
|
||||
if (!expr.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const { result, invalidNodes } = this.#parser.parse(expr, { loose: true });
|
||||
|
||||
// Locate the node at the cursor position.
|
||||
const nodeAtCursorFound = walk.findNodeAround(result, cursorOffset, (_type, node) =>
|
||||
isNodeAtCursor(node, cursorOffset)
|
||||
);
|
||||
|
||||
if (!nodeAtCursorFound) {
|
||||
// When we can't find one we might be in the boundary of the program/expression.
|
||||
// We could possibly be in a whitespace at the end of the expression or in a situation where the parsed
|
||||
// tree may contain 2 top level ExpressionStatement (second one returned as invalid node by the parser).
|
||||
//
|
||||
// In this case we want to provide operators as suggestions and refine the search of the "cursor" node to either:
|
||||
// - using the end position of the whole expression AST (e.g white space at the very end of the expression string)
|
||||
// - or using the second top level ExpressionStatement node found by the parser as the cursor is at the end of that node.
|
||||
if (cursorOffset > result.end) {
|
||||
const ast = invalidNodes.length > 0 ? invalidNodes[0]?.expression : result;
|
||||
|
||||
if (!ast) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const lastNodeFound = walk.findNodeAround(ast, ast.end, (_type, node) =>
|
||||
isNodeAtCursor(node, ast.end)
|
||||
);
|
||||
if (!lastNodeFound) {
|
||||
return [];
|
||||
}
|
||||
const { node } = lastNodeFound;
|
||||
|
||||
if (!isAnyNode(node)) {
|
||||
throw Error(`Unexpected node type ${node.type}`);
|
||||
}
|
||||
|
||||
return this.getOperatorSuggestionsForNode(ast, node, result.end, context);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
const { node } = nodeAtCursorFound;
|
||||
|
||||
if (!isAnyNode(node)) {
|
||||
throw Error(`Unexpected node type ${node.type}`);
|
||||
}
|
||||
|
||||
return this.getSuggestionsForNode(result, node, expr, cursorOffset, context);
|
||||
} catch (error) {
|
||||
this.#logger.error('Error while computing autocomplete suggestions', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides autocomplete suggestions for a specific node in the AST.
|
||||
*/
|
||||
private getSuggestionsForNode(
|
||||
ast: Expression,
|
||||
node: AnyNode,
|
||||
expr: string,
|
||||
cursorOffset: number,
|
||||
context: SymbolsTable
|
||||
): AutocompleteSuggestions {
|
||||
// When the node is an identifier look up the parent to get more context for the suggestions.
|
||||
let inferNode: AnyNode = node;
|
||||
if (node.type === 'Identifier' || node.type === 'Literal') {
|
||||
const parent = findParentNode(node, ast);
|
||||
inferNode =
|
||||
parent &&
|
||||
!['ExpressionStatement', 'LogicalExpression', 'ConditionalExpression'].includes(
|
||||
parent.type
|
||||
)
|
||||
? parent
|
||||
: node;
|
||||
}
|
||||
|
||||
switch (inferNode.type) {
|
||||
case 'Identifier':
|
||||
case 'MemberExpression': {
|
||||
const pathParts = this.getSymbolsPathPartsForMemberExpressionNode(
|
||||
inferNode,
|
||||
cursorOffset
|
||||
);
|
||||
|
||||
// Fetch suggestions from the symbol table
|
||||
const candidatesKeys = context.getMatchingSymbolsKeys(pathParts);
|
||||
|
||||
const suggestions: AutocompleteSymbolSuggestion[] = [];
|
||||
for (const candidate of candidatesKeys) {
|
||||
const symbolInfo = context.getSymbolInfo(candidate);
|
||||
if (symbolInfo) {
|
||||
suggestions.push({ type: 'symbol', symbol: symbolInfo });
|
||||
}
|
||||
}
|
||||
|
||||
if (suggestions.length === 1) {
|
||||
const lastPathSegment = pathParts.at(-1)?.replace(/\*$/, '');
|
||||
|
||||
// Return no suggestion when the only match is an exact match of the
|
||||
// typed token.
|
||||
return lastPathSegment !== suggestions[0]?.symbol.definition.name
|
||||
? suggestions
|
||||
: [];
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
case 'AssignmentExpression':
|
||||
case 'UnaryExpression': {
|
||||
// Provide suggestions for binary or logical operators based on the parsed operator
|
||||
// of the partial expression (e.g suggest "==" when typing "=" (parsed as AssignmentExpression)).
|
||||
return this.getOperatorSuggestionsForNode(ast, inferNode, cursorOffset, context);
|
||||
}
|
||||
case 'BinaryExpression': {
|
||||
const { left, right } = inferNode;
|
||||
|
||||
const isOperatorBinaryOp = SUPPORTED_BINARY_OPERATORS.some(
|
||||
(op) => op.operator === inferNode.operator
|
||||
);
|
||||
|
||||
const operatorIndex = expr.indexOf(inferNode.operator, left.end);
|
||||
const operatorOffset = operatorIndex + inferNode.operator.length;
|
||||
const isCursorAfterOperator = cursorOffset > operatorOffset;
|
||||
|
||||
const shouldSuggestValues =
|
||||
isCursorAfterOperator &&
|
||||
isOperatorBinaryOp &&
|
||||
isNodeAtCursor(right, cursorOffset);
|
||||
|
||||
if (shouldSuggestValues) {
|
||||
return this.getLiteralValueSuggestionsForNode(inferNode, cursorOffset, context);
|
||||
}
|
||||
|
||||
// Provide suggestions for binary operators based on the parsed operator
|
||||
return this.getOperatorSuggestionsForNode(ast, inferNode, cursorOffset, context);
|
||||
}
|
||||
case 'ConditionalExpression': {
|
||||
const { consequent, alternate } = inferNode;
|
||||
|
||||
if (isNodeAtCursor(consequent, cursorOffset)) {
|
||||
return this.getSuggestionsForNode(ast, consequent, expr, cursorOffset, context);
|
||||
}
|
||||
|
||||
if (isNodeAtCursor(alternate, cursorOffset)) {
|
||||
return this.getSuggestionsForNode(ast, alternate, expr, cursorOffset, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a path corresponding to the MemberExpression node that can be used to lookup matching symbols in the symbol table.
|
||||
*/
|
||||
private getSymbolsPathPartsForMemberExpressionNode(
|
||||
node: MemberExpression | Identifier,
|
||||
cursorOffset: number,
|
||||
options?: { withWildcardMatches: boolean }
|
||||
): string[] {
|
||||
const withWildcardMatches = options?.withWildcardMatches ?? true;
|
||||
const pathParts: string[] = [];
|
||||
|
||||
switch (node.type) {
|
||||
case 'MemberExpression':
|
||||
{
|
||||
const memberProperty = node.property;
|
||||
|
||||
// Only support identifier or literal expressions as member properties.
|
||||
if (!isSupportedMemberProperty(memberProperty)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Push the path part corresponding to the member property (e.g b in a.b or a['b'])
|
||||
const propertyPathPart = this.getSymbolsPathPartFromMemberProperty(
|
||||
memberProperty,
|
||||
cursorOffset
|
||||
);
|
||||
|
||||
if (propertyPathPart) {
|
||||
pathParts.push(
|
||||
withWildcardMatches ? `${propertyPathPart}*` : propertyPathPart
|
||||
);
|
||||
}
|
||||
|
||||
// Go through the parent(s) in the chain and add their path parts as well
|
||||
let parent: Expression | Super | undefined = node.object;
|
||||
while (parent) {
|
||||
const parentProperty = 'property' in parent ? parent.property : parent;
|
||||
|
||||
// Only support identifier or literal expressions as parent property.
|
||||
const parentPropertyPathPart = isSupportedMemberProperty(parentProperty)
|
||||
? this.getSymbolsPathPartFromMemberProperty(
|
||||
parentProperty,
|
||||
cursorOffset
|
||||
)
|
||||
: undefined;
|
||||
|
||||
if (parentPropertyPathPart) {
|
||||
pathParts.unshift(parentPropertyPathPart);
|
||||
}
|
||||
|
||||
parent = 'object' in parent ? parent.object : undefined;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Identifier': {
|
||||
const propertyPathPart = this.getSymbolsPathPartFromMemberProperty(
|
||||
node,
|
||||
cursorOffset
|
||||
);
|
||||
if (propertyPathPart) {
|
||||
pathParts.push(withWildcardMatches ? `${propertyPathPart}*` : propertyPathPart);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assertNever(node);
|
||||
}
|
||||
|
||||
return pathParts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a part of a symbol path corresponding to a MemberExpression node property.
|
||||
*/
|
||||
private getSymbolsPathPartFromMemberProperty(
|
||||
node: Identifier | Literal,
|
||||
cursorOffset: number
|
||||
): string {
|
||||
switch (node.type) {
|
||||
case 'Identifier': {
|
||||
if (isDummy(node)) {
|
||||
return '*';
|
||||
}
|
||||
|
||||
return isNodeAtCursor(node, cursorOffset)
|
||||
? node.name.slice(0, cursorOffset)
|
||||
: node.name;
|
||||
}
|
||||
case 'Literal': {
|
||||
if (isDummy(node) || !node.value) {
|
||||
return '*';
|
||||
}
|
||||
const value = String(node.value);
|
||||
return isNodeAtCursor(node, cursorOffset) ? value.slice(0, cursorOffset) : value;
|
||||
}
|
||||
default:
|
||||
assertNever(node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides autocomplete literal value suggestions for a specific node in the AST.
|
||||
*/
|
||||
private getLiteralValueSuggestionsForNode(
|
||||
node: BinaryExpression,
|
||||
cursorOffset: number,
|
||||
context: SymbolsTable
|
||||
): Array<AutocompleteLiteralValueSuggestion> {
|
||||
const { left, right } = node;
|
||||
|
||||
if (left.type !== 'MemberExpression' && left.type !== 'Identifier') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (right.type !== 'Identifier' && right.type !== 'Literal') {
|
||||
return [];
|
||||
}
|
||||
|
||||
const leftSymbolPath = this.getSymbolsPathPartsForMemberExpressionNode(left, cursorOffset, {
|
||||
withWildcardMatches: false,
|
||||
});
|
||||
|
||||
const isLeftComputedMember = left.type === 'MemberExpression' && left.computed;
|
||||
const leftSymbolInfo = context.getSymbolInfo(
|
||||
isLeftComputedMember ? leftSymbolPath.slice(0, -1) : leftSymbolPath
|
||||
);
|
||||
|
||||
if (!leftSymbolInfo) {
|
||||
return [];
|
||||
}
|
||||
|
||||
switch (leftSymbolInfo.definition.type) {
|
||||
case SymbolType.Boolean: {
|
||||
const suggestions: DirectLiteralValueSuggestion[] = [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: true },
|
||||
},
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: { kind: 'direct', type: SymbolType.Boolean, data: false },
|
||||
},
|
||||
];
|
||||
|
||||
return isDummy(right)
|
||||
? suggestions
|
||||
: suggestions.filter((literalValue) => {
|
||||
const literalValueString = String(literalValue.value.data);
|
||||
const rightNodeValue =
|
||||
right.type === 'Identifier' ? right.name : (right.raw ?? '');
|
||||
return (
|
||||
literalValueString !== rightNodeValue &&
|
||||
literalValueString.startsWith(rightNodeValue)
|
||||
);
|
||||
});
|
||||
}
|
||||
case SymbolType.String: {
|
||||
if (!leftSymbolInfo.definition.enum) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const rightNodeValue = (() => {
|
||||
if (right.type === 'Identifier') {
|
||||
return right.name;
|
||||
}
|
||||
|
||||
const nodeValue = right.raw ?? '';
|
||||
if (/^(['"])(.*)\1$/.test(nodeValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return nodeValue.replaceAll(/["']/g, '');
|
||||
})();
|
||||
|
||||
if (rightNodeValue === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const suggestions = isDummy(right)
|
||||
? leftSymbolInfo.definition.enum
|
||||
: leftSymbolInfo.definition.enum.filter((enumValue) =>
|
||||
enumValue.startsWith(rightNodeValue)
|
||||
);
|
||||
|
||||
return suggestions.map((value) => ({
|
||||
type: 'literal-value',
|
||||
value: {
|
||||
kind: 'direct',
|
||||
type: SymbolType.String,
|
||||
data: value,
|
||||
},
|
||||
}));
|
||||
}
|
||||
case SymbolType.Array: {
|
||||
// Only return a literal in array value suggestion when the left hand side of the binary expression
|
||||
// is computed, e.g myArray[1]
|
||||
return isLeftComputedMember
|
||||
? [
|
||||
{
|
||||
type: 'literal-value',
|
||||
value: {
|
||||
kind: 'in-array',
|
||||
srcSymbol: leftSymbolInfo.definition,
|
||||
matchedLiteralString: !isDummy(right)
|
||||
? right.type === 'Identifier'
|
||||
? right.name
|
||||
: (right.raw ?? '')
|
||||
: '',
|
||||
},
|
||||
},
|
||||
]
|
||||
: [];
|
||||
}
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides autocomplete operator suggestions for a specific node in the AST.
|
||||
*/
|
||||
private getOperatorSuggestionsForNode(
|
||||
ast: Expression,
|
||||
node: AnyNode,
|
||||
cursorOffset: number,
|
||||
context: SymbolsTable
|
||||
): Array<AutocompleteOperatorSuggestion> {
|
||||
if (node.type === 'Literal') {
|
||||
const parent = findParentNode(node, ast);
|
||||
|
||||
if (parent?.type === 'BinaryExpression') {
|
||||
return [...SUPPORTED_LOGICAL_OPERATORS, ...SUPPORTED_CONDITIONAL_OPERATORS].map(
|
||||
(op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const literalSymbol = SymbolsTable.inferSymbolFromValue(node.raw);
|
||||
return this.getOperatorSuggestionsForSymbol(literalSymbol);
|
||||
}
|
||||
|
||||
// When the node is an identifier look the parent to get more context for the suggestions
|
||||
let inferNode: AnyNode = node;
|
||||
if (node.type === 'Identifier') {
|
||||
const parent = findParentNode(node, ast);
|
||||
inferNode = parent && parent.type !== 'ExpressionStatement' ? parent : node;
|
||||
}
|
||||
|
||||
switch (inferNode.type) {
|
||||
case 'MemberExpression': {
|
||||
const pathParts = this.getSymbolsPathPartsForMemberExpressionNode(
|
||||
inferNode,
|
||||
cursorOffset,
|
||||
{
|
||||
withWildcardMatches: false,
|
||||
}
|
||||
);
|
||||
const symbolInfo = context.getSymbolInfo(pathParts);
|
||||
return symbolInfo
|
||||
? this.getOperatorSuggestionsForSymbol(symbolInfo.definition)
|
||||
: [];
|
||||
}
|
||||
case 'AssignmentExpression':
|
||||
case 'BinaryExpression':
|
||||
case 'UnaryExpression': {
|
||||
// Starting to write a binary/logical operator so suggest operator matching the already
|
||||
// typed character as operator.
|
||||
const operator = inferNode.operator;
|
||||
return (
|
||||
[...SUPPORTED_BINARY_OPERATORS, ...SUPPORTED_LOGICAL_OPERATORS]
|
||||
.filter((op) => op.operator.startsWith(operator))
|
||||
// No need to include the operator that match exactly
|
||||
.filter((op) => op.operator !== operator)
|
||||
.map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
}))
|
||||
);
|
||||
}
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides autocomplete operator suggestions based on the type of a symbol.
|
||||
*/
|
||||
private getOperatorSuggestionsForSymbol(
|
||||
symbol: ExtractSymbolDef<SymbolType>
|
||||
): Array<AutocompleteOperatorSuggestion> {
|
||||
switch (symbol.type) {
|
||||
case SymbolType.Number:
|
||||
case SymbolType.Boolean:
|
||||
case SymbolType.Null:
|
||||
case SymbolType.Undefined:
|
||||
case SymbolType.Object:
|
||||
case SymbolType.Array: {
|
||||
const equalityOps = SUPPORTED_BINARY_OPERATORS.slice(0, 4);
|
||||
const finalSuggestions =
|
||||
symbol.type === SymbolType.Boolean
|
||||
? [
|
||||
...equalityOps,
|
||||
...SUPPORTED_LOGICAL_OPERATORS,
|
||||
...SUPPORTED_CONDITIONAL_OPERATORS,
|
||||
]
|
||||
: equalityOps;
|
||||
return finalSuggestions.map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
}));
|
||||
}
|
||||
case SymbolType.String:
|
||||
return SUPPORTED_BINARY_OPERATORS.map((op) => ({
|
||||
type: 'operator',
|
||||
...op,
|
||||
}));
|
||||
case SymbolType.Function:
|
||||
return this.getOperatorSuggestionsForSymbol(symbol.returns);
|
||||
case SymbolType.Union: {
|
||||
return symbol.members.reduce<Array<AutocompleteOperatorSuggestion>>((prev, cur) => {
|
||||
prev.push(...this.getOperatorSuggestionsForSymbol(cur));
|
||||
return prev;
|
||||
}, []);
|
||||
}
|
||||
default:
|
||||
assertNever(symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the parent of child node in the provided AST.
|
||||
*/
|
||||
function findParentNode(child: AnyNode, ast: Expression): AnyNode | undefined {
|
||||
let foundParent: AnyNode | undefined;
|
||||
|
||||
walk.ancestor(ast, {
|
||||
[child.type]: (node: AcornNode, _state: undefined, ancestors: AnyNode[]) => {
|
||||
if (node.start === child.start && node.end === child.end) {
|
||||
// The parent is the second last ancestor in the stack (last one is the actual node)
|
||||
foundParent = ancestors[ancestors.length - 2];
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return foundParent;
|
||||
}
|
||||
|
||||
function isSupportedMemberProperty(node: Expression | PrivateIdentifier | Super) {
|
||||
return node.type === 'Identifier' || node.type === 'Literal';
|
||||
}
|
||||
|
||||
function isNodeAtCursor(node: AcornNode, cursorOffset: number) {
|
||||
return cursorOffset >= node.start && cursorOffset <= node.end;
|
||||
}
|
||||
|
||||
function isAnyNode(node: AcornNode): node is AnyNode {
|
||||
return 'type' in node;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Position, Token } from 'acorn';
|
||||
|
||||
export class ExpressionError extends Error {
|
||||
/**
|
||||
* The location of the error in the parsed expression.
|
||||
*/
|
||||
public location?: Position | null;
|
||||
|
||||
/**
|
||||
* The expression token with the error
|
||||
*/
|
||||
public token?: Token;
|
||||
|
||||
constructor(message: string, loc?: Position | null, token?: Token) {
|
||||
super(message);
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, ExpressionError);
|
||||
}
|
||||
this.name = 'ExpressionError';
|
||||
this.location = loc;
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export * from './errors';
|
||||
export * from './input-values';
|
||||
export * from './runtime';
|
||||
export * from './symbols';
|
||||
export * from './template';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { JSONSchema7 } from 'json-schema';
|
||||
import { filterOutNullable } from './utils';
|
||||
|
||||
type InputValuesType =
|
||||
| null
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| { [key: string]: InputValuesType }
|
||||
| InputValuesType[];
|
||||
|
||||
/**
|
||||
* Infers a default inputValues object based on the JSON schema of an object.
|
||||
*/
|
||||
export function inferDefaultInputValuesFromObjectJSONSchema(
|
||||
schema: JSONSchema7
|
||||
): Record<string, InputValuesType> {
|
||||
if (schema.type !== 'object' || !schema.properties) {
|
||||
throw new Error(`Expected schema of object to be provided: ${schema.type}`);
|
||||
}
|
||||
|
||||
const result: Record<string, InputValuesType> = {};
|
||||
|
||||
for (const [key, propertySchema] of Object.entries(schema.properties)) {
|
||||
if (typeof propertySchema === 'boolean') {
|
||||
continue;
|
||||
}
|
||||
|
||||
result[key] = inferDefaultInputValueFromJSONSchema(propertySchema);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function inferDefaultInputValueFromJSONSchema(schema: JSONSchema7): InputValuesType {
|
||||
switch (schema.type) {
|
||||
case 'object':
|
||||
return inferDefaultInputValuesFromObjectJSONSchema(schema);
|
||||
case 'array': {
|
||||
if (schema.items && Array.isArray(schema.items)) {
|
||||
return schema.items.map((itemSchema) => {
|
||||
if (typeof itemSchema === 'boolean') {
|
||||
return false;
|
||||
}
|
||||
return inferDefaultInputValueFromJSONSchema(itemSchema);
|
||||
});
|
||||
}
|
||||
return [];
|
||||
}
|
||||
case 'string':
|
||||
case 'number':
|
||||
case 'integer':
|
||||
case 'boolean':
|
||||
case 'null':
|
||||
return inferDefaultInputValueFromPrimitive(schema);
|
||||
default:
|
||||
throw new Error(`Unsupported schema type: ${schema.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
function inferDefaultInputValueFromPrimitive(schema: JSONSchema7): InputValuesType {
|
||||
switch (schema.type) {
|
||||
case 'boolean':
|
||||
return true;
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return 1234;
|
||||
case 'string': {
|
||||
const enumValues = schema.enum?.filter(filterOutNullable);
|
||||
return enumValues?.[0] ?? 'default';
|
||||
}
|
||||
case 'null':
|
||||
return null;
|
||||
default:
|
||||
throw new Error(`Unsupported schema type: ${schema.type}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
import {
|
||||
type Options as AcornOptions,
|
||||
type Expression,
|
||||
type ExpressionStatement,
|
||||
type Position,
|
||||
type Program,
|
||||
type Token,
|
||||
parse,
|
||||
tokenizer,
|
||||
} from 'acorn';
|
||||
import { parse as parseLoose } from 'acorn-loose';
|
||||
import escodegen from 'escodegen';
|
||||
import evalESTreeExpr from 'eval-estree-expression';
|
||||
const { evaluate } = evalESTreeExpr;
|
||||
|
||||
import { AutoComplete } from './autocomplete';
|
||||
import { ExpressionError } from './errors';
|
||||
import type { SymbolsTable } from './symbols';
|
||||
import type { TemplatePart } from './template';
|
||||
import { parseTemplate as parseTemplateParts } from './template';
|
||||
import type { ExpressionAutocompleteResults, ExpressionParserResult, Logger } from './types';
|
||||
import { formatExpressionResult } from './utils';
|
||||
|
||||
export class ExpressionRuntime {
|
||||
#parserOptions: AcornOptions;
|
||||
#autocompleter: AutoComplete;
|
||||
#logger: Logger;
|
||||
|
||||
constructor(logger: Logger = console) {
|
||||
this.#parserOptions = {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'script',
|
||||
allowHashBang: false,
|
||||
locations: true,
|
||||
};
|
||||
this.#autocompleter = new AutoComplete(this, logger);
|
||||
this.#logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an expression based on the given inputs/context.
|
||||
*/
|
||||
public evaluate(expr: string, inputs: object): unknown {
|
||||
try {
|
||||
const parsed = this.parse(expr);
|
||||
|
||||
if (parsed.invalidNodes.length > 0) {
|
||||
throw new ExpressionError('Invalid nodes found when parsing');
|
||||
}
|
||||
|
||||
return evaluate.sync<Expression>(parsed.result, inputs, {
|
||||
functions: true,
|
||||
withMembers: true,
|
||||
generate: escodegen.generate,
|
||||
});
|
||||
} catch (error) {
|
||||
throw error instanceof Error
|
||||
? new ExpressionError(error.message)
|
||||
: new ExpressionError('Unexpected error');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an expression safely by returning the error instead of throwing when invalid.
|
||||
*/
|
||||
public safeEvaluate(
|
||||
expr: string,
|
||||
inputs: object
|
||||
): { value: unknown; error?: undefined } | { value?: undefined; error: ExpressionError } {
|
||||
try {
|
||||
const value = this.evaluate(expr, inputs);
|
||||
return {
|
||||
value,
|
||||
};
|
||||
} catch (error) {
|
||||
this.#logger.error(`Error while evaluating expression ${expr}`, error);
|
||||
|
||||
if (error instanceof ExpressionError) {
|
||||
return {
|
||||
value: undefined,
|
||||
error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
value: undefined,
|
||||
error:
|
||||
error instanceof Error
|
||||
? new ExpressionError(error.message)
|
||||
: new ExpressionError('Unexpected error'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a condition safely to a boolean.
|
||||
*/
|
||||
public evaluateBoolean(expr: string, inputs: object): boolean {
|
||||
if (expr.trim().length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const evalResult = this.safeEvaluate(expr, inputs);
|
||||
|
||||
if (typeof evalResult.error !== 'undefined') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Boolean(evalResult.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an array of conditions as a single logical expression.
|
||||
* The function treats the conditions as if they were joined by an AND operator,
|
||||
* meaning the evaluation returns `true` only if all conditions are truthy.
|
||||
*/
|
||||
public evaluateBooleanAll(expressions: string[], inputs: object): boolean {
|
||||
if (expressions.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return expressions.every((expression) => this.evaluateBoolean(expression, inputs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a template and validate all embedded expressions.
|
||||
*/
|
||||
public parseTemplate(template: string): { parts: TemplatePart[]; errors: ExpressionError[] } {
|
||||
const parts = parseTemplateParts(template);
|
||||
const errors: ExpressionError[] = [];
|
||||
|
||||
for (const part of parts) {
|
||||
if (part.type === 'expression') {
|
||||
try {
|
||||
const { invalidNodes } = this.parse(part.value);
|
||||
if (invalidNodes.length > 0) {
|
||||
errors.push(new ExpressionError('Invalid expression'));
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(error as ExpressionError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { parts, errors };
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a template string containing `{{ expression }}` placeholders.
|
||||
*/
|
||||
public evaluateTemplate(template: string, inputs: object): string {
|
||||
const { parts } = this.parseTemplate(template);
|
||||
|
||||
return parts
|
||||
.map((part) => {
|
||||
if (part.type === 'text') {
|
||||
return part.value;
|
||||
}
|
||||
const result = this.evaluate(part.value, inputs);
|
||||
return formatExpressionResult(result, '');
|
||||
})
|
||||
.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a binary expression and returns an @ExpressionParserResult.
|
||||
*/
|
||||
public parse(
|
||||
expr: string,
|
||||
options: { loose?: boolean } = {
|
||||
loose: false,
|
||||
}
|
||||
): ExpressionParserResult {
|
||||
try {
|
||||
const ast = options.loose
|
||||
? parseLoose(expr, { ...this.#parserOptions })
|
||||
: parse(expr, { ...this.#parserOptions });
|
||||
|
||||
if (!ast.body || ast.body.length === 0) {
|
||||
throw new ExpressionError('Empty or invalid expression');
|
||||
}
|
||||
|
||||
// Extract the first expression statement that we find
|
||||
const firstExprIndex = ast.body.findIndex((node) => isParsedExpressionStatement(node));
|
||||
const [statement] = ast.body.splice(firstExprIndex, 1);
|
||||
|
||||
if (!statement || !isParsedExpressionStatement(statement)) {
|
||||
throw new ExpressionError('Empty or invalid expression');
|
||||
}
|
||||
|
||||
// Return information on the other nodes as invalid nodes
|
||||
const invalidNodes = ast.body.filter(filterOutModuleDeclarationStatement);
|
||||
|
||||
return {
|
||||
result: statement.expression,
|
||||
invalidNodes,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
throw createExpressionErrorFromSyntaxError(expr, error);
|
||||
}
|
||||
if (error instanceof ExpressionError) {
|
||||
throw error;
|
||||
}
|
||||
throw new ExpressionError('Unexpected error');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides autocomplete suggestions for the given expression at the provided cursor offset.
|
||||
*/
|
||||
public autocomplete(
|
||||
expr: string,
|
||||
cursorOffset: number,
|
||||
context: SymbolsTable
|
||||
): ExpressionAutocompleteResults {
|
||||
const suggestions = this.#autocompleter.getSuggestions(expr, cursorOffset, context);
|
||||
|
||||
return { suggestions };
|
||||
}
|
||||
|
||||
public generate(_node: Expression): string {
|
||||
throw new Error('Not yet implemented');
|
||||
}
|
||||
}
|
||||
|
||||
function createExpressionErrorFromSyntaxError(
|
||||
code: string,
|
||||
error: SyntaxError & { loc?: Position }
|
||||
): ExpressionError {
|
||||
const loc = error.loc;
|
||||
|
||||
if (!loc) {
|
||||
return new ExpressionError(error.message);
|
||||
}
|
||||
|
||||
const errorMessage = `${error.message.replace(/\s*\(\d+:\d+\)$/, '')} at ${code.split('\n').length > 1 ? `line ${loc.line}, ` : ''}char ${loc.column}`;
|
||||
const token = getTokenAtLoc(code, loc);
|
||||
|
||||
if (!token) {
|
||||
return new ExpressionError(errorMessage, loc);
|
||||
}
|
||||
|
||||
return new ExpressionError(errorMessage, loc, token);
|
||||
}
|
||||
function getTokenAtLoc(code: string, errorLoc: Position): Token | undefined {
|
||||
const tokens = tokenizer(code, {
|
||||
ecmaVersion: 'latest',
|
||||
locations: true,
|
||||
});
|
||||
|
||||
try {
|
||||
for (const token of tokens) {
|
||||
if (!token.loc) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { start, end } = token.loc;
|
||||
|
||||
const onSameLine = errorLoc.line === start.line;
|
||||
const inColumnRange = errorLoc.column >= start.column && errorLoc.column < end.column;
|
||||
|
||||
if (onSameLine && inColumnRange) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
} catch (_error) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function isParsedExpressionStatement(
|
||||
statement: Program['body'][number]
|
||||
): statement is ExpressionStatement {
|
||||
return statement.type === 'ExpressionStatement';
|
||||
}
|
||||
|
||||
export function filterOutModuleDeclarationStatement(
|
||||
statement: Program['body'][number]
|
||||
): statement is ExpressionStatement {
|
||||
return ![
|
||||
'ImportDeclaration',
|
||||
'ExportNamedDeclaration',
|
||||
'ExportDefaultDeclaration',
|
||||
'ExportAllDeclaration',
|
||||
].includes(statement.type);
|
||||
}
|
||||
@@ -0,0 +1,497 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { SymbolArray, SymbolObject, SymbolString } from '../symbols';
|
||||
import { SymbolsTable } from '../symbols-table';
|
||||
import type { SymbolType } from '../types';
|
||||
|
||||
describe('ExpressionRuntime', () => {
|
||||
const initialSymbols = {
|
||||
visitor: SymbolObject({
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: SymbolObject({
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: SymbolString({ name: 'key' }),
|
||||
flags: SymbolObject({
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: SymbolString({ name: 'FLAG1' }),
|
||||
FLAG2: SymbolString({ name: 'FLAG2' }),
|
||||
FLAG3: SymbolString({ name: 'FLAG3' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
hello: SymbolArray({
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: SymbolString(),
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
};
|
||||
describe('addSymbols', () => {
|
||||
it('should the symbols matching the provided object to the table', () => {
|
||||
const symbolsTable = new SymbolsTable(initialSymbols);
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'object',
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
hello: {
|
||||
type: 'array',
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
ref: 'visitor',
|
||||
childrenRefs: ['visitor.claims'],
|
||||
});
|
||||
|
||||
symbolsTable.addSymbols({
|
||||
space: SymbolObject({
|
||||
name: 'space',
|
||||
properties: {
|
||||
id: SymbolString({ name: 'id' }),
|
||||
title: SymbolString({ name: 'title' }),
|
||||
},
|
||||
methods: [],
|
||||
}),
|
||||
});
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['space'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'object',
|
||||
name: 'space',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
name: 'id',
|
||||
},
|
||||
title: {
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
ref: 'space',
|
||||
childrenRefs: ['space.id', 'space.title'],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Symbols standard library', () => {
|
||||
it('should allow to access methods & properties defined as part of the standard library', () => {
|
||||
const symbolsTable = new SymbolsTable({
|
||||
id: SymbolString({ name: 'id' }),
|
||||
title: SymbolString({ name: 'title' }),
|
||||
});
|
||||
|
||||
expect(
|
||||
symbolsTable.getSymbolInfo<SymbolType.String>('id')?.definition.properties.length
|
||||
).toMatchObject({
|
||||
type: 'number',
|
||||
name: 'length',
|
||||
description:
|
||||
'The length data property of a String value contains the length of the string in UTF-16 code units.',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSymbolInfo', () => {
|
||||
it('should add the symbols matching the initial symbol definition passed to the constructor', () => {
|
||||
const symbolsTable = new SymbolsTable(initialSymbols);
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'object',
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
hello: {
|
||||
type: 'array',
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
ref: 'visitor',
|
||||
childrenRefs: ['visitor.claims'],
|
||||
});
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor', 'claims'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'object',
|
||||
name: 'claims',
|
||||
description: 'The claims contained in the visitor JWT token',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
hello: {
|
||||
type: 'array',
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
ref: 'visitor.claims',
|
||||
parentRef: 'visitor',
|
||||
childrenRefs: [
|
||||
'visitor.claims.key',
|
||||
'visitor.claims.flags',
|
||||
'visitor.claims.hello',
|
||||
],
|
||||
});
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor', 'claims', 'key'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
ref: 'visitor.claims.key',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.key.length',
|
||||
'visitor.claims.key.at',
|
||||
'visitor.claims.key.endsWith',
|
||||
'visitor.claims.key.includes',
|
||||
],
|
||||
});
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor', 'claims', 'flags'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
ref: 'visitor.claims.flags',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1',
|
||||
'visitor.claims.flags.FLAG2',
|
||||
'visitor.claims.flags.FLAG3',
|
||||
],
|
||||
});
|
||||
|
||||
expect(
|
||||
symbolsTable.getSymbolInfo(['visitor', 'claims', 'flags', 'FLAG1'])
|
||||
).toMatchObject({
|
||||
definition: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
ref: 'visitor.claims.flags.FLAG1',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG1.length',
|
||||
'visitor.claims.flags.FLAG1.at',
|
||||
'visitor.claims.flags.FLAG1.endsWith',
|
||||
'visitor.claims.flags.FLAG1.includes',
|
||||
],
|
||||
});
|
||||
|
||||
expect(
|
||||
symbolsTable.getSymbolInfo(['visitor', 'claims', 'flags', 'FLAG2'])
|
||||
).toMatchObject({
|
||||
definition: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
ref: 'visitor.claims.flags.FLAG2',
|
||||
parentRef: 'visitor.claims.flags',
|
||||
childrenRefs: [
|
||||
'visitor.claims.flags.FLAG2.length',
|
||||
'visitor.claims.flags.FLAG2.at',
|
||||
'visitor.claims.flags.FLAG2.endsWith',
|
||||
'visitor.claims.flags.FLAG2.includes',
|
||||
],
|
||||
});
|
||||
|
||||
expect(symbolsTable.getSymbolInfo(['visitor', 'claims', 'hello'])).toMatchObject({
|
||||
definition: {
|
||||
type: 'array',
|
||||
name: 'hello',
|
||||
description: 'An array of string',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
ref: 'visitor.claims.hello',
|
||||
parentRef: 'visitor.claims',
|
||||
childrenRefs: [
|
||||
'visitor.claims.hello.length',
|
||||
'visitor.claims.hello.at',
|
||||
'visitor.claims.hello.includes',
|
||||
'visitor.claims.hello.some',
|
||||
'visitor.claims.hello.every',
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('inferSymbolFromValue', () => {
|
||||
it('should infer properly a symbol based on a value', () => {
|
||||
const symbolDef = SymbolsTable.inferSymbolFromValue(
|
||||
{
|
||||
visitor: {
|
||||
claims: {
|
||||
key: 'test',
|
||||
flags: {
|
||||
FLAG1: 'testflag1',
|
||||
FLAG2: 'testflag2',
|
||||
FLAG3: 'testflag3',
|
||||
},
|
||||
hello: ['test', 'test1', 'test2'],
|
||||
},
|
||||
},
|
||||
},
|
||||
'context'
|
||||
);
|
||||
expect(symbolDef).toMatchObject({
|
||||
type: 'object',
|
||||
name: 'context',
|
||||
properties: {
|
||||
visitor: {
|
||||
type: 'object',
|
||||
name: 'visitor',
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
name: 'claims',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
hello: {
|
||||
type: 'array',
|
||||
name: 'hello',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('inferSymbolFromJSONSchema', () => {
|
||||
it('should infer properly a symbol table based on a JSON schema', () => {
|
||||
const symbolDef = SymbolsTable.inferSymbolFromJSONSchema(
|
||||
{
|
||||
type: 'object',
|
||||
description: `The attributes tied to a site's visitor.`,
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
description: 'The user feature flags',
|
||||
properties: {
|
||||
FLAG1: { type: 'string' },
|
||||
FLAG2: { type: 'string' },
|
||||
FLAG3: { type: 'string' },
|
||||
},
|
||||
},
|
||||
hello: {
|
||||
type: 'string',
|
||||
enum: ['test', 'test1', 'test2'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'visitor'
|
||||
);
|
||||
expect(symbolDef).toMatchObject({
|
||||
type: 'object',
|
||||
name: 'visitor',
|
||||
description: `The attributes tied to a site's visitor.`,
|
||||
properties: {
|
||||
claims: {
|
||||
type: 'object',
|
||||
name: 'claims',
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
name: 'key',
|
||||
},
|
||||
flags: {
|
||||
type: 'object',
|
||||
name: 'flags',
|
||||
description: 'The user feature flags',
|
||||
properties: {
|
||||
FLAG1: {
|
||||
type: 'string',
|
||||
name: 'FLAG1',
|
||||
},
|
||||
FLAG2: {
|
||||
type: 'string',
|
||||
name: 'FLAG2',
|
||||
},
|
||||
FLAG3: {
|
||||
type: 'string',
|
||||
name: 'FLAG3',
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
hello: {
|
||||
type: 'string',
|
||||
enum: ['test', 'test1', 'test2'],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
},
|
||||
},
|
||||
methods: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './symbols';
|
||||
export * from './symbols-table';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,350 @@
|
||||
import type { JSONSchema7 } from 'json-schema';
|
||||
|
||||
import { filterOutNullable } from '../utils';
|
||||
import {
|
||||
SymbolArray,
|
||||
SymbolBoolean,
|
||||
SymbolNull,
|
||||
SymbolNumber,
|
||||
SymbolObject,
|
||||
SymbolString,
|
||||
SymbolUndefined,
|
||||
} from './symbols';
|
||||
import {
|
||||
type ExtractSymbolDef,
|
||||
type GenericSymbolDef,
|
||||
type ObjectSymbolDef,
|
||||
SymbolType,
|
||||
type SymbolWithMethods,
|
||||
type SymbolWithProperties,
|
||||
resolveSymbolDef,
|
||||
} from './types';
|
||||
|
||||
export interface SymbolInfo<T extends SymbolType = SymbolType> {
|
||||
/**
|
||||
* Definition of the symbol.
|
||||
*/
|
||||
definition: ExtractSymbolDef<T>;
|
||||
|
||||
/**
|
||||
* Reference of the symbol in the table of symbols.
|
||||
*/
|
||||
ref: string;
|
||||
|
||||
/**
|
||||
* Stores the reference to the parent symbol.
|
||||
*/
|
||||
parentRef?: string;
|
||||
|
||||
/**
|
||||
* Stores the reference to the children symbols.
|
||||
*/
|
||||
childrenRefs?: string[];
|
||||
}
|
||||
|
||||
export class SymbolError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, SymbolError);
|
||||
}
|
||||
this.name = 'SymbolError';
|
||||
}
|
||||
}
|
||||
|
||||
export class SymbolsTable {
|
||||
/**
|
||||
* Internal table that keeps track of all symbols reference.
|
||||
*/
|
||||
#table: Record<string, SymbolInfo>;
|
||||
|
||||
/**
|
||||
* Internal table that keep track of the raw symbols definitions.
|
||||
*/
|
||||
#rawSymbols: Record<string, GenericSymbolDef>;
|
||||
|
||||
constructor(initialContext: Record<string, GenericSymbolDef> = {}) {
|
||||
this.#table = {};
|
||||
this.#rawSymbols = {};
|
||||
this.addSymbols(initialContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new symbols table by merging the current one with the provided one.
|
||||
*/
|
||||
merge(other: SymbolsTable): SymbolsTable {
|
||||
return new SymbolsTable({
|
||||
...this.#rawSymbols,
|
||||
...other.#rawSymbols,
|
||||
});
|
||||
}
|
||||
|
||||
toString() {
|
||||
return JSON.stringify(this.#table, null, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Infer the symbol of a value and generate the appropriate symbol definition.
|
||||
*/
|
||||
static inferSymbolFromValue(value: unknown, name?: string): ExtractSymbolDef<SymbolType> {
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length === 0) {
|
||||
return SymbolArray({ items: SymbolUndefined() });
|
||||
}
|
||||
|
||||
const firstItemSymbol = SymbolsTable.inferSymbolFromValue(value.at(0));
|
||||
// Check that the array is not a mixin of different items types.
|
||||
if (value.length > 1) {
|
||||
const secondItemSymbol = SymbolsTable.inferSymbolFromValue(value.at(1));
|
||||
if (firstItemSymbol.type !== secondItemSymbol.type) {
|
||||
throw new SymbolError('Array with mixin items types are not supported');
|
||||
}
|
||||
}
|
||||
return SymbolArray({ name, items: firstItemSymbol });
|
||||
}
|
||||
|
||||
if (typeof value === 'undefined') {
|
||||
return SymbolUndefined({ name });
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
return SymbolNull({ name });
|
||||
}
|
||||
|
||||
const valueType = typeof value;
|
||||
switch (valueType) {
|
||||
case 'string':
|
||||
return SymbolString({ name });
|
||||
case 'number':
|
||||
return SymbolNumber({ name });
|
||||
case 'boolean':
|
||||
return SymbolBoolean({ name });
|
||||
case 'object': {
|
||||
const properties = Object.entries(value).reduce<Record<string, GenericSymbolDef>>(
|
||||
(prev, [name, val]) => {
|
||||
prev[name] = SymbolsTable.inferSymbolFromValue(val, name);
|
||||
return prev;
|
||||
},
|
||||
{}
|
||||
);
|
||||
return SymbolObject({ name, properties, methods: [] });
|
||||
}
|
||||
default:
|
||||
throw new SymbolError(`Unsupported symbol type ${valueType}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Infer a table of symbol based on a JSON schema object describing it.
|
||||
*/
|
||||
static inferSymbolFromJSONSchema(
|
||||
schema: JSONSchema7,
|
||||
name?: string
|
||||
): ExtractSymbolDef<SymbolType> {
|
||||
switch (schema.type) {
|
||||
case 'string':
|
||||
return SymbolString({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
...(schema.enum
|
||||
? {
|
||||
enum: schema.enum
|
||||
.filter(filterOutNullable)
|
||||
.map((enumValue) => enumValue.toString()),
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return SymbolNumber({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
});
|
||||
case 'boolean':
|
||||
return SymbolBoolean({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
});
|
||||
case 'null':
|
||||
return SymbolNull({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
});
|
||||
case 'object':
|
||||
return SymbolsTable.#buildObjectSymbolFromJSONSchemaObject(schema, name);
|
||||
case 'array':
|
||||
return SymbolsTable.#buildArraySymbolFromJSONSchemaArray(schema, name);
|
||||
default:
|
||||
throw new Error(`Unsupported schema type: ${schema.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
static #buildObjectSymbolFromJSONSchemaObject(
|
||||
schema: JSONSchema7,
|
||||
name?: string
|
||||
): ObjectSymbolDef {
|
||||
const properties: Record<string, GenericSymbolDef> = {};
|
||||
|
||||
if (schema.properties) {
|
||||
Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => {
|
||||
properties[propertyName] = SymbolsTable.inferSymbolFromJSONSchema(
|
||||
propertySchema as JSONSchema7,
|
||||
propertyName
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return SymbolObject({
|
||||
name,
|
||||
properties,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
methods: [],
|
||||
});
|
||||
}
|
||||
|
||||
static #buildArraySymbolFromJSONSchemaArray(
|
||||
schema: JSONSchema7,
|
||||
name?: string
|
||||
): ExtractSymbolDef<SymbolType.Array> {
|
||||
if (schema.items) {
|
||||
const itemSymbol = SymbolsTable.inferSymbolFromJSONSchema(
|
||||
schema.items as JSONSchema7,
|
||||
`${name || ''}_item`
|
||||
);
|
||||
return SymbolArray({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
items: itemSymbol,
|
||||
});
|
||||
}
|
||||
|
||||
return SymbolArray({
|
||||
name,
|
||||
...(schema.description ? { description: schema.description } : {}),
|
||||
items: SymbolUndefined(),
|
||||
});
|
||||
}
|
||||
|
||||
private generateSymbolRefPath(path: string[]): string {
|
||||
return path.join('.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single symbol to the table at the provided path.
|
||||
*/
|
||||
private addSymbol(path: string[], definition: GenericSymbolDef, raw = false): void {
|
||||
const fullPath = this.generateSymbolRefPath(path);
|
||||
const parentPath = path.slice(0, -1).join('.');
|
||||
|
||||
if (this.#table[fullPath]) {
|
||||
throw new SymbolError(`Symbol "${fullPath}" already exists.`);
|
||||
}
|
||||
|
||||
if (raw) {
|
||||
this.#rawSymbols[fullPath] = definition;
|
||||
}
|
||||
|
||||
// Add the new symbol linking it to its parent
|
||||
this.#table[fullPath] = {
|
||||
definition: resolveSymbolDef(definition),
|
||||
ref: fullPath,
|
||||
parentRef: parentPath || undefined,
|
||||
childrenRefs: [],
|
||||
};
|
||||
|
||||
if (parentPath && this.#table[parentPath]) {
|
||||
if (this.#table[parentPath].childrenRefs) {
|
||||
this.#table[parentPath].childrenRefs.push(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Add any nested symbols if the value is a symbol with properties...
|
||||
if (isSymbolWithProperties(definition)) {
|
||||
Object.entries(definition.properties).forEach(([propKey, propSymbol]) => {
|
||||
if (isObjectSymbol(propSymbol)) {
|
||||
this.addSymbols({ [propKey]: propSymbol }, path, false);
|
||||
} else {
|
||||
this.addSymbol([...path, propKey], propSymbol, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ...or a symbol with methods
|
||||
if (isSymbolWithMethods(definition)) {
|
||||
definition.methods.forEach((methodSymbol) => {
|
||||
this.addSymbol([...path, methodSymbol.name], methodSymbol, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the provided object of symbols definitions to the symbol table.
|
||||
*/
|
||||
public addSymbols(
|
||||
symbols: Record<string, GenericSymbolDef>,
|
||||
prefix: string[] = [],
|
||||
raw = true
|
||||
): void {
|
||||
for (const [key, symbolDef] of Object.entries(symbols)) {
|
||||
const path = [...prefix, key];
|
||||
|
||||
// Add the current symbol to the table.
|
||||
this.addSymbol(path, symbolDef, raw);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a symbol's information using its path in the table.
|
||||
*/
|
||||
public getSymbolInfo<T extends SymbolType>(path: string | string[]): SymbolInfo<T> | undefined {
|
||||
const key = Array.isArray(path) ? path.join('.') : path;
|
||||
const info = this.#table[key];
|
||||
return info ? typedSymbolInfo(info) : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all symbol keys matching the pattern defined by the provided path.
|
||||
*/
|
||||
public getMatchingSymbolsKeys(path: string[]): string[] {
|
||||
const wildcardRegex = new RegExp(
|
||||
`^${path
|
||||
.map((segment) => {
|
||||
if (segment.includes('*')) {
|
||||
return `${segment.split('*')[0]}([^.]+)?`;
|
||||
}
|
||||
return segment;
|
||||
})
|
||||
.join('\\.')}$`
|
||||
);
|
||||
|
||||
return Object.keys(this.#table)
|
||||
.filter((key) => wildcardRegex.test(key))
|
||||
.filter(filterOutNullable);
|
||||
}
|
||||
}
|
||||
|
||||
function isObjectSymbol(symbol: GenericSymbolDef): symbol is ObjectSymbolDef {
|
||||
return symbol.type === SymbolType.Object;
|
||||
}
|
||||
|
||||
function isSymbolWithProperties(symbol: GenericSymbolDef): symbol is SymbolWithProperties {
|
||||
return (
|
||||
symbol.type === SymbolType.Object ||
|
||||
symbol.type === SymbolType.Array ||
|
||||
symbol.type === SymbolType.String
|
||||
);
|
||||
}
|
||||
|
||||
function isSymbolWithMethods(symbol: GenericSymbolDef): symbol is SymbolWithMethods {
|
||||
return (
|
||||
symbol.type === SymbolType.Object ||
|
||||
symbol.type === SymbolType.Array ||
|
||||
symbol.type === SymbolType.String
|
||||
);
|
||||
}
|
||||
|
||||
function typedSymbolInfo<T extends SymbolType>(info: SymbolInfo): SymbolInfo<T> {
|
||||
const definition = resolveSymbolDef(info.definition);
|
||||
return { ...info, definition } as SymbolInfo<T>;
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
import {
|
||||
type ArraySymbolDef,
|
||||
type BooleanSymbolDef,
|
||||
type ExtractSymbolDef,
|
||||
type FunctionSymbolDef,
|
||||
type GenericSymbolDef,
|
||||
type NullSymbolDef,
|
||||
type NumberSymbolDef,
|
||||
type ObjectSymbolDef,
|
||||
type StringSymbolDef,
|
||||
SymbolType,
|
||||
type SymbolsWithPropertiesAndMethods,
|
||||
type UndefinedSymbolDef,
|
||||
type UnionSymbolDef,
|
||||
} from './types';
|
||||
|
||||
export function SymbolBoolean(args: Omit<BooleanSymbolDef, 'type'> = {}): BooleanSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Boolean,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolNumber(args: Omit<NumberSymbolDef, 'type'> = {}): NumberSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Number,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolString(
|
||||
args: Omit<StringSymbolDef, 'type' | 'methods' | 'properties'> = {}
|
||||
): StringSymbolDef {
|
||||
return createSymbolWithPropertiesAndMethods<StringSymbolDef>(SymbolType.String, args);
|
||||
}
|
||||
|
||||
export function SymbolObject(args: Omit<ObjectSymbolDef, 'type'>): ObjectSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Object,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolArray(
|
||||
args: Omit<ArraySymbolDef, 'type' | 'methods' | 'properties'>
|
||||
): ArraySymbolDef {
|
||||
return createSymbolWithPropertiesAndMethods(SymbolType.Array, args);
|
||||
}
|
||||
|
||||
export function SymbolFunction(args: Omit<FunctionSymbolDef, 'type'>): FunctionSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Function,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function OptionalFunctionArg(
|
||||
optionalArg: ExtractSymbolDef<SymbolType>
|
||||
): ExtractSymbolDef<SymbolType> & { optional: true } {
|
||||
return {
|
||||
...optionalArg,
|
||||
optional: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolUnion(args: Omit<UnionSymbolDef, 'type'>): UnionSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Union,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolUndefined(args: Omit<UndefinedSymbolDef, 'type'> = {}): UndefinedSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Undefined,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
export function SymbolNull(args: Omit<NullSymbolDef, 'type'> = {}): NullSymbolDef {
|
||||
return {
|
||||
type: SymbolType.Null,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
function createSymbolWithPropertiesAndMethods<
|
||||
T extends SymbolsWithPropertiesAndMethods & { type: SymbolType },
|
||||
>(type: T['type'], args: Omit<T, 'type' | 'methods' | 'properties'>): T {
|
||||
const symbol = { type, ...args } as T;
|
||||
|
||||
Object.defineProperty(symbol, 'properties', {
|
||||
get() {
|
||||
if (symbol.type === SymbolType.Array) {
|
||||
return isArraySymbol(symbol)
|
||||
? StandardLibrary[SymbolType.Array]?.(symbol).properties
|
||||
: {};
|
||||
}
|
||||
return StandardLibrary[symbol.type]?.properties || {};
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(symbol, 'methods', {
|
||||
get() {
|
||||
if (symbol.type === SymbolType.Array) {
|
||||
return isArraySymbol(symbol)
|
||||
? StandardLibrary[SymbolType.Array]?.(symbol).methods
|
||||
: [];
|
||||
}
|
||||
return StandardLibrary[symbol.type]?.methods || [];
|
||||
},
|
||||
});
|
||||
|
||||
return symbol;
|
||||
}
|
||||
|
||||
// TODO-ADAPTIVE-CONTENT: extend the definition of the supported standard library methods and properties.
|
||||
|
||||
const StandardLibrary: Partial<
|
||||
{
|
||||
[key in Exclude<SymbolType, SymbolType.Array>]: {
|
||||
properties: Record<string, GenericSymbolDef>;
|
||||
methods: FunctionSymbolDef[];
|
||||
};
|
||||
} & {
|
||||
[SymbolType.Array]: (symbol: ArraySymbolDef) => {
|
||||
properties: Record<string, GenericSymbolDef>;
|
||||
methods: FunctionSymbolDef[];
|
||||
};
|
||||
}
|
||||
> = {
|
||||
[SymbolType.String]: {
|
||||
properties: {
|
||||
length: SymbolNumber({
|
||||
name: 'length',
|
||||
description:
|
||||
'The length data property of a String value contains the length of the string in UTF-16 code units.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length',
|
||||
}),
|
||||
},
|
||||
methods: [
|
||||
SymbolFunction({
|
||||
name: 'at',
|
||||
description: `Takes an integer value and returns the item at that index, allowing for positive and negative integers.
|
||||
Negative integers count back from the last item in the string.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at',
|
||||
args: [
|
||||
SymbolNumber({
|
||||
name: 'index',
|
||||
description: 'The index (position) of the string character to be returned',
|
||||
}),
|
||||
],
|
||||
returns: SymbolUnion({
|
||||
description: `A String consisting of the single UTF-16 code unit located at the specified position.
|
||||
Returns undefined if the given index can not be found.`,
|
||||
members: [SymbolString(), SymbolUndefined()],
|
||||
}),
|
||||
}),
|
||||
SymbolFunction({
|
||||
name: 'endsWith',
|
||||
description: `Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding
|
||||
elements of this object (converted to a String) starting at endPosition – length(this). Otherwise returns false.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith',
|
||||
args: [
|
||||
SymbolString({
|
||||
name: 'searchString',
|
||||
description: `The characters to be searched for at the end of str. Cannot be a regex.
|
||||
All values that are not regexes are coerced to strings, so omitting it or passing undefined causes endsWith() to search for
|
||||
the string "undefined", which is rarely what you want.`,
|
||||
}),
|
||||
OptionalFunctionArg(
|
||||
SymbolNumber({
|
||||
name: 'endPosition',
|
||||
description: `The end position at which searchString is expected to be found
|
||||
(the index of searchString's last character plus 1). Defaults to str.length.`,
|
||||
})
|
||||
),
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description: `true if the given characters are found at the end of the string, including when searchString is an empty string;
|
||||
otherwise, false.`,
|
||||
}),
|
||||
}),
|
||||
SymbolFunction({
|
||||
name: 'includes',
|
||||
description: `Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions
|
||||
that are greater than or equal to position; otherwise, returns false.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes',
|
||||
args: [
|
||||
SymbolString({
|
||||
name: 'searchString',
|
||||
description: `A string to be searched for within str. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it
|
||||
or passing undefined causes includes() to search for the string "undefined", which is rarely what you want.`,
|
||||
}),
|
||||
OptionalFunctionArg(
|
||||
SymbolNumber({
|
||||
name: 'position',
|
||||
description:
|
||||
'The position within the string at which to begin searching for searchString. (Defaults to 0.)',
|
||||
})
|
||||
),
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description: `true if the search string is found anywhere within the given string, including when searchString is an empty string;
|
||||
otherwise, false.`,
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
[SymbolType.Array]: (arraySymbolDef: ArraySymbolDef) => ({
|
||||
properties: {
|
||||
length: SymbolNumber({
|
||||
name: 'length',
|
||||
description: `The length data property of an Array instance represents the number of elements in that array.
|
||||
The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length',
|
||||
}),
|
||||
},
|
||||
methods: [
|
||||
SymbolFunction({
|
||||
name: 'at',
|
||||
description: `Takes an integer value and returns the item at that index, allowing for positive and negative integers.
|
||||
Negative integers count back from the last item in the array.`,
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at',
|
||||
args: [
|
||||
SymbolNumber({
|
||||
name: 'index',
|
||||
description: `Zero-based index of the array element to be returned, converted to an integer.
|
||||
Negative index counts back from the end of the array — if index < 0, index + array.length is accessed.`,
|
||||
}),
|
||||
],
|
||||
returns: SymbolUnion({
|
||||
description: `The element in the array matching the given index. Always returns undefined if index < -array.length or index >= array.length
|
||||
without attempting to access the corresponding property.`,
|
||||
members: [arraySymbolDef.items, SymbolUndefined()],
|
||||
}),
|
||||
}),
|
||||
SymbolFunction({
|
||||
name: 'includes',
|
||||
description:
|
||||
'Determines whether an array includes a certain value among its entries, returning true or false as appropriate.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes',
|
||||
args: [
|
||||
{
|
||||
...arraySymbolDef.items,
|
||||
name: 'searchElement',
|
||||
description: 'The value to be searched for within the array.',
|
||||
},
|
||||
OptionalFunctionArg(
|
||||
SymbolNumber({
|
||||
name: 'fromIndex',
|
||||
description:
|
||||
'The position within the string at which to begin searching for searchString. (Defaults to 0.)',
|
||||
})
|
||||
),
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description:
|
||||
'true if the value searchElement is found within the array (or the part of the array indicated by the index fromIndex, if specified).',
|
||||
}),
|
||||
}),
|
||||
SymbolFunction({
|
||||
name: 'some',
|
||||
description:
|
||||
'Tests whether at least one element in the array passes the test implemented by the provided function.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some',
|
||||
args: [
|
||||
SymbolFunction({
|
||||
name: 'callback',
|
||||
description: 'A function that tests each element of the array.',
|
||||
args: [
|
||||
{
|
||||
...arraySymbolDef.items,
|
||||
name: 'element',
|
||||
description: 'The current element being processed in the array.',
|
||||
},
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description:
|
||||
'true if the callback function returns a truthy value for at least one element in the array.',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description:
|
||||
'true if the callback function returns a truthy value for at least one element in the array.',
|
||||
}),
|
||||
}),
|
||||
SymbolFunction({
|
||||
name: 'every',
|
||||
description:
|
||||
'Tests whether all elements in the array pass the test implemented by the provided function.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every',
|
||||
args: [
|
||||
SymbolFunction({
|
||||
name: 'callback',
|
||||
description: 'A function that tests each element of the array.',
|
||||
args: [
|
||||
{
|
||||
...arraySymbolDef.items,
|
||||
name: 'element',
|
||||
description: 'The current element being processed in the array.',
|
||||
},
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description:
|
||||
'true if the callback function returns a truthy value for all elements in the array.',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
returns: SymbolBoolean({
|
||||
description:
|
||||
'true if the callback function returns a truthy value for all elements in the array.',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
};
|
||||
|
||||
function isArraySymbol(symbol: GenericSymbolDef): symbol is ArraySymbolDef {
|
||||
return symbol.type === SymbolType.Array;
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
import type { MandateProps } from '../utils';
|
||||
|
||||
export enum SymbolType {
|
||||
Boolean = 'boolean',
|
||||
Number = 'number',
|
||||
String = 'string',
|
||||
Object = 'object',
|
||||
Array = 'array',
|
||||
Function = 'function',
|
||||
Union = 'union',
|
||||
Undefined = 'undefined',
|
||||
Null = 'null',
|
||||
}
|
||||
|
||||
export interface SymbolMetadata {
|
||||
/**
|
||||
* Long description of the symbol.
|
||||
*/
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* Link to a documentation/manual page.
|
||||
*/
|
||||
link?: string;
|
||||
}
|
||||
|
||||
export interface GenericSymbolDef extends SymbolMetadata {
|
||||
/**
|
||||
* Type of the symbol.
|
||||
*/
|
||||
type: SymbolType;
|
||||
|
||||
/**
|
||||
* Name of the symbol.
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
export interface SymbolWithProperties extends GenericSymbolDef {
|
||||
/**
|
||||
* Properties on the symbol type.
|
||||
*/
|
||||
properties: Record<string, GenericSymbolDef>;
|
||||
}
|
||||
|
||||
export interface SymbolWithMethods extends GenericSymbolDef {
|
||||
/**
|
||||
* Methods that can be called on the symbol type.
|
||||
*/
|
||||
methods: FunctionSymbolDef[];
|
||||
}
|
||||
|
||||
export interface BooleanSymbolDef extends GenericSymbolDef {
|
||||
type: SymbolType.Boolean;
|
||||
}
|
||||
|
||||
export interface NumberSymbolDef extends GenericSymbolDef {
|
||||
type: SymbolType.Number;
|
||||
}
|
||||
|
||||
export interface StringSymbolDef extends SymbolWithProperties, SymbolWithMethods {
|
||||
type: SymbolType.String;
|
||||
|
||||
/**
|
||||
* Set of enumerated values that the string symbol is retristred to.
|
||||
*/
|
||||
enum?: string[];
|
||||
|
||||
/**
|
||||
* Properties on strings.
|
||||
*/
|
||||
properties: {
|
||||
length: NumberSymbolDef;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ObjectSymbolDef extends SymbolWithProperties, SymbolWithMethods {
|
||||
type: SymbolType.Object;
|
||||
}
|
||||
|
||||
export interface ArraySymbolDef extends SymbolWithProperties, SymbolWithMethods {
|
||||
type: SymbolType.Array;
|
||||
|
||||
/**
|
||||
* Symbol representing the type of the items of the array
|
||||
*/
|
||||
items: ExtractSymbolDef<SymbolType>;
|
||||
|
||||
/**
|
||||
* Properties on arrays.
|
||||
*/
|
||||
properties: {
|
||||
length: NumberSymbolDef;
|
||||
};
|
||||
}
|
||||
|
||||
export interface FunctionSymbolDef extends MandateProps<GenericSymbolDef, 'name'> {
|
||||
type: SymbolType.Function;
|
||||
|
||||
/**
|
||||
* Symbols describing the arguments of the function.
|
||||
*/
|
||||
args: (ExtractSymbolDef<SymbolType> & { optional?: boolean })[];
|
||||
|
||||
/**
|
||||
* Symbol describing the returned value of the function.
|
||||
*/
|
||||
returns: ExtractSymbolDef<SymbolType>;
|
||||
}
|
||||
|
||||
export interface UnionSymbolDef extends GenericSymbolDef {
|
||||
type: SymbolType.Union;
|
||||
|
||||
/**
|
||||
* Symbols composing the union.
|
||||
*/
|
||||
members: ExtractSymbolDef<SymbolType>[];
|
||||
}
|
||||
|
||||
export interface UndefinedSymbolDef extends GenericSymbolDef {
|
||||
type: SymbolType.Undefined;
|
||||
}
|
||||
|
||||
export interface NullSymbolDef extends GenericSymbolDef {
|
||||
type: SymbolType.Null;
|
||||
}
|
||||
|
||||
export type SymbolsWithPropertiesAndMethods = ArraySymbolDef | ObjectSymbolDef | StringSymbolDef;
|
||||
|
||||
export type ExtractSymbolDef<T extends SymbolType> = T extends SymbolType.String
|
||||
? StringSymbolDef
|
||||
: T extends SymbolType.Number
|
||||
? NumberSymbolDef
|
||||
: T extends SymbolType.Boolean
|
||||
? BooleanSymbolDef
|
||||
: T extends SymbolType.Array
|
||||
? ArraySymbolDef
|
||||
: T extends SymbolType.Object
|
||||
? ObjectSymbolDef
|
||||
: T extends SymbolType.Function
|
||||
? FunctionSymbolDef
|
||||
: T extends SymbolType.Union
|
||||
? UnionSymbolDef
|
||||
: T extends SymbolType.Undefined
|
||||
? UndefinedSymbolDef
|
||||
: T extends SymbolType.Null
|
||||
? NullSymbolDef
|
||||
: never;
|
||||
|
||||
export function resolveSymbolDef(
|
||||
symbol: GenericSymbolDef
|
||||
):
|
||||
| StringSymbolDef
|
||||
| NumberSymbolDef
|
||||
| BooleanSymbolDef
|
||||
| ArraySymbolDef
|
||||
| ObjectSymbolDef
|
||||
| FunctionSymbolDef
|
||||
| UnionSymbolDef
|
||||
| UndefinedSymbolDef
|
||||
| NullSymbolDef {
|
||||
switch (symbol.type) {
|
||||
case SymbolType.String:
|
||||
return symbol as StringSymbolDef;
|
||||
|
||||
case SymbolType.Number:
|
||||
return symbol as NumberSymbolDef;
|
||||
|
||||
case SymbolType.Boolean:
|
||||
return symbol as BooleanSymbolDef;
|
||||
|
||||
case SymbolType.Array:
|
||||
return symbol as ArraySymbolDef;
|
||||
|
||||
case SymbolType.Object:
|
||||
return symbol as ObjectSymbolDef;
|
||||
|
||||
case SymbolType.Function:
|
||||
return symbol as FunctionSymbolDef;
|
||||
|
||||
case SymbolType.Union:
|
||||
return symbol as UnionSymbolDef;
|
||||
|
||||
case SymbolType.Undefined:
|
||||
return symbol as UndefinedSymbolDef;
|
||||
|
||||
case SymbolType.Null:
|
||||
return symbol as NullSymbolDef;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown symbol type: ${symbol.type}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
export type TemplateText = {
|
||||
type: 'text';
|
||||
value: string;
|
||||
start: number;
|
||||
end: number;
|
||||
};
|
||||
|
||||
export type TemplateExpression = {
|
||||
type: 'expression';
|
||||
value: string;
|
||||
start: number; // Start index of the expression content (after `{{`)
|
||||
end: number; // End index of the expression content (before `}}`)
|
||||
};
|
||||
|
||||
export type TemplatePart = TemplateText | TemplateExpression;
|
||||
|
||||
/**
|
||||
* Parse a template string containing `{{ expression }}` placeholders.
|
||||
*/
|
||||
export function parseTemplate(template: string): TemplatePart[] {
|
||||
const parts: TemplatePart[] = [];
|
||||
const regex = /\{\{(.*?)\}\}/gs;
|
||||
let lastIndex = 0;
|
||||
|
||||
for (const match of template.matchAll(regex)) {
|
||||
const matchStart = match.index ?? 0;
|
||||
const matchEnd = matchStart + match[0].length;
|
||||
|
||||
if (matchStart > lastIndex) {
|
||||
parts.push({
|
||||
type: 'text',
|
||||
value: template.slice(lastIndex, matchStart),
|
||||
start: lastIndex,
|
||||
end: matchStart,
|
||||
});
|
||||
}
|
||||
|
||||
parts.push({
|
||||
type: 'expression',
|
||||
value: (match[1] ?? '').trim(),
|
||||
start: matchStart + 2,
|
||||
end: matchEnd - 2,
|
||||
});
|
||||
lastIndex = matchEnd;
|
||||
}
|
||||
|
||||
if (lastIndex < template.length) {
|
||||
parts.push({
|
||||
type: 'text',
|
||||
value: template.slice(lastIndex),
|
||||
start: lastIndex,
|
||||
end: template.length,
|
||||
});
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import type { BinaryOperator, Expression, ExpressionStatement, LogicalOperator } from 'acorn';
|
||||
|
||||
import type { ArraySymbolDef, SymbolInfo, SymbolType } from './symbols';
|
||||
|
||||
export interface ExpressionGenerator {
|
||||
/**
|
||||
* Converts an ESTree compatible AST node into a string representing the corresponding expression.
|
||||
*/
|
||||
generate(node: Expression): string;
|
||||
}
|
||||
|
||||
export interface ExpressionParserResult {
|
||||
/**
|
||||
* The expression statement from the valid portion of the parsed expression.
|
||||
*
|
||||
* It is undefined when no valid expression statements could be found.
|
||||
*/
|
||||
result: Expression;
|
||||
|
||||
/**
|
||||
* The information of the invalid (non-expression) nodes found from the other portions of the parsed expression.
|
||||
*/
|
||||
invalidNodes: Array<ExpressionStatement>;
|
||||
}
|
||||
|
||||
export interface ExpressionAutocompleteResults {
|
||||
suggestions: AutocompleteSuggestions;
|
||||
}
|
||||
|
||||
type ConditionalOperator = '?';
|
||||
|
||||
export const SUPPORTED_BINARY_OPERATORS = [
|
||||
{
|
||||
operator: '==',
|
||||
description: 'Checks whether two values are equal.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality',
|
||||
},
|
||||
{
|
||||
operator: '!=',
|
||||
description: 'Checks whether two values are unequal.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality',
|
||||
},
|
||||
{
|
||||
operator: '===',
|
||||
description: 'Checks whether two values are equal (strict comparison).',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality',
|
||||
},
|
||||
{
|
||||
operator: '!==',
|
||||
description: 'Checks whether two values are unequal (strict comparison).',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality',
|
||||
},
|
||||
{
|
||||
operator: '<',
|
||||
description: 'Checks if the left value is less than the right value.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than',
|
||||
},
|
||||
{
|
||||
operator: '<=',
|
||||
description: 'Checks if the left value is less than or equal to the right value.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than_or_equal',
|
||||
},
|
||||
{
|
||||
operator: '>',
|
||||
description: 'Checks if the left value is greater than the right value.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than',
|
||||
},
|
||||
{
|
||||
operator: '>=',
|
||||
description: 'Checks if the left value is greater than or equal to the right value.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than_or_equal',
|
||||
},
|
||||
{
|
||||
operator: 'in',
|
||||
description: 'Checks if a property exists in an object or if a value is in an array.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in',
|
||||
},
|
||||
] as const;
|
||||
|
||||
export const SUPPORTED_LOGICAL_OPERATORS = [
|
||||
{
|
||||
operator: '&&',
|
||||
description: 'Logical AND operator; returns true if both operands are true.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND',
|
||||
},
|
||||
{
|
||||
operator: '||',
|
||||
description: 'Logical OR operator; returns true if at least one operand is true.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR',
|
||||
},
|
||||
] as const;
|
||||
|
||||
export const SUPPORTED_CONDITIONAL_OPERATORS = [
|
||||
{
|
||||
operator: '?',
|
||||
description:
|
||||
'Conditional (ternary) operator; returns one of two values based on a condition.',
|
||||
link: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator',
|
||||
},
|
||||
] as const;
|
||||
|
||||
type DirectLiteralValue = {
|
||||
kind: 'direct';
|
||||
} & (
|
||||
| {
|
||||
type: SymbolType.Boolean;
|
||||
data: boolean;
|
||||
}
|
||||
| {
|
||||
type: SymbolType.Number;
|
||||
data: number;
|
||||
}
|
||||
| {
|
||||
type: SymbolType.String;
|
||||
data: string;
|
||||
}
|
||||
| {
|
||||
type: SymbolType.Null;
|
||||
data: null;
|
||||
}
|
||||
);
|
||||
|
||||
type InArrayLiteralValue = {
|
||||
kind: 'in-array';
|
||||
srcSymbol: ArraySymbolDef;
|
||||
matchedLiteralString: string;
|
||||
};
|
||||
|
||||
export type DirectLiteralValueSuggestion = {
|
||||
type: 'literal-value';
|
||||
value: DirectLiteralValue;
|
||||
};
|
||||
|
||||
export type InArrayLiteralValueSuggestion = {
|
||||
type: 'literal-value';
|
||||
value: InArrayLiteralValue;
|
||||
};
|
||||
|
||||
export type AutocompleteLiteralValueSuggestion =
|
||||
| DirectLiteralValueSuggestion
|
||||
| InArrayLiteralValueSuggestion;
|
||||
|
||||
export interface AutocompleteOperatorSuggestion {
|
||||
type: 'operator';
|
||||
operator:
|
||||
| Extract<BinaryOperator, (typeof SUPPORTED_BINARY_OPERATORS)[number]['operator']>
|
||||
| Extract<LogicalOperator, (typeof SUPPORTED_LOGICAL_OPERATORS)[number]['operator']>
|
||||
| Extract<
|
||||
ConditionalOperator,
|
||||
(typeof SUPPORTED_CONDITIONAL_OPERATORS)[number]['operator']
|
||||
>;
|
||||
description: string;
|
||||
link: string;
|
||||
}
|
||||
|
||||
export interface AutocompleteSymbolSuggestion {
|
||||
type: 'symbol';
|
||||
symbol: SymbolInfo;
|
||||
}
|
||||
|
||||
export type AutocompleteSuggestions = Array<
|
||||
| AutocompleteSymbolSuggestion
|
||||
| AutocompleteLiteralValueSuggestion
|
||||
| AutocompleteOperatorSuggestion
|
||||
>;
|
||||
|
||||
type LoggerFn = (message: string, ...args: any[]) => void;
|
||||
|
||||
/**
|
||||
* A logger that can be passed to the runtime.
|
||||
*/
|
||||
export interface Logger {
|
||||
debug: LoggerFn;
|
||||
info: LoggerFn;
|
||||
error: LoggerFn;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Format the result value of an expression for display as a string.
|
||||
*/
|
||||
export function formatExpressionResult(value: any, defaultValue = ''): string {
|
||||
if (value === undefined || value === null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === 'number' || typeof value === 'boolean') {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter function to exclude `null` values
|
||||
*/
|
||||
export function filterOutNullable<T>(value: T): value is NonNullable<T> {
|
||||
return !!value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type to make optional properties on a object mandatory.
|
||||
*
|
||||
* interface SomeObject {
|
||||
* uid: string;
|
||||
* price: number | null;
|
||||
* location?: string;
|
||||
* }
|
||||
*
|
||||
* type ValuableObject = MandateProps<SomeObject, 'price' | 'location'>;
|
||||
*/
|
||||
export type MandateProps<T extends {}, K extends keyof T> = T & {
|
||||
[MK in K]-?: NonNullable<T[MK]>;
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": ["@tsconfig/strictest/tsconfig.json", "@tsconfig/node20/tsconfig.json"],
|
||||
"compilerOptions": {
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"isolatedModules": true,
|
||||
"incremental": true,
|
||||
"noEmit": true,
|
||||
"noPropertyAccessFromIndexSignature": false,
|
||||
"exactOptionalPropertyTypes": false,
|
||||
"types": [
|
||||
"bun-types" // add Bun global
|
||||
]
|
||||
},
|
||||
"include": ["types/**/*.d.ts", "src/**/*.ts"]
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
declare module 'eval-estree-expression' {
|
||||
/**
|
||||
* Options for evaluation and compilation.
|
||||
*/
|
||||
export interface EvalESTreeExpressionOptions {
|
||||
/**
|
||||
* Force logical operators to return a boolean result. Default: undefined
|
||||
*/
|
||||
booleanLogicalOperators?: boolean;
|
||||
/**
|
||||
* Allow function calls to be evaluated. This is unsafe, please enable this option at your own risk. Default: false
|
||||
*/
|
||||
functions?: boolean;
|
||||
/**
|
||||
* Enable support for function statements and expressions by enabling the functions option AND by passing the .generate() function from the escodegen library. Default: undefined
|
||||
*/
|
||||
generate?: boolean | ((node: any) => string);
|
||||
/**
|
||||
* Enable the =~ regex operator to support testing values without using functions (example name =~ /^a.*c$/). Default: true
|
||||
*/
|
||||
regexOperator?: boolean;
|
||||
/**
|
||||
* Throw an error when variables are undefined. Default: false
|
||||
*/
|
||||
strict?: boolean;
|
||||
/**
|
||||
* Used with the variables method to return nested variables (e.g., variables with dot notation, like foo.bar.baz). Default: undefined
|
||||
*/
|
||||
withMembers?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an ESTree expression asynchronously against a given context.
|
||||
* @param expression - An object representing an ESTree-compliant AST node.
|
||||
* @param context - An object containing variables and values to be used during evaluation.
|
||||
* @returns A promise resolving to the result of the evaluation.
|
||||
*/
|
||||
export function evaluate<ASTNode>(
|
||||
ast: ASTNode,
|
||||
context: object,
|
||||
options?: EvalESTreeExpressionOptions
|
||||
): Promise<any>;
|
||||
|
||||
/**
|
||||
* Evaluates an ESTree expression synchronously against a given context.
|
||||
* @param expression - An object representing an ESTree-compliant AST node.
|
||||
* @param context - An object containing variables and values to be used during evaluation.
|
||||
* @returns The result of the evaluation.
|
||||
*/
|
||||
export namespace evaluate {
|
||||
function sync<ASTNode>(
|
||||
expression: ASTNode,
|
||||
context: object,
|
||||
options?: EvalESTreeExpressionOptions
|
||||
): any;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,23 @@
|
||||
# @gitbook/fonts
|
||||
|
||||
## 0.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 10995e0: Use NPM Trusted publishing for publishing the package.
|
||||
|
||||
## 0.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
|
||||
## 0.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
@@ -4,28 +4,38 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"development": "./src/index.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"sideEffects": false,
|
||||
"version": "0.1.3",
|
||||
"dependencies": {
|
||||
"@gitbook/api": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"google-font-metadata": "^6.0.3",
|
||||
"typescript": "^5.5.3"
|
||||
"bun-types": "catalog:",
|
||||
"google-font-metadata": "^6.0.7",
|
||||
"tsdown": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
},
|
||||
"scripts": {
|
||||
"generate": "bun ./bin/generate.js",
|
||||
"build": "tsc --project tsconfig.build.json",
|
||||
"build": "tsdown",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"dev": "tsc -w",
|
||||
"dev": "bun run build -- --watch ./src",
|
||||
"clean": "rm -rf ./dist && rm -rf ./src/data",
|
||||
"unit": "bun test"
|
||||
"unit": "bun test",
|
||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||
},
|
||||
"files": ["dist", "src", "bin", "README.md", "CHANGELOG.md"],
|
||||
"files": ["dist", "bin", "README.md", "CHANGELOG.md"],
|
||||
"engines": {
|
||||
"node": ">=20.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/GitbookIO/gitbook"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
// Bun Snapshot v1, https://goo.gl/fbAQLP
|
||||
// Bun Snapshot v1, https://bun.sh/docs/test/snapshots
|
||||
|
||||
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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/inter/v20/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",
|
||||
"url": "https://fonts.gstatic.com/s/roboto/v49/KFOMCnqEu92Fr1ME7kSn66aGLdTylUAMQXC89YmC2DPNWubEbVmUiAz0klQm_20.woff",
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -67,7 +67,7 @@ function getBestUnicodeRange(text: string, ranges: Record<string, string>): stri
|
||||
|
||||
const body = token.slice(2); // drop "U+"
|
||||
const [startHex, endHex] = body.split('-');
|
||||
const start = Number.parseInt(startHex, 16);
|
||||
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;
|
||||
@@ -92,7 +92,7 @@ function getBestUnicodeRange(text: string, ranges: Record<string, string>): stri
|
||||
|
||||
for (const [label, rangesArr] of Object.entries(parsed)) {
|
||||
if (rangesArr.some(([lo, hi]) => cp >= lo && cp <= hi)) {
|
||||
hits[label]++;
|
||||
hits[label]!++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "src/**/*.test.ts"]
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
"module": "ESNext",
|
||||
"target": "es2022",
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
|
||||
@@ -1,5 +1,324 @@
|
||||
# gitbook
|
||||
|
||||
## 0.20.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 1d92dc1: Update site layout and blocks with container queries and better transitions
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8fdc5c8: Fix webframe height issue
|
||||
- 9022d87: Add breadcrumbs to search results
|
||||
- 0c01832: SearchInput fixes — Translate Clear button, Bold theme color, "Esc" keyboard shortcut
|
||||
- 1d1f0a7: Fix search container shadow
|
||||
- 2aa4f2e: Update dropdown menu styling
|
||||
- a49781e: Printable collapsed code block
|
||||
- 7d6a316: Revert contentref anchor title
|
||||
- e27986b: Reword "Everywhere" search scope to "All docs"
|
||||
- 758b34d: Move the "Ask <query>" to the bottom of search results if query is not a question
|
||||
- 0ae7cfd: Improve AI Search branding
|
||||
- f9f8011: Add alt text support to card covers
|
||||
- ebbb476: Improve ancestor label in references
|
||||
- 1b0613e: Tweak language dropdown styling
|
||||
- 3355960: Fix ToggeableLinkItem display
|
||||
- c31e564: Fix icon resolution for page group
|
||||
- 55c0b03: Support translations and generic variants together
|
||||
- 3876f70: Edgeless Assistant window on `depth: flat` sites
|
||||
- 683bb8f: Add confirmation to "Copy MCP URL" page action
|
||||
- 4534ea8: Implement unique title tags for sections & variants
|
||||
- 6d56df7: Treat pages without visible TOC items as `no-toc`
|
||||
- 8606f22: Add padding to TOC page group items
|
||||
- 3676d64: Update search scope UI
|
||||
- c32d449: Fix OpenAPI path display
|
||||
- 8ce7322: Add OpenAPI servers selection
|
||||
- 2c3066e: Improve OAuth2 scopes handling in OpenAPI
|
||||
- 6b1c9fe: Fix opacity of ScrollContainer buttons on bold themes
|
||||
- Updated dependencies [b4a021a]
|
||||
- Updated dependencies [a512c90]
|
||||
- Updated dependencies [df1966d]
|
||||
- Updated dependencies [b45feaf]
|
||||
- Updated dependencies [10995e0]
|
||||
- Updated dependencies [f9f8011]
|
||||
- Updated dependencies [10995e0]
|
||||
- Updated dependencies [8ce7322]
|
||||
- Updated dependencies [2c3066e]
|
||||
- @gitbook/react-openapi@1.5.3
|
||||
- @gitbook/openapi-parser@3.0.5
|
||||
- @gitbook/react-contentkit@0.7.8
|
||||
- @gitbook/browser-types@0.1.3
|
||||
- @gitbook/cache-tags@0.3.4
|
||||
- @gitbook/react-math@0.6.4
|
||||
- @gitbook/colors@0.4.3
|
||||
- @gitbook/embed@0.1.5
|
||||
- @gitbook/fonts@0.1.3
|
||||
- @gitbook/icons@0.3.4
|
||||
- @gitbook/expr@1.2.4
|
||||
|
||||
## 0.19.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6142d6b: Mark as sideEffects, fix all package bundles
|
||||
- 75543b1: Update minimum Node version in .npmrc
|
||||
- 511f1cf: Add scroll margin to Expandable
|
||||
- Updated dependencies [6142d6b]
|
||||
- @gitbook/emoji-codepoints@0.2.2
|
||||
- @gitbook/react-contentkit@0.7.7
|
||||
- @gitbook/openapi-parser@3.0.4
|
||||
- @gitbook/browser-types@0.1.2
|
||||
- @gitbook/react-openapi@1.5.2
|
||||
- @gitbook/cache-tags@0.3.3
|
||||
- @gitbook/react-math@0.6.3
|
||||
- @gitbook/colors@0.4.2
|
||||
- @gitbook/fonts@0.1.2
|
||||
- @gitbook/icons@0.3.3
|
||||
- @gitbook/expr@1.2.3
|
||||
|
||||
## 0.19.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 295f03d: Republish packages
|
||||
- Updated dependencies [bdde392]
|
||||
- Updated dependencies [295f03d]
|
||||
- @gitbook/react-contentkit@0.7.6
|
||||
- @gitbook/browser-types@0.1.1
|
||||
- @gitbook/cache-tags@0.3.2
|
||||
- @gitbook/colors@0.4.1
|
||||
- @gitbook/emoji-codepoints@0.2.1
|
||||
- @gitbook/expr@1.2.2
|
||||
- @gitbook/fonts@0.1.1
|
||||
- @gitbook/icons@0.3.2
|
||||
- @gitbook/openapi-parser@3.0.3
|
||||
- @gitbook/react-math@0.6.2
|
||||
- @gitbook/react-openapi@1.5.1
|
||||
|
||||
## 0.19.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- ed7d47d: Show a toolbar for authenticated users with access to the current site.
|
||||
- 50c1be3: Move inline expression evaluation from API to GBO
|
||||
- 56e46ce: Add Italian translation
|
||||
- b932e4e: Scope search across sections and variants
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 162cfe4: Hide sections if only one is present
|
||||
- 1456251: Unify section display condition
|
||||
- 9344431: Fix missing geo data in site insights because of incorrect proxying of events.
|
||||
- eea8f1e: Enhance OpenAPI security scopes handling
|
||||
- 2b8a2d2: Add stable scroll gutter to search container
|
||||
- b498521: Fix robots.txt preventing favicon from being indexed.
|
||||
- ceb32b1: Redo search scope UI
|
||||
- 8e99871: Highlight discriminator properties in oneOf, allOf, anyOf objects
|
||||
- 2fc2127: Make search accessible
|
||||
- f3e4041: Adapt OpenAPI code samples to prefill API key using visitor data
|
||||
- 6815dd1: Fix markdownPageURL for PageActions
|
||||
- c06b3dd: Improve default site icon
|
||||
- cba583a: Use sitespace title instead of space title in search
|
||||
- e434442: Support nested site section groups
|
||||
- 469b332: Add support for inline expressions rendering with visitor data on GBO side
|
||||
- 0ef5dc8: Update to column width sizing
|
||||
- 1e4e54e: Fix rounded outline items
|
||||
- 379d486: Expose "Best match" when site has translations
|
||||
- c21693f: Improve screen reader accessibility for hints
|
||||
- badf3a9: Add alt text to inline image
|
||||
- c23d4ef: Fix crash for card cover defined without objectFit
|
||||
- d74f8a7: Improve wide page width layout
|
||||
- b8388e5: Add scrollcontainer component
|
||||
- b85eccd: Refactor section tabs
|
||||
- 1e53f46: Fix button content truncate
|
||||
- 7a504b7: Fix OpenAPI response dropdown & vertical overflow
|
||||
- a4c3399: Fix OpenAPI alternatives not showing
|
||||
- Updated dependencies [a629900]
|
||||
- Updated dependencies [eea8f1e]
|
||||
- Updated dependencies [8e99871]
|
||||
- Updated dependencies [f3e4041]
|
||||
- Updated dependencies [319a1e5]
|
||||
- Updated dependencies [529f940]
|
||||
- Updated dependencies [754cc11]
|
||||
- Updated dependencies [d7948e3]
|
||||
- Updated dependencies [a4c3399]
|
||||
- @gitbook/expr@1.2.1
|
||||
- @gitbook/react-openapi@1.5.0
|
||||
|
||||
## 0.18.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 262afa3: Expose a MCP server for the docs site under /~gitbook/mcp
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [262afa3]
|
||||
- @gitbook/icons@0.3.1
|
||||
- @gitbook/react-openapi@1.4.3
|
||||
- @gitbook/react-contentkit@0.7.5
|
||||
|
||||
## 0.17.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @gitbook/react-openapi@1.4.2
|
||||
|
||||
## 0.17.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 24f601d: Small optim in resolveTryItPrefillForOperation
|
||||
- aea5eb1: Persist language choice across sections if possible
|
||||
- 1165a81: Language selector edge cases
|
||||
- f9a2977: Better handling for external link "mailto:" in Hovered Card in GBO
|
||||
- Updated dependencies [24f601d]
|
||||
- @gitbook/react-openapi@1.4.1
|
||||
|
||||
## 0.17.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 12c9d76: Adapt OpenAPI blocks to eval adaptive exprs & prefill TryIt config
|
||||
- 7911350: Add language selector to site header
|
||||
- 659be55: Track insight event when embedded assistant is displayed.
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 2e0d706: Fix corner radius of active section on "Line" sidebar style
|
||||
- 78a632b: Fix edit on GitHub button doesn't take you to GitHub
|
||||
- 1edc5d6: Add "hold message" to Assistant
|
||||
- 4520728: Support bold in headings
|
||||
- 61b8507: OpenAPI: Make responses without objects clickable
|
||||
- d1fdc13: Remove ligatures from Lato font
|
||||
- a8fca0e: Fix custom assistant keyboard shortcut
|
||||
- 262a9b1: Fix embed script and assets being cached for too long
|
||||
- c890e01: Fix order in robots.txt preventing indexation of images by Google.
|
||||
- 1839ea2: Fix content min-height with sections
|
||||
- 9201e2c: Adds vertical align to column block
|
||||
- 193d591: Use space language as source of truth for UI locale
|
||||
- f08dd29: Fix Search results are not clickable on sites without header
|
||||
- 17dd382: Add `original` background color step
|
||||
- 4f35882: Fix event ask_question not being tracked
|
||||
- d51b79e: Fix Search bar is broken on site with sections when header is disabled
|
||||
- 6f368b5: Fix embed assistant window width on small screens
|
||||
- Updated dependencies [193d591]
|
||||
- Updated dependencies [12c9d76]
|
||||
- Updated dependencies [4927e96]
|
||||
- Updated dependencies [61b8507]
|
||||
- Updated dependencies [7fefe49]
|
||||
- Updated dependencies [360aa1c]
|
||||
- Updated dependencies [98e42cf]
|
||||
- Updated dependencies [17dd382]
|
||||
- @gitbook/colors@0.4.0
|
||||
- @gitbook/react-openapi@1.4.0
|
||||
- @gitbook/openapi-parser@3.0.2
|
||||
|
||||
## 0.16.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 6830815: Support custom AI providers
|
||||
- cbc71a5: Allow integrations to provide tools to the Docs Assistant
|
||||
- cc2e615: Emit a <link rel="alternate"> for the markdown version of a page
|
||||
- 81a6bd7: Support customization of buttons and tools through iframe API
|
||||
- 8927e8f: Start routes for embeddable version of the assistant and docs pages.
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- d30bcba: Improve `Button` and `ButtonGroup` styling
|
||||
- e1b2cf6: Fix scroll of page outline
|
||||
- d655d3e: Support "objectivec" as alias for Objective C syntax
|
||||
- 13ff22b: Fix AI Search follow-up question closing search
|
||||
- ffa866c: Small fixes to search modal
|
||||
- 36af03f: Fixes to `PageAside`
|
||||
- fb858a1: Tweaks to AIChatButton and AIChatInput
|
||||
- bcfa8d8: Improve vertical alignment of site items and fix floating page aside
|
||||
- ea7e94f: Fix search bar layout shift caused by ToC
|
||||
- 2e6e28e: Fix: Long strings overflow out of message bubble in docs assistant
|
||||
- ff96bb5: Support new coverDefinitionDark for cards & image type
|
||||
- 388b20d: Clear AI chat properly
|
||||
- ba7ec14: Fix bug in search highlight in GBO
|
||||
- 6217a2e: Page outline: scroll to active item
|
||||
- 42c17f5: Improve OpenAPI parsing errors
|
||||
- 854c448: Custom assistants followup
|
||||
- 43766d6: Fix Custom logo not rendering on the published site
|
||||
- Updated dependencies [cbc71a5]
|
||||
- Updated dependencies [25e2b40]
|
||||
- Updated dependencies [42c17f5]
|
||||
- Updated dependencies [854c448]
|
||||
- @gitbook/browser-types@0.1.0
|
||||
- @gitbook/icons@0.3.0
|
||||
- @gitbook/openapi-parser@3.0.1
|
||||
- @gitbook/react-contentkit@0.7.4
|
||||
- @gitbook/react-openapi@1.3.6
|
||||
|
||||
## 0.15.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- d130532: Suggest questions in the current space context
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 44f4151: Fix close buttons tooltips
|
||||
- d903273: Strip visitor params from URL
|
||||
- df7de8f: Fix an issue where links were not rendering properly in Ask AI answers
|
||||
- d270b4a: All tracking is now disabled for dynamic routes such as the site preview.
|
||||
- b1f608b: Implement basic URL scheme for assistant with ask
|
||||
- d024658: Prevent page breaks inside blocks when printing
|
||||
- 185cdb4: Fix scrolling to anchor positionning
|
||||
- 5c49235: Remove highlighting in Safari for PowerShell and C++ to avoid page crash until next version with bug fix is released
|
||||
- f1a6dec: Update OpenAPI parser
|
||||
- 2cdba53: Upgrade to Tailwind v4
|
||||
- Updated dependencies [2cdba53]
|
||||
- Updated dependencies [f1a6dec]
|
||||
- @gitbook/react-math@0.6.1
|
||||
- @gitbook/icons@0.2.2
|
||||
- @gitbook/openapi-parser@3.0.0
|
||||
- @gitbook/react-contentkit@0.7.3
|
||||
- @gitbook/react-openapi@1.3.5
|
||||
|
||||
## 0.14.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 7c951ef: 'Support new colors in text formatting
|
||||
- 39c4f76: Add fullwidth page option
|
||||
- eb1bd3a: AI response feedback buttons
|
||||
- 2ba7e54: Fix headings styles in hint block
|
||||
- 250c194: Add nosnippet to announcement banner.
|
||||
- 4aeb81b: Fix search input styling & text cutoff
|
||||
- 68f0dbc: Add confirmation modal to contentkit button
|
||||
- 611e286: Remove padding on first Heading in Columns
|
||||
- b0c534f: Tweaks to AI Chat and AI Actions Dropdown
|
||||
- Updated dependencies [b5ad0ce]
|
||||
- @gitbook/openapi-parser@2.2.2
|
||||
- @gitbook/react-openapi@1.3.4
|
||||
|
||||
## 0.14.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 0003030: Implement AI actions dropdown
|
||||
- acb9f53: New search layout
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 334cfdd: Fix responsive class for SearchInput button
|
||||
- 6816f0f: add browserlists and fix old browser css-masks
|
||||
- 9ee9082: Refactor icon loading state in AIAction components
|
||||
- 52ab368: Reposition AI Actions dropdown
|
||||
- 7212345: Fix AI Actions dropdown and LLM integration
|
||||
- 8daede5: Ensure all content links are resolved relatively to preview.
|
||||
- ed684c1: Move AI Actions markdown fetching to client-side
|
||||
- 9cc5a78: Generalise keyboard shortcuts, add Cmd+J to AI Chat
|
||||
- Updated dependencies [6816f0f]
|
||||
- Updated dependencies [1738677]
|
||||
- @gitbook/icons@0.2.1
|
||||
- @gitbook/openapi-parser@2.2.1
|
||||
- @gitbook/react-contentkit@0.7.2
|
||||
- @gitbook/react-openapi@1.3.3
|
||||
|
||||
## 0.13.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -62,11 +62,11 @@ const testCases: TestsCase[] = [
|
||||
contentBaseURL: 'https://docs.gmgn.ai',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
{
|
||||
name: 'docs.spicychat.ai',
|
||||
contentBaseURL: 'https://docs.spicychat.ai',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
// {
|
||||
// name: 'docs.spicychat.ai',
|
||||
// contentBaseURL: 'https://docs.spicychat.ai',
|
||||
// tests: [{ name: 'Home', url: '/' }],
|
||||
// },
|
||||
{
|
||||
name: 'docs.portainer.io',
|
||||
contentBaseURL: 'https://docs.portainer.io',
|
||||
@@ -112,11 +112,11 @@ const testCases: TestsCase[] = [
|
||||
contentBaseURL: 'https://adiblar.gitbook.io',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
{
|
||||
name: 'docs.gradient.network',
|
||||
contentBaseURL: 'https://docs.gradient.network',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
// {
|
||||
// name: 'docs.gradient.network',
|
||||
// contentBaseURL: 'https://docs.gradient.network',
|
||||
// tests: [{ name: 'Home', url: '/' }],
|
||||
// },
|
||||
// {
|
||||
// name: 'mygate-network.gitbook.io',
|
||||
// contentBaseURL: 'https://mygate-network.gitbook.io',
|
||||
@@ -172,11 +172,6 @@ const testCases: TestsCase[] = [
|
||||
contentBaseURL: 'https://docs.realapp.link',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
{
|
||||
name: 'docs.plaza.finance',
|
||||
contentBaseURL: 'https://docs.plaza.finance',
|
||||
tests: [{ name: 'Home', url: '/' }],
|
||||
},
|
||||
{
|
||||
name: 'docs.publicai.io',
|
||||
contentBaseURL: 'https://docs.publicai.io',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
CustomizationAIMode,
|
||||
CustomizationBackground,
|
||||
CustomizationCorners,
|
||||
CustomizationDefaultMonospaceFont,
|
||||
@@ -19,6 +20,7 @@ import {
|
||||
|
||||
import { getSiteAPIToken } from '../tests/utils';
|
||||
import {
|
||||
type Test,
|
||||
type TestsCase,
|
||||
allDeprecatedThemePresets,
|
||||
allLocales,
|
||||
@@ -31,9 +33,215 @@ import {
|
||||
headerLinks,
|
||||
runTestCases,
|
||||
waitForCookiesDialog,
|
||||
waitForCoverImages,
|
||||
waitForNotFound,
|
||||
} from './util';
|
||||
|
||||
const AI_PROMPT = `You're being invoked by the GitBook CI/CD pipeline. To make screenshot testing of the GitBook Assistant visually consistent, look up the title of the first page you find and respond with only EXACTLY its title. To find the page title, invoke the search tool with the query "GitBook". Before invoking the search tool, respond with the exact text: "I'm going to look up 'GitBook' and then respond with only the page title.". Do not execute any other tools or output any other text.`;
|
||||
|
||||
const overrideAIInitialState = () => {
|
||||
const greeting = document.querySelector('[data-testid="ai-chat-time-greeting"]');
|
||||
if (greeting) {
|
||||
greeting.textContent = 'Good morning';
|
||||
}
|
||||
};
|
||||
const overrideAIResponse = () => {
|
||||
const userMessage = document.querySelector('[data-testid="ai-chat-message-user"]');
|
||||
if (userMessage) {
|
||||
userMessage.textContent = '[Replaced message] Chat message sent by the user';
|
||||
}
|
||||
const assistantMessage = document.querySelectorAll(
|
||||
'[data-testid="ai-chat-message-assistant"] .ai-response-document'
|
||||
);
|
||||
assistantMessage.forEach((message) => {
|
||||
message.innerHTML = '[Replaced message] AI chat response';
|
||||
});
|
||||
const suggestions = document.querySelectorAll('[data-testid="ai-chat-followup-suggestion"]');
|
||||
suggestions.forEach((suggestion) => {
|
||||
suggestion.textContent = 'Follow-up suggestion';
|
||||
});
|
||||
};
|
||||
|
||||
const searchTestCases: Test[] = [
|
||||
{
|
||||
name: 'Search - AI Mode: None - Complete flow',
|
||||
url: getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.None,
|
||||
},
|
||||
}),
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
const searchInput = page.getByTestId('search-input');
|
||||
await searchInput.focus();
|
||||
await expect(page.getByTestId('search-results')).toHaveCount(0); // No pop-up yet because there's no recommended questions.
|
||||
|
||||
// Fill search input, expecting search results
|
||||
await searchInput.fill('gitbook');
|
||||
await expect(page.getByTestId('search-results')).toBeVisible({
|
||||
timeout: 10_000,
|
||||
});
|
||||
const pageResults = await page.getByTestId('search-page-result').all();
|
||||
await expect(pageResults.length).toBeGreaterThanOrEqual(1);
|
||||
const pageSectionResults = await page.getByTestId('search-page-section-result').all();
|
||||
await expect(pageSectionResults.length).toBeGreaterThanOrEqual(2);
|
||||
await expect(page.getByTestId('search-ask-question')).toHaveCount(0); // No AI search results with aiMode=None.
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search - AI Mode: None - Keyboard shortcut',
|
||||
url: getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.None,
|
||||
},
|
||||
}),
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
await page.keyboard.press('ControlOrMeta+K');
|
||||
await expect(page.getByTestId('search-input')).toBeFocused();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search - AI Mode: None - URL query (Initial)',
|
||||
url: `${getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.None,
|
||||
},
|
||||
})}&q=`,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-results')).toHaveCount(0); // No pop-up yet because there's no recommended questions.
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search - AI Mode: None - URL query (Results)',
|
||||
url: `${getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.None,
|
||||
},
|
||||
})}&q=gitbook`,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-input')).toBeFocused();
|
||||
await expect(page.getByTestId('search-input')).toHaveValue('gitbook');
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search - AI Mode: Search - URL query (Results)',
|
||||
url: `${getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Search,
|
||||
},
|
||||
})}&q=gitbook`,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-input')).toBeFocused();
|
||||
await expect(page.getByTestId('search-input')).toHaveValue('gitbook');
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Ask - AI Mode: Assistant - Complete flow',
|
||||
url: getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Assistant,
|
||||
},
|
||||
}),
|
||||
run: async (page) => {
|
||||
const searchInput = page.locator('css=[data-testid="search-input"]');
|
||||
|
||||
// Focus search input, expecting recommended questions
|
||||
await searchInput.focus();
|
||||
await expect(page.getByTestId('search-results')).toBeVisible({
|
||||
timeout: 30_000,
|
||||
});
|
||||
const recommendedQuestions = await page
|
||||
.getByTestId('search-recommended-question')
|
||||
.all();
|
||||
await expect(recommendedQuestions.length).toBeGreaterThan(2); // Expect at least 3 questions
|
||||
|
||||
// Fill search input, expecting AI search option
|
||||
await searchInput.fill(AI_PROMPT);
|
||||
const aiSearchResult = page.getByTestId('search-ask-question');
|
||||
await expect(aiSearchResult).toBeVisible();
|
||||
await aiSearchResult.click();
|
||||
await expect(page.getByTestId('ai-chat')).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-message-user').first()).toHaveText(AI_PROMPT);
|
||||
await expect(page.getByTestId('ai-chat-message-assistant').first()).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-followup-suggestion')).toHaveCount(3, {
|
||||
timeout: 60_000,
|
||||
});
|
||||
// Override text content for visual consistency in screenshots
|
||||
await page.evaluate(overrideAIResponse);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Ask - AI Mode: Assistant - Keyboard shortcut',
|
||||
url: getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Assistant,
|
||||
},
|
||||
}),
|
||||
run: async (page) => {
|
||||
await page.keyboard.press('ControlOrMeta+I');
|
||||
await expect(page.getByTestId('ai-chat')).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
|
||||
// Override text content for visual consistency in screenshots
|
||||
await page.evaluate(overrideAIInitialState);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Ask - AI Mode: Assistant - Button',
|
||||
url: getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Assistant,
|
||||
},
|
||||
}),
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
await page.getByTestId('ai-chat-button').click();
|
||||
await expect(page.getByTestId('ai-chat')).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
|
||||
// Override text content for visual consistency in screenshots
|
||||
await page.evaluate(overrideAIInitialState);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Ask - AI Mode: Assistant - URL query (Initial)',
|
||||
url: `${getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Assistant,
|
||||
},
|
||||
})}&ask=`,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-input')).not.toBeFocused();
|
||||
await expect(page.getByTestId('search-input')).toBeEmpty();
|
||||
await expect(page.getByTestId('ai-chat')).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
|
||||
// Override text content for visual consistency in screenshots
|
||||
await page.evaluate(overrideAIInitialState);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Ask - AI Mode: Assistant - URL query (Results)',
|
||||
url: `${getCustomizationURL({
|
||||
ai: {
|
||||
mode: CustomizationAIMode.Assistant,
|
||||
},
|
||||
})}&ask=${encodeURIComponent(AI_PROMPT)}`,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-input')).not.toBeFocused();
|
||||
await expect(page.getByTestId('search-input')).not.toHaveValue('What is GitBook?');
|
||||
await expect(page.getByTestId('ai-chat')).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-message-user').first()).toHaveText(AI_PROMPT);
|
||||
await expect(page.getByTestId('ai-chat-message-assistant').first()).toBeVisible();
|
||||
await expect(page.getByTestId('ai-chat-followup-suggestion')).toHaveCount(3, {
|
||||
timeout: 60_000,
|
||||
});
|
||||
// Override text content for visual consistency in screenshots
|
||||
await page.evaluate(overrideAIResponse);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const testCases: TestsCase[] = [
|
||||
{
|
||||
name: 'GitBook Site (Single Variant)',
|
||||
@@ -54,33 +262,58 @@ const testCases: TestsCase[] = [
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
url: '?q=',
|
||||
screenshot: false,
|
||||
name: 'Expandable TOC navigation',
|
||||
url: '',
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
const allItems = await page.getByTestId('search-result-item').all();
|
||||
// Expect at least 3 questions
|
||||
await expect(allItems.length).toBeGreaterThan(2);
|
||||
await waitForCookiesDialog(page);
|
||||
|
||||
// Verify "Navigation" link is not visible initially
|
||||
const navigationLink = page.getByRole('link', { name: 'Navigation' });
|
||||
await expect(navigationLink).not.toBeVisible();
|
||||
|
||||
// Find and click the chevron element that is next to "Editor" in the TOC
|
||||
// It is a span inside the link
|
||||
const editorChevron = page
|
||||
.getByRole('link', { name: 'Editor' })
|
||||
.locator('span');
|
||||
await editorChevron.click();
|
||||
|
||||
// Verify "Navigation" link becomes visible after expansion
|
||||
await expect(navigationLink).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search Results',
|
||||
url: '?q=gitbook',
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'AI Search',
|
||||
url: '?q=What+is+GitBook%3F&ask=true',
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-ask-answer')).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
},
|
||||
name: 'Expandable nested TOC navigation',
|
||||
url: '',
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
await waitForCookiesDialog(page);
|
||||
|
||||
// Verify "Spaces" link is not visible initially
|
||||
const navigationLink = page.getByRole('link', { name: 'Spaces' });
|
||||
await expect(navigationLink).not.toBeVisible();
|
||||
|
||||
// Find and click the chevron element that is next to "Editor" in the TOC
|
||||
// It is a span inside the link
|
||||
const editorChevron = page
|
||||
.getByRole('link', { name: 'Editor' })
|
||||
.locator('span');
|
||||
await editorChevron.click();
|
||||
|
||||
// At this stage the link should still not be visible
|
||||
await expect(navigationLink).not.toBeVisible();
|
||||
|
||||
// Then we click 'Content Structure' chevron to expand further
|
||||
const contentStructureChevron = page
|
||||
.getByRole('link', { name: 'Content Structure' })
|
||||
.locator('span');
|
||||
await contentStructureChevron.click();
|
||||
|
||||
// Verify "Spaces" link becomes visible after expansion
|
||||
await expect(navigationLink).toBeVisible();
|
||||
},
|
||||
},
|
||||
...searchTestCases,
|
||||
{
|
||||
name: 'Not found',
|
||||
url: 'content-not-found',
|
||||
@@ -137,6 +370,37 @@ const testCases: TestsCase[] = [
|
||||
).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Switch variant with alternate link in metadata',
|
||||
url: 'rfcs',
|
||||
run: async (page) => {
|
||||
const spaceDropdown = page
|
||||
.locator('[data-testid="space-dropdown-button"]')
|
||||
.locator('visible=true');
|
||||
await spaceDropdown.click();
|
||||
|
||||
const variantSelectionDropdown = page.locator(
|
||||
'css=[data-testid="dropdown-menu"]'
|
||||
);
|
||||
|
||||
// Click the variant space called 'Multi-Variants' for which
|
||||
// there is an alternate link in the current (RFC variant) page metadata
|
||||
await variantSelectionDropdown
|
||||
.getByRole('menuitem', {
|
||||
name: 'Multi-Variants',
|
||||
})
|
||||
.click();
|
||||
|
||||
// It should navigate to the alternate link defined in the metadata (a completely different page)
|
||||
await page.waitForURL((url) =>
|
||||
url.pathname.includes('multi-variants/reference/api-reference/pets')
|
||||
);
|
||||
// Verify we are on the correct page by checking the h1
|
||||
await expect(
|
||||
page.getByRole('heading', { level: 1, name: 'Pets' })
|
||||
).toBeVisible();
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -279,34 +543,7 @@ const testCases: TestsCase[] = [
|
||||
url: '',
|
||||
run: waitForCookiesDialog,
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
url: '?q=',
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
const allItems = await page.getByTestId('search-result-item').all();
|
||||
// Expect at least 3 questions
|
||||
await expect(allItems.length).toBeGreaterThan(2);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Search Results',
|
||||
url: '?q=gitbook',
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-results')).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'AI Search',
|
||||
url: '?q=What+is+GitBook%3F&ask=true',
|
||||
run: async (page) => {
|
||||
await expect(page.getByTestId('search-ask-answer')).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
},
|
||||
screenshot: false,
|
||||
},
|
||||
...searchTestCases,
|
||||
{
|
||||
name: 'Not found',
|
||||
url: 'content-not-found',
|
||||
@@ -415,6 +652,28 @@ const testCases: TestsCase[] = [
|
||||
await expect(page.locator('[data-testid="table-of-contents"]')).toBeVisible();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'With sections',
|
||||
url: async () => {
|
||||
const data = await getSiteAPIToken('https://gitbook.com/docs');
|
||||
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.set('token', data.apiToken);
|
||||
|
||||
return `url/preview/${data.site}/?${searchParams.toString()}`;
|
||||
},
|
||||
screenshot: false,
|
||||
run: async (page) => {
|
||||
const sectionTabs = page.getByLabel('Sections');
|
||||
await expect(sectionTabs).toBeVisible();
|
||||
|
||||
const sectionTabLinks = sectionTabs.getByRole('link');
|
||||
for (const link of await sectionTabLinks.all()) {
|
||||
const href = await link.getAttribute('href');
|
||||
expect(href).toMatch(/^\/url\/preview\/site_p4Xo4\/?/);
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -584,7 +843,10 @@ const testCases: TestsCase[] = [
|
||||
url: 'blocks/integrations',
|
||||
run: async (page) => {
|
||||
await waitForCookiesDialog(page);
|
||||
const mermaidIframe = page.locator('iframe[title*="mermaid"]').contentFrame();
|
||||
const mermaidIframe = page
|
||||
.locator('iframe[title*="mermaid"]')
|
||||
.first()
|
||||
.contentFrame();
|
||||
await expect(mermaidIframe.getByText('Mermaid', { exact: true })).toBeVisible();
|
||||
await expect(mermaidIframe.getByText('Diagram', { exact: true })).toBeVisible();
|
||||
},
|
||||
@@ -650,6 +912,11 @@ const testCases: TestsCase[] = [
|
||||
url: 'blocks/cards',
|
||||
fullPage: true,
|
||||
},
|
||||
{
|
||||
name: 'Updates',
|
||||
url: 'blocks/updates',
|
||||
fullPage: true,
|
||||
},
|
||||
{
|
||||
name: 'Math',
|
||||
url: 'blocks/math',
|
||||
@@ -708,7 +975,10 @@ const testCases: TestsCase[] = [
|
||||
{
|
||||
name: 'With cover',
|
||||
url: 'page-options/page-with-cover',
|
||||
run: waitForCookiesDialog,
|
||||
run: async (page) => {
|
||||
await waitForCookiesDialog(page);
|
||||
await waitForCoverImages(page);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'With cover for dark mode',
|
||||
@@ -723,12 +993,18 @@ const testCases: TestsCase[] = [
|
||||
{
|
||||
name: 'With hero cover',
|
||||
url: 'page-options/page-with-hero-cover',
|
||||
run: waitForCookiesDialog,
|
||||
run: async (page) => {
|
||||
await waitForCookiesDialog(page);
|
||||
await waitForCoverImages(page);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'With cover and no TOC',
|
||||
url: 'page-options/page-with-cover-and-no-toc',
|
||||
run: waitForCookiesDialog,
|
||||
run: async (page) => {
|
||||
await waitForCookiesDialog(page);
|
||||
await waitForCoverImages(page);
|
||||
},
|
||||
screenshot: {
|
||||
waitForTOCScrolling: false,
|
||||
},
|
||||
@@ -910,6 +1186,22 @@ const testCases: TestsCase[] = [
|
||||
},
|
||||
]),
|
||||
},
|
||||
{
|
||||
name: 'Page actions',
|
||||
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
|
||||
tests: [
|
||||
{
|
||||
name: 'Without page actions',
|
||||
url: getCustomizationURL({
|
||||
pageActions: {
|
||||
markdown: false,
|
||||
externalAI: false,
|
||||
},
|
||||
}),
|
||||
run: waitForCookiesDialog,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'Ads',
|
||||
contentBaseURL: 'https://gitbook.gitbook.io/test-gitbook-open/',
|
||||
@@ -1295,10 +1587,7 @@ const testCases: TestsCase[] = [
|
||||
locale,
|
||||
},
|
||||
}),
|
||||
run: async (page) => {
|
||||
const dialog = page.getByTestId('cookies-dialog');
|
||||
await expect(dialog).toBeVisible();
|
||||
},
|
||||
run: waitForCookiesDialog,
|
||||
})),
|
||||
},
|
||||
{
|
||||
|
||||
@@ -143,7 +143,10 @@ export const headerLinks: CustomizationHeaderItem[] = [
|
||||
|
||||
export async function waitForCookiesDialog(page: Page) {
|
||||
const dialog = page.getByTestId('cookies-dialog');
|
||||
await expect(dialog).toBeVisible();
|
||||
await expect(dialog).toBeVisible({
|
||||
// Cookies dialog may take some times to appear
|
||||
timeout: 10_000,
|
||||
});
|
||||
}
|
||||
|
||||
export async function waitForNotFound(_page: Page, response: Response | null) {
|
||||
@@ -151,6 +154,13 @@ export async function waitForNotFound(_page: Page, response: Response | null) {
|
||||
expect(response?.status()).toBe(404);
|
||||
}
|
||||
|
||||
export async function waitForCoverImages(page: Page) {
|
||||
// Wait for cover images to exist (not the shimmer placeholder)
|
||||
await expect(page.locator('img[alt="Page cover"]').first()).toBeVisible({
|
||||
timeout: 10_000,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform test cases into Playwright tests and run it.
|
||||
*/
|
||||
@@ -318,10 +328,6 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
|
||||
feedback: {
|
||||
enabled: false,
|
||||
},
|
||||
// TODO: remove aiSearch once the cache has been fully updated (after 11/07/2025)
|
||||
aiSearch: {
|
||||
enabled: true,
|
||||
},
|
||||
ai: {
|
||||
mode: CustomizationAIMode.None,
|
||||
},
|
||||
@@ -337,6 +343,11 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
|
||||
pagination: {
|
||||
enabled: true,
|
||||
},
|
||||
pageActions: {
|
||||
externalAI: true,
|
||||
markdown: true,
|
||||
mcp: true,
|
||||
},
|
||||
trademark: {
|
||||
enabled: true,
|
||||
},
|
||||
|
||||
@@ -14,6 +14,9 @@ const nextConfig = {
|
||||
dynamic: 3600, // 1 hour
|
||||
static: 3600, // 1 hour
|
||||
},
|
||||
|
||||
// Since content is fully static, we don't want to fetch on hover again
|
||||
optimisticClientCache: false,
|
||||
},
|
||||
|
||||
env: {
|
||||
|
||||
@@ -7,7 +7,8 @@ export default {
|
||||
converter: 'edge',
|
||||
proxyExternalRequest: 'fetch',
|
||||
queue: () => import('./openNext/queue/middleware').then((m) => m.default),
|
||||
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default),
|
||||
incrementalCache: () =>
|
||||
import('./openNext/incrementalCache/server').then((m) => m.default),
|
||||
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default),
|
||||
},
|
||||
},
|
||||
@@ -18,7 +19,8 @@ export default {
|
||||
converter: 'edge',
|
||||
proxyExternalRequest: 'fetch',
|
||||
queue: () => import('./openNext/queue/middleware').then((m) => m.default),
|
||||
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default),
|
||||
incrementalCache: () =>
|
||||
import('./openNext/incrementalCache/middleware').then((m) => m.default),
|
||||
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -2,6 +2,9 @@ import { runWithCloudflareRequestContext } from '../../.open-next/cloudflare/ini
|
||||
|
||||
import { DurableObject } from 'cloudflare:workers';
|
||||
|
||||
//Only needed to run locally, in prod we'll use the one from do.js
|
||||
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js';
|
||||
|
||||
// Only needed to run locally, in prod we'll use the one from do.js
|
||||
export class R2WriteBuffer extends DurableObject {
|
||||
writePromise;
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
{
|
||||
"main": "default.js",
|
||||
"name": "gitbook-open-v2-server",
|
||||
"keep_names": false,
|
||||
"compatibility_date": "2025-04-14",
|
||||
"compatibility_flags": [
|
||||
"nodejs_compat",
|
||||
"allow_importable_env",
|
||||
"global_fetch_strictly_public"
|
||||
],
|
||||
"observability": {
|
||||
"enabled": true
|
||||
},
|
||||
"vars": {
|
||||
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true"
|
||||
},
|
||||
"env": {
|
||||
"dev": {
|
||||
"vars": {
|
||||
"STAGE": "dev"
|
||||
"STAGE": "dev",
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true",
|
||||
"GITBOOK_URL": "http://localhost:8771"
|
||||
},
|
||||
"r2_buckets": [
|
||||
{
|
||||
@@ -35,13 +35,17 @@
|
||||
{
|
||||
"name": "WRITE_BUFFER",
|
||||
"class_name": "R2WriteBuffer"
|
||||
},
|
||||
{
|
||||
"name": "NEXT_TAG_CACHE_DO_SHARDED",
|
||||
"class_name": "DOShardedTagCache"
|
||||
}
|
||||
]
|
||||
},
|
||||
"migrations": [
|
||||
{
|
||||
"tag": "v1",
|
||||
"new_sqlite_classes": ["R2WriteBuffer"]
|
||||
"new_sqlite_classes": ["R2WriteBuffer", "DOShardedTagCache"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -84,6 +88,9 @@
|
||||
}
|
||||
},
|
||||
"staging": {
|
||||
"vars": {
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true"
|
||||
},
|
||||
"r2_buckets": [
|
||||
{
|
||||
"binding": "NEXT_INC_CACHE_R2_BUCKET",
|
||||
@@ -125,7 +132,8 @@
|
||||
"vars": {
|
||||
// This is a bit misleading, but it means that we can have 500 concurrent revalidations
|
||||
// This means that we'll have up to 100 durable objects instance running at the same time
|
||||
"MAX_REVALIDATE_CONCURRENCY": "100"
|
||||
"MAX_REVALIDATE_CONCURRENCY": "100",
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true"
|
||||
},
|
||||
"r2_buckets": [
|
||||
{
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
"main": "do.js",
|
||||
"name": "gitbook-open-v2-do",
|
||||
"compatibility_date": "2025-04-14",
|
||||
"keep_names": false,
|
||||
"compatibility_flags": [
|
||||
"nodejs_compat",
|
||||
"allow_importable_env",
|
||||
"global_fetch_strictly_public"
|
||||
],
|
||||
"observability": {
|
||||
"enabled": true
|
||||
},
|
||||
"env": {
|
||||
"preview": {
|
||||
"vars": {
|
||||
|
||||
@@ -7,12 +7,39 @@ export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js
|
||||
|
||||
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js';
|
||||
|
||||
//Format used by the tail logger
|
||||
const formatLog = (requestDuration, responseCode, responseCache, responseRoute) =>
|
||||
`%${JSON.stringify({ requestDuration, responseCode, responseCache, responseRoute })}%[main] Response Done`;
|
||||
|
||||
const getResolvedRoute = (reqOrResp, defaultValue) => {
|
||||
try {
|
||||
const resolvedRoutes = JSON.parse(
|
||||
reqOrResp.headers.get('x-opennext-resolved-routes') ?? ''
|
||||
)[0].route;
|
||||
return resolvedRoutes ?? defaultValue;
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
};
|
||||
|
||||
export default class extends WorkerEntrypoint {
|
||||
async fetch(request) {
|
||||
return runWithCloudflareRequestContext(request, this.env, this.ctx, async () => {
|
||||
const startTime = Date.now();
|
||||
const middlewareRequest = new Request(request.url, request);
|
||||
middlewareRequest.headers.set('x-open-next-continent', request.cf?.continent || '');
|
||||
// - `Request`s are handled by the Next server
|
||||
const reqOrResp = await middlewareHandler(request, this.env, this.ctx);
|
||||
const reqOrResp = await middlewareHandler(middlewareRequest, this.env, this.ctx);
|
||||
if (reqOrResp instanceof Response) {
|
||||
const duration = Date.now() - startTime;
|
||||
const logMessage = formatLog(
|
||||
duration,
|
||||
reqOrResp.status,
|
||||
reqOrResp.headers.get('x-opennext-cache') ?? 'MISS',
|
||||
getResolvedRoute(reqOrResp, 'middleware')
|
||||
);
|
||||
// biome-ignore lint/suspicious/noConsole: <explanation>
|
||||
console.log(logMessage);
|
||||
return reqOrResp;
|
||||
}
|
||||
|
||||
@@ -22,17 +49,31 @@ export default class extends WorkerEntrypoint {
|
||||
'Cloudflare-Workers-Version-Overrides',
|
||||
`gitbook-open-v2-${this.env.STAGE}="${this.env.WORKER_VERSION_ID}"`
|
||||
);
|
||||
return this.env.DEFAULT_WORKER?.fetch(reqOrResp, {
|
||||
const response = await this.env.DEFAULT_WORKER?.fetch(reqOrResp, {
|
||||
redirect: 'manual',
|
||||
cf: {
|
||||
cacheEverything: false,
|
||||
},
|
||||
});
|
||||
const formatedLog = formatLog(
|
||||
Date.now() - startTime,
|
||||
response.status,
|
||||
`SERVER-${response.headers.get('x-nextjs-cache') ?? 'MISS'}`,
|
||||
getResolvedRoute(reqOrResp, 'unresolved')
|
||||
);
|
||||
// biome-ignore lint/suspicious/noConsole: <explanation>
|
||||
console.log(formatedLog);
|
||||
|
||||
return response;
|
||||
}
|
||||
// If we are in preview mode, we need to send the request to the preview URL
|
||||
const modifiedUrl = new URL(reqOrResp.url);
|
||||
modifiedUrl.hostname = this.env.PREVIEW_HOSTNAME;
|
||||
const nextRequest = new Request(modifiedUrl, reqOrResp);
|
||||
return fetch(nextRequest, {
|
||||
// We never want to follow the redirects here.
|
||||
// Redirects are supposed to happen from the client.
|
||||
redirect: 'manual',
|
||||
cf: {
|
||||
cacheEverything: false,
|
||||
},
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
"main": "middleware.js",
|
||||
"name": "gitbook-open-v2",
|
||||
"compatibility_date": "2025-04-14",
|
||||
"keep_names": false,
|
||||
"compatibility_flags": [
|
||||
"nodejs_compat",
|
||||
"allow_importable_env",
|
||||
@@ -11,9 +12,6 @@
|
||||
"directory": "../../.open-next/assets",
|
||||
"binding": "ASSETS"
|
||||
},
|
||||
"observability": {
|
||||
"enabled": true
|
||||
},
|
||||
"vars": {
|
||||
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true"
|
||||
},
|
||||
@@ -21,7 +19,12 @@
|
||||
"dev": {
|
||||
"vars": {
|
||||
"STAGE": "dev",
|
||||
"NEXT_PRIVATE_DEBUG_CACHE": "true"
|
||||
"NEXT_PRIVATE_DEBUG_CACHE": "true",
|
||||
// When deployed locally, we don't have access to the tag cache here,
|
||||
// we should just bypass the cache to go to the server directly
|
||||
"SHOULD_BYPASS_CACHE": "true",
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true",
|
||||
"GITBOOK_URL": "http://localhost:8771"
|
||||
},
|
||||
"r2_buckets": [
|
||||
{
|
||||
@@ -85,7 +88,8 @@
|
||||
"staging": {
|
||||
"vars": {
|
||||
"STAGE": "staging",
|
||||
"WORKER_VERSION_ID": "TO_REPLACE"
|
||||
"WORKER_VERSION_ID": "TO_REPLACE",
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true"
|
||||
},
|
||||
"routes": [
|
||||
{
|
||||
@@ -153,7 +157,8 @@
|
||||
// TODO: remove this once the issue is fixed
|
||||
"DEBUG_CLOUDFLARE": "true",
|
||||
"WORKER_VERSION_ID": "TO_REPLACE",
|
||||
"STAGE": "production"
|
||||
"STAGE": "production",
|
||||
"OPEN_NEXT_REQUEST_ID_HEADER": "true"
|
||||
},
|
||||
"routes": [
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ const args = process.argv.slice(2);
|
||||
const versionId = args[0];
|
||||
|
||||
// The preview URL is in the format https://<versionId>-gitbook-open-v2-server-preview.gitbook.workers.dev
|
||||
const previewHostname = `${versionId.split('-')[0]}-gitbook-open-v2-server-preview.gitbook.workers.dev`;
|
||||
const previewHostname = `${versionId?.split('-')[0]}-gitbook-open-v2-server-preview.gitbook.workers.dev`;
|
||||
|
||||
let updatedFile = file.replace(
|
||||
/"PREVIEW_HOSTNAME": "TO_REPLACE"/,
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
import { trace } from '@/lib/tracing';
|
||||
import type {
|
||||
CacheEntryType,
|
||||
CacheValue,
|
||||
IncrementalCache,
|
||||
WithLastModified,
|
||||
} from '@opennextjs/aws/types/overrides.js';
|
||||
import { getCloudflareContext } from '@opennextjs/cloudflare';
|
||||
|
||||
import type { DurableObjectNamespace, Rpc } from '@cloudflare/workers-types';
|
||||
|
||||
export const BINDING_NAME = 'NEXT_INC_CACHE_R2_BUCKET';
|
||||
export const DEFAULT_PREFIX = 'incremental-cache';
|
||||
|
||||
export type KeyOptions = {
|
||||
cacheType?: CacheEntryType;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* It is very similar to the `R2IncrementalCache` in the `@opennextjs/cloudflare` package, but it allow us to trace
|
||||
* the cache operations. It also integrates both R2 and Cache API in a single class.
|
||||
* Having our own, will allow us to customize it in the future if needed.
|
||||
*/
|
||||
class GitbookIncrementalCache implements IncrementalCache {
|
||||
name = 'GitbookIncrementalCache';
|
||||
|
||||
protected localCache: Cache | undefined;
|
||||
|
||||
async get<CacheType extends CacheEntryType = 'cache'>(
|
||||
key: string,
|
||||
cacheType?: CacheType
|
||||
): Promise<WithLastModified<CacheValue<CacheType>> | null> {
|
||||
const cacheKey = this.getR2Key(key, cacheType);
|
||||
return trace(
|
||||
{
|
||||
operation: 'openNextIncrementalCacheGet',
|
||||
name: cacheKey,
|
||||
},
|
||||
async (span) => {
|
||||
span.setAttribute('cacheType', cacheType ?? 'cache');
|
||||
const r2 = getCloudflareContext().env[BINDING_NAME];
|
||||
const localCache = await this.getCacheInstance();
|
||||
if (!r2) throw new Error('No R2 bucket');
|
||||
try {
|
||||
// Check local cache first if available
|
||||
const localCacheEntry = await localCache.match(this.getCacheUrlKey(cacheKey));
|
||||
if (localCacheEntry) {
|
||||
span.setAttribute('cacheHit', 'local');
|
||||
const result = (await localCacheEntry.json()) as WithLastModified<
|
||||
CacheValue<CacheType>
|
||||
>;
|
||||
return this.returnNullOn404(result);
|
||||
}
|
||||
|
||||
const r2Object = await r2.get(cacheKey);
|
||||
if (!r2Object) return null;
|
||||
|
||||
span.setAttribute('cacheHit', 'r2');
|
||||
return this.returnNullOn404({
|
||||
value: await r2Object.json(),
|
||||
lastModified: r2Object.uploaded.getTime(),
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Failed to get from cache', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
//TODO: This is a workaround to handle 404 responses in the cache.
|
||||
// It should be handled by OpenNext cache interception directly. This should be removed once OpenNext cache interception is fixed.
|
||||
returnNullOn404<CacheType extends CacheEntryType = 'cache'>(
|
||||
cacheEntry: WithLastModified<CacheValue<CacheType>> | null
|
||||
): WithLastModified<CacheValue<CacheType>> | null {
|
||||
if (!cacheEntry?.value) return null;
|
||||
if ('meta' in cacheEntry.value && cacheEntry.value.meta?.status === 404) {
|
||||
return null;
|
||||
}
|
||||
return cacheEntry;
|
||||
}
|
||||
|
||||
async set<CacheType extends CacheEntryType = 'cache'>(
|
||||
key: string,
|
||||
value: CacheValue<CacheType>,
|
||||
cacheType?: CacheType
|
||||
): Promise<void> {
|
||||
const cacheKey = this.getR2Key(key, cacheType);
|
||||
return trace(
|
||||
{
|
||||
operation: 'openNextIncrementalCacheSet',
|
||||
name: cacheKey,
|
||||
},
|
||||
async (span) => {
|
||||
span.setAttribute('cacheType', cacheType ?? 'cache');
|
||||
const localCache = await this.getCacheInstance();
|
||||
|
||||
try {
|
||||
await this.writeToR2(cacheKey, JSON.stringify(value));
|
||||
|
||||
//TODO: Check if there is any places where we don't have tags
|
||||
// Ideally we should always have tags, but in case we don't, we need to decide how to handle it
|
||||
// For now we default to a build ID tag, which allow us to invalidate the cache in case something is wrong in this deployment
|
||||
const tags = this.getTagsFromCacheEntry(value) ?? [
|
||||
`build_id/${process.env.NEXT_BUILD_ID}`,
|
||||
];
|
||||
|
||||
// We consider R2 as the source of truth, so we update the local cache
|
||||
// only after a successful R2 write
|
||||
await localCache.put(
|
||||
this.getCacheUrlKey(cacheKey),
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
value,
|
||||
// Note: `Date.now()` returns the time of the last IO rather than the actual time.
|
||||
// See https://developers.cloudflare.com/workers/reference/security-model/
|
||||
lastModified: Date.now(),
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
// Cache-Control default to 30 minutes, will be overridden by `revalidate`
|
||||
// In theory we should always get the `revalidate` value
|
||||
'cache-control': `max-age=${value.revalidate ?? 60 * 30}`,
|
||||
'cache-tag': tags.join(','),
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('Failed to set to cache', e);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
const cacheKey = this.getR2Key(key);
|
||||
return trace(
|
||||
{
|
||||
operation: 'openNextIncrementalCacheDelete',
|
||||
name: cacheKey,
|
||||
},
|
||||
async () => {
|
||||
const r2 = getCloudflareContext().env[BINDING_NAME];
|
||||
const localCache = await this.getCacheInstance();
|
||||
if (!r2) throw new Error('No R2 bucket');
|
||||
|
||||
try {
|
||||
await r2.delete(cacheKey);
|
||||
|
||||
// Here again R2 is the source of truth, so we delete from local cache first
|
||||
await localCache.delete(this.getCacheUrlKey(cacheKey));
|
||||
} catch (e) {
|
||||
console.error('Failed to delete from cache', e);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async writeToR2(key: string, value: string): Promise<void> {
|
||||
const env = getCloudflareContext().env as {
|
||||
WRITE_BUFFER: DurableObjectNamespace<
|
||||
Rpc.DurableObjectBranded & {
|
||||
write: (key: string, value: string) => Promise<void>;
|
||||
}
|
||||
>;
|
||||
};
|
||||
const id = env.WRITE_BUFFER.idFromName(key);
|
||||
|
||||
// A stub is a client used to invoke methods on the Durable Object
|
||||
const stub = env.WRITE_BUFFER.get(id);
|
||||
|
||||
await stub.write(key, value);
|
||||
}
|
||||
|
||||
async getCacheInstance(): Promise<Cache> {
|
||||
if (this.localCache) return this.localCache;
|
||||
this.localCache = await caches.open('incremental-cache');
|
||||
return this.localCache;
|
||||
}
|
||||
|
||||
// Utility function to generate keys for R2/Cache API
|
||||
getR2Key(initialKey: string, cacheType: CacheEntryType = 'cache'): string {
|
||||
let key = initialKey;
|
||||
if (cacheType === 'composable') {
|
||||
// For composable cache, we need to discard the build ID from the key
|
||||
const [_buildId, ...restOfTheKey] = JSON.parse(initialKey);
|
||||
key = JSON.stringify([...restOfTheKey]);
|
||||
}
|
||||
|
||||
const hash = createHash('sha256').update(key).digest('hex');
|
||||
return `${DEFAULT_PREFIX}/${cacheType === 'cache' ? process.env?.NEXT_BUILD_ID : 'dataCache'}/${hash}.${cacheType}`.replace(
|
||||
/\/+/g,
|
||||
'/'
|
||||
);
|
||||
}
|
||||
|
||||
getCacheUrlKey(cacheKey: string): string {
|
||||
return `http://cache.local/${cacheKey}`;
|
||||
}
|
||||
|
||||
getTagsFromCacheEntry<CacheType extends CacheEntryType>(
|
||||
entry: CacheValue<CacheType>
|
||||
): string[] | undefined {
|
||||
if ('tags' in entry && entry.tags) {
|
||||
return entry.tags;
|
||||
}
|
||||
|
||||
if ('meta' in entry && entry.meta && 'headers' in entry.meta && entry.meta.headers) {
|
||||
const rawTags = entry.meta.headers['x-next-cache-tags'];
|
||||
if (typeof rawTags === 'string') {
|
||||
return rawTags.split(',');
|
||||
}
|
||||
}
|
||||
if ('value' in entry) {
|
||||
return entry.tags;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new GitbookIncrementalCache();
|
||||
@@ -0,0 +1,137 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import type {
|
||||
CacheEntryType,
|
||||
CacheValue,
|
||||
IncrementalCache,
|
||||
WithLastModified,
|
||||
} from '@opennextjs/aws/types/overrides.js';
|
||||
import { getCloudflareContext } from '@opennextjs/cloudflare';
|
||||
|
||||
import type { DurableObjectNamespace, Rpc } from '@cloudflare/workers-types';
|
||||
|
||||
export const BINDING_NAME = 'NEXT_INC_CACHE_R2_BUCKET';
|
||||
export const DEFAULT_PREFIX = 'incremental-cache';
|
||||
|
||||
export type KeyOptions = {
|
||||
cacheType?: CacheEntryType;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* It is very similar to the `R2IncrementalCache` in the `@opennextjs/cloudflare` package, but it has an additional
|
||||
* R2WriteBuffer Durable Object to handle writes to R2. Given how we set up cache, we often end up writing to the same key too fast.
|
||||
*/
|
||||
export class GitbookIncrementalCache implements IncrementalCache {
|
||||
name = 'GitbookIncrementalCache';
|
||||
|
||||
async get<CacheType extends CacheEntryType = 'cache'>(
|
||||
key: string,
|
||||
cacheType?: CacheType
|
||||
): Promise<WithLastModified<CacheValue<CacheType>> | null> {
|
||||
const cacheKey = this.getR2Key(key, cacheType);
|
||||
|
||||
const r2 = getCloudflareContext().env[BINDING_NAME];
|
||||
if (!r2) throw new Error('No R2 bucket');
|
||||
if (process.env.SHOULD_BYPASS_CACHE === 'true') {
|
||||
// We are in a local middleware environment, we should bypass the cache
|
||||
// and go directly to the server.
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const r2Object = await r2.get(cacheKey);
|
||||
if (!r2Object) return null;
|
||||
|
||||
const json = (await r2Object.json()) as CacheValue<CacheType>;
|
||||
const lastModified = r2Object.uploaded.getTime();
|
||||
|
||||
if (!json) return null;
|
||||
|
||||
return this.returnNullOn404({
|
||||
value: json,
|
||||
lastModified,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Failed to get from cache', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: This is a workaround to handle 404 responses in the cache.
|
||||
// It should be handled by OpenNext cache interception directly. This should be removed once OpenNext cache interception is fixed.
|
||||
returnNullOn404<CacheType extends CacheEntryType = 'cache'>(
|
||||
cacheEntry: WithLastModified<CacheValue<CacheType>> | null
|
||||
): WithLastModified<CacheValue<CacheType>> | null {
|
||||
if (!cacheEntry?.value) return null;
|
||||
if ('meta' in cacheEntry.value && cacheEntry.value.meta?.status === 404) {
|
||||
return null;
|
||||
}
|
||||
return cacheEntry;
|
||||
}
|
||||
|
||||
async set<CacheType extends CacheEntryType = 'cache'>(
|
||||
key: string,
|
||||
value: CacheValue<CacheType>,
|
||||
cacheType?: CacheType
|
||||
): Promise<void> {
|
||||
const cacheKey = this.getR2Key(key, cacheType);
|
||||
|
||||
try {
|
||||
await this.writeToR2(cacheKey, JSON.stringify(value));
|
||||
} catch (e) {
|
||||
console.error('Failed to set to cache', e);
|
||||
}
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
const cacheKey = this.getR2Key(key);
|
||||
|
||||
const r2 = getCloudflareContext().env[BINDING_NAME];
|
||||
if (!r2) throw new Error('No R2 bucket');
|
||||
|
||||
try {
|
||||
await r2.delete(cacheKey);
|
||||
} catch (e) {
|
||||
console.error('Failed to delete from cache', e);
|
||||
}
|
||||
}
|
||||
|
||||
async writeToR2(key: string, value: string): Promise<void> {
|
||||
try {
|
||||
const env = getCloudflareContext().env as {
|
||||
WRITE_BUFFER: DurableObjectNamespace<
|
||||
Rpc.DurableObjectBranded & {
|
||||
write: (key: string, value: string) => Promise<void>;
|
||||
}
|
||||
>;
|
||||
};
|
||||
const id = env.WRITE_BUFFER.idFromName(key);
|
||||
|
||||
// A stub is a client used to invoke methods on the Durable Object
|
||||
const stub = env.WRITE_BUFFER.get(id);
|
||||
|
||||
await stub.write(key, value);
|
||||
} catch {
|
||||
// We fallback to writing directly to R2
|
||||
// it can fail locally because the limit is 1Mb per args
|
||||
// It is 32Mb in production, so we should be fine
|
||||
const r2 = getCloudflareContext().env[BINDING_NAME];
|
||||
r2?.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function to generate keys for R2/Cache API
|
||||
getR2Key(initialKey: string, cacheType: CacheEntryType = 'cache'): string {
|
||||
let key = initialKey;
|
||||
if (cacheType === 'composable') {
|
||||
// For composable cache, we need to discard the build ID from the key
|
||||
const [_buildId, ...restOfTheKey] = JSON.parse(initialKey);
|
||||
key = JSON.stringify([...restOfTheKey]);
|
||||
}
|
||||
|
||||
const hash = createHash('sha256').update(key).digest('hex');
|
||||
return `${DEFAULT_PREFIX}/${cacheType === 'cache' ? process.env?.NEXT_BUILD_ID : 'dataCache'}/${hash}.${cacheType}`.replace(
|
||||
/\/+/g,
|
||||
'/'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { withRegionalCache } from '@opennextjs/cloudflare/overrides/incremental-cache/regional-cache';
|
||||
import { GitbookIncrementalCache } from './incrementalCache';
|
||||
|
||||
export default withRegionalCache(new GitbookIncrementalCache(), {
|
||||
mode: 'long-lived',
|
||||
// We can do it because we use our own logic to invalidate the cache
|
||||
bypassTagCacheOnCacheHit: true,
|
||||
defaultLongLivedTtlSec: 60 * 60 * 24 /* 24 hours */,
|
||||
// We don't want to update the cache entry on every cache hit
|
||||
shouldLazilyUpdateOnCacheHit: false,
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user