mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 14:00:28 +00:00
Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 725952a106 | |||
| 3319375e9a | |||
| c5a4619020 | |||
| 989fc69abe | |||
| 0924259217 | |||
| 05affac34f | |||
| 453a459566 | |||
| 722f02ea09 | |||
| 5bcea2fe3b | |||
| 73afd1f5d0 | |||
| 9b8a0d3e54 | |||
| 75bd98d66c | |||
| 9b914d10bd | |||
| 3e11678d8d | |||
| 6e03638130 | |||
| 23f884efb7 | |||
| 2ae76f999b | |||
| 685325cdc7 | |||
| ab132eadb7 | |||
| 14504e43b5 | |||
| 4ed79574e8 | |||
| 31bcd1a386 | |||
| 270e5ef7ad | |||
| 1f4733355e | |||
| 027a859ef4 | |||
| 59f50eaee7 | |||
| 80d2733c25 | |||
| feb6735a4d | |||
| f1d1d2fd81 | |||
| a749c67803 | |||
| c808bb1483 | |||
| e24206ebf0 | |||
| 3410785a3a | |||
| b4a12d606e | |||
| a0545545d7 | |||
| 1f11650ca1 | |||
| da55facf2c | |||
| dc2dbc5710 | |||
| bdd6303bcc | |||
| 66d0fc0683 | |||
| 05e1d8cd96 | |||
| 9f0de74caa | |||
| a820739bd2 | |||
| 304042017c | |||
| 82cd9f2979 | |||
| a3f1fea27b | |||
| bb5c6a42e7 | |||
| 445baaaa61 | |||
| 7419ee7dba | |||
| aa6be381a4 | |||
| 61575838f3 | |||
| 359bb979f8 |
@@ -1 +0,0 @@
|
|||||||
1.2.1
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
name: 'Deploy vercel'
|
||||||
|
description: 'Deploy GitBook to Vercel'
|
||||||
|
inputs:
|
||||||
|
vercel-org:
|
||||||
|
description: 'Vercel organization'
|
||||||
|
required: true
|
||||||
|
vercel-project:
|
||||||
|
description: 'Vercel project'
|
||||||
|
required: true
|
||||||
|
vercel-token:
|
||||||
|
description: 'Vercel token'
|
||||||
|
required: true
|
||||||
|
environment:
|
||||||
|
description: 'Environment to deploy to'
|
||||||
|
required: true
|
||||||
|
outputs:
|
||||||
|
deployment-url:
|
||||||
|
description: "Deployment URL"
|
||||||
|
value: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
|
runs:
|
||||||
|
using: 'composite'
|
||||||
|
steps:
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: ./.github/composite/setup-bun
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||||
|
- name: Sets env vars for environment
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "VERCEL_ENVIRONMENT=${{ inputs.environment }}" >> $GITHUB_ENV
|
||||||
|
- name: Pull Vercel Environment Information
|
||||||
|
run: bun run vercel pull --yes --environment=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }}
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||||
|
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||||
|
- name: Build Project Artifacts
|
||||||
|
run: bun run vercel build --target=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }}
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||||
|
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||||
|
- name: Deploy Project Artifacts to Vercel
|
||||||
|
id: deploy
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --target=$VERCEL_ENVIRONMENT --token=${{ inputs.vercel-token }})
|
||||||
|
echo "deployment-url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
|
||||||
|
env:
|
||||||
|
VERCEL_ORG_ID: ${{ inputs.vercel-org }}
|
||||||
|
VERCEL_PROJECT_ID: ${{ inputs.vercel-project }}
|
||||||
|
- name: Outputs
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
||||||
@@ -15,4 +15,4 @@ runs:
|
|||||||
- name: Setup bun
|
- name: Setup bun
|
||||||
uses: oven-sh/setup-bun@v2
|
uses: oven-sh/setup-bun@v2
|
||||||
with:
|
with:
|
||||||
bun-version-file: '.bun-version'
|
bun-version-file: 'package.json'
|
||||||
|
|||||||
+86
-74
@@ -39,7 +39,7 @@ jobs:
|
|||||||
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
|
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
|
||||||
if: 1 && !startsWith(github.ref, 'refs/heads/main')
|
if: 1 && !startsWith(github.ref, 'refs/heads/main')
|
||||||
- name: Build Next.js with next-on-pages
|
- name: Build Next.js with next-on-pages
|
||||||
run: bun run turbo build:cloudflare
|
run: bun run turbo gitbook#build:cloudflare
|
||||||
env:
|
env:
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
||||||
@@ -49,7 +49,7 @@ jobs:
|
|||||||
SENTRY_RELEASE: ${{ github.sha }}
|
SENTRY_RELEASE: ${{ github.sha }}
|
||||||
- id: deploy
|
- id: deploy
|
||||||
name: Deploy to Cloudflare
|
name: Deploy to Cloudflare
|
||||||
uses: cloudflare/wrangler-action@v3.11.0
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
@@ -87,48 +87,41 @@ jobs:
|
|||||||
statuses: write
|
statuses: write
|
||||||
outputs:
|
outputs:
|
||||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
env:
|
|
||||||
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
|
|
||||||
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup Bun
|
- name: Deploy ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||||
uses: ./.github/composite/setup-bun
|
|
||||||
- name: Install dependencies
|
|
||||||
run: bun install --frozen-lockfile
|
|
||||||
env:
|
|
||||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
|
||||||
- name: Sets env vars for production
|
|
||||||
run: |
|
|
||||||
echo "VERCEL_ENVIRONMENT=production" >> $GITHUB_ENV
|
|
||||||
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
|
|
||||||
echo "GITBOOK_ASSETS_PREFIX=https://static.gitbook.com" >> $GITHUB_ENV
|
|
||||||
if: startsWith(github.ref, 'refs/heads/main')
|
|
||||||
- name: Sets env vars for preview
|
|
||||||
run: |
|
|
||||||
echo "VERCEL_ENVIRONMENT=preview" >> $GITHUB_ENV
|
|
||||||
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
|
|
||||||
if: 1 && !startsWith(github.ref, 'refs/heads/main')
|
|
||||||
- name: Pull Vercel Environment Information
|
|
||||||
run: bun run vercel pull --yes --environment=$VERCEL_ENVIRONMENT --token=${{ secrets.VERCEL_TOKEN }}
|
|
||||||
- name: Build Project Artifacts
|
|
||||||
run: bun run vercel build --token=${{ secrets.VERCEL_TOKEN }}
|
|
||||||
env:
|
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
|
||||||
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
|
|
||||||
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
|
|
||||||
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
|
|
||||||
SENTRY_DSN: ${{ vars.SENTRY_DSN }}
|
|
||||||
SENTRY_RELEASE: ${{ github.sha }}
|
|
||||||
- name: Deploy Project Artifacts to Vercel
|
|
||||||
id: deploy
|
id: deploy
|
||||||
run: |
|
uses: ./.github/composite/deploy-vercel
|
||||||
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }})
|
with:
|
||||||
echo "deployment-url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
|
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
|
||||||
- name: Outputs
|
vercel-org: ${{ secrets.VERCEL_ORG_ID }}
|
||||||
run: |
|
vercel-project: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||||
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
|
vercel-token: ${{ secrets.VERCEL_TOKEN }}
|
||||||
|
deploy-v2-vercel-staging:
|
||||||
|
name: Deploy v2 to Vercel (staging)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
deployments: write
|
||||||
|
issues: write
|
||||||
|
pull-requests: write
|
||||||
|
checks: write
|
||||||
|
statuses: write
|
||||||
|
if: startsWith(github.ref, 'refs/heads/main')
|
||||||
|
outputs:
|
||||||
|
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Deploy staging
|
||||||
|
id: deploy
|
||||||
|
uses: ./.github/composite/deploy-vercel
|
||||||
|
with:
|
||||||
|
environment: staging
|
||||||
|
vercel-org: ${{ secrets.VERCEL_ORG_ID }}
|
||||||
|
vercel-project: ${{ secrets.VERCEL_PROJECT_ID }}
|
||||||
|
vercel-token: ${{ secrets.VERCEL_TOKEN }}
|
||||||
deploy-v2-cloudflare:
|
deploy-v2-cloudflare:
|
||||||
name: Deploy v2 to Cloudflare Worker
|
name: Deploy v2 to Cloudflare Worker
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -141,9 +134,6 @@ jobs:
|
|||||||
statuses: write
|
statuses: write
|
||||||
outputs:
|
outputs:
|
||||||
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
|
||||||
env:
|
|
||||||
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
|
|
||||||
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -156,7 +146,8 @@ jobs:
|
|||||||
- name: Sets env vars for production
|
- name: Sets env vars for production
|
||||||
run: |
|
run: |
|
||||||
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
|
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
|
||||||
echo "GITBOOK_ASSETS_PREFIX=https://static.gitbook.com" >> $GITHUB_ENV
|
echo "GITBOOK_URL=https://open-2c.gitbook.com" >> $GITHUB_ENV
|
||||||
|
echo "GITBOOK_ASSETS_PREFIX=https://static-2c.gitbook.com" >> $GITHUB_ENV
|
||||||
if: startsWith(github.ref, 'refs/heads/main')
|
if: startsWith(github.ref, 'refs/heads/main')
|
||||||
- name: Sets env vars for preview
|
- name: Sets env vars for preview
|
||||||
run: |
|
run: |
|
||||||
@@ -173,7 +164,7 @@ jobs:
|
|||||||
SENTRY_RELEASE: ${{ github.sha }}
|
SENTRY_RELEASE: ${{ github.sha }}
|
||||||
- id: deploy
|
- id: deploy
|
||||||
name: Deploy to Cloudflare
|
name: Deploy to Cloudflare
|
||||||
uses: cloudflare/wrangler-action@v3.11.0
|
uses: cloudflare/wrangler-action@v3.14.0
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
||||||
@@ -226,13 +217,14 @@ jobs:
|
|||||||
|
|
||||||
| Site | v1 | v2 |
|
| Site | v1 | v2 |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| GitBook | [${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com](${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/static/url/docs.gitbook.com](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/static/url/docs.gitbook.com) |
|
| GitBook | [${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com](${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com) |
|
||||||
| E2E | [${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/static/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/static/url/gitbook.gitbook.io/test-gitbook-open) |
|
| E2E | [${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) |
|
||||||
edit-mode: replace
|
edit-mode: replace
|
||||||
visual-testing:
|
visual-testing:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Visual Testing
|
name: Visual Testing
|
||||||
needs: deploy
|
needs: deploy
|
||||||
|
timeout-minutes: 6
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -245,18 +237,33 @@ jobs:
|
|||||||
- name: Run Playwright tests
|
- name: Run Playwright tests
|
||||||
run: bun e2e
|
run: bun e2e
|
||||||
env:
|
env:
|
||||||
BASE_URL: ${{needs.deploy.outputs.deployment-url}}
|
BASE_URL: ${{ needs.deploy.outputs.deployment-url }}
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
- uses: actions/upload-artifact@v4
|
visual-testing-v2:
|
||||||
if: ${{ !cancelled() }}
|
runs-on: ubuntu-latest
|
||||||
with:
|
name: Visual Testing v2
|
||||||
name: playwright-test-results
|
needs: deploy-v2-vercel
|
||||||
path: packages/gitbook/test-results/
|
timeout-minutes: 6
|
||||||
retention-days: 3
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: ./.github/composite/setup-bun
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
- name: Setup Playwright
|
||||||
|
uses: ./.github/actions/setup-playwright
|
||||||
|
- name: Run Playwright tests
|
||||||
|
run: bun e2e
|
||||||
|
env:
|
||||||
|
BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
||||||
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
|
ARGOS_BUILD_NAME: 'v2'
|
||||||
visual-testing-customers:
|
visual-testing-customers:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Visual Testing Customers
|
name: Visual Testing Customers
|
||||||
needs: deploy
|
needs: deploy
|
||||||
|
timeout-minutes: 6
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -269,15 +276,29 @@ jobs:
|
|||||||
- name: Run Playwright tests
|
- name: Run Playwright tests
|
||||||
run: bun e2e-customers
|
run: bun e2e-customers
|
||||||
env:
|
env:
|
||||||
BASE_URL: ${{needs.deploy.outputs.deployment-url}}
|
BASE_URL: ${{ needs.deploy.outputs.deployment-url }}
|
||||||
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
ARGOS_BUILD_NAME: 'customers'
|
ARGOS_BUILD_NAME: 'customers'
|
||||||
- uses: actions/upload-artifact@v4
|
visual-testing-customers-v2:
|
||||||
if: ${{ !cancelled() }}
|
runs-on: ubuntu-latest
|
||||||
with:
|
name: Visual Testing Customers v2
|
||||||
name: playwright-test-results-customers
|
needs: deploy-v2-vercel
|
||||||
path: packages/gitbook/test-results/
|
timeout-minutes: 6
|
||||||
retention-days: 3
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: ./.github/composite/setup-bun
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
- name: Setup Playwright
|
||||||
|
uses: ./.github/actions/setup-playwright
|
||||||
|
- name: Run Playwright tests
|
||||||
|
run: bun e2e-customers
|
||||||
|
env:
|
||||||
|
BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
|
||||||
|
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
|
||||||
|
ARGOS_BUILD_NAME: 'customers-v2'
|
||||||
pagespeed-testing:
|
pagespeed-testing:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: PageSpeed Testing
|
name: PageSpeed Testing
|
||||||
@@ -299,6 +320,7 @@ jobs:
|
|||||||
format:
|
format:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Format
|
name: Format
|
||||||
|
timeout-minutes: 6
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -309,22 +331,10 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
PUPPETEER_SKIP_DOWNLOAD: 1
|
PUPPETEER_SKIP_DOWNLOAD: 1
|
||||||
- run: bun format:check
|
- run: bun format:check
|
||||||
lint:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
name: Lint
|
|
||||||
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
|
|
||||||
- run: bun lint --no-cache
|
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Test
|
name: Test
|
||||||
|
timeout-minutes: 6
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -339,6 +349,7 @@ jobs:
|
|||||||
# CI to check that the repository builds correctly on a machine without the credentials
|
# CI to check that the repository builds correctly on a machine without the credentials
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Build (Open Source)
|
name: Build (Open Source)
|
||||||
|
timeout-minutes: 6
|
||||||
env:
|
env:
|
||||||
NPM_TOKEN_READONLY: ''
|
NPM_TOKEN_READONLY: ''
|
||||||
steps:
|
steps:
|
||||||
@@ -354,6 +365,7 @@ jobs:
|
|||||||
typecheck:
|
typecheck:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Typecheck
|
name: Typecheck
|
||||||
|
timeout-minutes: 6
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ jobs:
|
|||||||
uses: changesets/action@v1
|
uses: changesets/action@v1
|
||||||
with:
|
with:
|
||||||
publish: npm run release
|
publish: npm run release
|
||||||
|
version: npm run changeset-version
|
||||||
env:
|
env:
|
||||||
# Using a PAT instead of GITHUB_TOKEN because we need to run workflows when releases are created
|
# 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
|
# https://github.com/orgs/community/discussions/26875#discussioncomment-3253761
|
||||||
|
|||||||
@@ -22,3 +22,6 @@ yarn-error.log*
|
|||||||
|
|
||||||
# Env files
|
# Env files
|
||||||
.env.local
|
.env.local
|
||||||
|
|
||||||
|
# TypeScript
|
||||||
|
*.tsbuildinfo
|
||||||
|
|||||||
+4
-1
@@ -11,4 +11,7 @@ packages/icons/src/data/*.json
|
|||||||
dist/
|
dist/
|
||||||
|
|
||||||
README.md
|
README.md
|
||||||
CHANGELOG.md
|
CHANGELOG.md
|
||||||
|
|
||||||
|
# v2
|
||||||
|
packages/gitbook-v2/public/~gitbook/static/
|
||||||
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"recommendations": ["biomejs.biome"]
|
||||||
|
}
|
||||||
Vendored
+6
-1
@@ -7,5 +7,10 @@
|
|||||||
["style \\=([^;]*);", "\"([^\"]*)\""],
|
["style \\=([^;]*);", "\"([^\"]*)\""],
|
||||||
["style \\=([^;]*);", "\\`([^\\`]*)\\`"]
|
["style \\=([^;]*);", "\\`([^\\`]*)\\`"]
|
||||||
],
|
],
|
||||||
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"]
|
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"],
|
||||||
|
"prettier.enable": false,
|
||||||
|
"editor.defaultFormatter": "biomejs.biome",
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.organizeImports": "explicit"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+168
@@ -0,0 +1,168 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
|
||||||
|
"vcs": {
|
||||||
|
"enabled": false,
|
||||||
|
"clientKind": "git",
|
||||||
|
"useIgnoreFile": false
|
||||||
|
},
|
||||||
|
"files": {
|
||||||
|
"ignoreUnknown": false,
|
||||||
|
"ignore": [
|
||||||
|
"**/node_modules/**/*",
|
||||||
|
"**/dist/**/*",
|
||||||
|
"**/build/**/*",
|
||||||
|
"**/public/**/*",
|
||||||
|
"**/.next/**/*",
|
||||||
|
"**/.open-next/**/*",
|
||||||
|
"**/.turbo/**/*",
|
||||||
|
"**/.vercel/**/*",
|
||||||
|
"**/.cache/**/*",
|
||||||
|
"**/.wrangler/**/*",
|
||||||
|
"packages/openapi-parser/src/fixtures/**/*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"formatter": {
|
||||||
|
"enabled": true,
|
||||||
|
"useEditorconfig": true,
|
||||||
|
"formatWithErrors": false,
|
||||||
|
"indentStyle": "space",
|
||||||
|
"indentWidth": 4,
|
||||||
|
"lineEnding": "lf",
|
||||||
|
"lineWidth": 100,
|
||||||
|
"attributePosition": "auto",
|
||||||
|
"bracketSpacing": true
|
||||||
|
},
|
||||||
|
"organizeImports": {
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
"linter": {
|
||||||
|
"enabled": true,
|
||||||
|
"rules": {
|
||||||
|
"recommended": true,
|
||||||
|
"performance": {
|
||||||
|
"noDelete": "warn"
|
||||||
|
},
|
||||||
|
"security": {
|
||||||
|
"noDangerouslySetInnerHtml": "off"
|
||||||
|
},
|
||||||
|
"complexity": {
|
||||||
|
"noForEach": "off",
|
||||||
|
"noUselessFragments": "warn",
|
||||||
|
"noBannedTypes": "warn"
|
||||||
|
},
|
||||||
|
"correctness": {
|
||||||
|
"noUndeclaredVariables": "error",
|
||||||
|
"noUnusedVariables": "error",
|
||||||
|
"useArrayLiterals": "error",
|
||||||
|
"useHookAtTopLevel": "error",
|
||||||
|
"noUnusedImports": "error",
|
||||||
|
"noVoidElementsWithChildren": "warn",
|
||||||
|
"useJsxKeyInIterable": "warn",
|
||||||
|
"useExhaustiveDependencies": "warn",
|
||||||
|
"noUnknownFunction": "warn"
|
||||||
|
},
|
||||||
|
"style": {
|
||||||
|
"noNonNullAssertion": "warn",
|
||||||
|
"noParameterAssign": "off",
|
||||||
|
"useThrowOnlyError": "error"
|
||||||
|
},
|
||||||
|
"suspicious": {
|
||||||
|
"noConsole": "warn",
|
||||||
|
"noExplicitAny": "warn",
|
||||||
|
"noImplicitAnyLet": "warn",
|
||||||
|
"noConfusingVoidType": "warn",
|
||||||
|
"noControlCharactersInRegex": "warn",
|
||||||
|
"noPrototypeBuiltins": "warn",
|
||||||
|
"noAssignInExpressions": "warn",
|
||||||
|
"noArrayIndexKey": "warn"
|
||||||
|
},
|
||||||
|
"a11y": {
|
||||||
|
"useSemanticElements": "warn",
|
||||||
|
"useKeyWithClickEvents": "warn",
|
||||||
|
"noSvgWithoutTitle": "warn",
|
||||||
|
"useButtonType": "warn",
|
||||||
|
"useIframeTitle": "warn",
|
||||||
|
"useAltText": "warn",
|
||||||
|
"noPositiveTabindex": "warn",
|
||||||
|
"useFocusableInteractive": "warn",
|
||||||
|
"useAriaPropsForRole": "warn",
|
||||||
|
"useValidAnchor": "warn",
|
||||||
|
"noLabelWithoutControl": "warn",
|
||||||
|
"noNoninteractiveTabindex": "warn"
|
||||||
|
},
|
||||||
|
"nursery": {
|
||||||
|
"useSortedClasses": {
|
||||||
|
"level": "error",
|
||||||
|
"fix": "safe",
|
||||||
|
"options": {
|
||||||
|
"attributes": ["class", "className", "style"],
|
||||||
|
"functions": ["clsx", "tw"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"javascript": {
|
||||||
|
"formatter": {
|
||||||
|
"jsxQuoteStyle": "double",
|
||||||
|
"quoteProperties": "asNeeded",
|
||||||
|
"trailingCommas": "es5",
|
||||||
|
"semicolons": "always",
|
||||||
|
"arrowParentheses": "always",
|
||||||
|
"bracketSameLine": false,
|
||||||
|
"quoteStyle": "single",
|
||||||
|
"attributePosition": "auto",
|
||||||
|
"bracketSpacing": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"include": [
|
||||||
|
"packages/gitbook/**/*",
|
||||||
|
"packages/gitbook-v2/**/*",
|
||||||
|
"packages/react-openapi/**/*",
|
||||||
|
"packages/react-math/**/*",
|
||||||
|
"packages/react-contentkit/**/*",
|
||||||
|
"packages/icons/**/*"
|
||||||
|
],
|
||||||
|
"javascript": {
|
||||||
|
"globals": ["React"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": ["packages/gitbook/**/*"],
|
||||||
|
"javascript": {
|
||||||
|
"globals": ["React", "GitBookIntegrationEvent"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": ["*.css"],
|
||||||
|
"javascript": {
|
||||||
|
"globals": ["theme"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": ["*.test.ts", "packages/gitbook/tests/**/*"],
|
||||||
|
"javascript": {
|
||||||
|
"globals": ["Bun"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": [
|
||||||
|
"packages/cache-do/**/*",
|
||||||
|
"packages/gitbook/cf-env.d.ts",
|
||||||
|
"packages/gitbook/src/cloudflare-entrypoint.ts"
|
||||||
|
],
|
||||||
|
"javascript": {
|
||||||
|
"globals": [
|
||||||
|
"DurableObjectLocationHint",
|
||||||
|
"DurableObjectNamespace",
|
||||||
|
"DurableObjectStub",
|
||||||
|
"ContinentCode",
|
||||||
|
"Fetcher",
|
||||||
|
"ExportedHandler"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# Caching
|
|
||||||
|
|
||||||
## Revalidating the cache
|
|
||||||
|
|
||||||
Invalidate cache can be done at two levels using tags:
|
|
||||||
|
|
||||||
- Data fetching cache
|
|
||||||
- Rendering cache
|
|
||||||
|
|
||||||
To invalidate and refetch the data cache, you can execute a POST request to `/~/gitbook/revalidate`:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
curl --location --request POST 'https://gitbook/mycompany.com/~gitbook/revalidate' \
|
|
||||||
--header 'Content-Type: application/json' \
|
|
||||||
--data-raw '{"tags": ["space.id"]}'
|
|
||||||
```
|
|
||||||
|
|
||||||
To invalidate the rendering cache, the implementation mainly depends on the infrastructure serving the content, GitBook outputs a `Cache-Tag` header on every requests. The value of the header is a comma separated list of tags.
|
|
||||||
|
|
||||||
## Purging the cache
|
|
||||||
|
|
||||||
Purging the cache, without revalidating, is done by passing `"purge": true` in the request body.
|
|
||||||
+7
-10
@@ -2,12 +2,12 @@
|
|||||||
"name": "gitbook",
|
"name": "gitbook",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@biomejs/biome": "^1.9.4",
|
||||||
"@changesets/cli": "^2.27.12",
|
"@changesets/cli": "^2.27.12",
|
||||||
"prettier": "^3.0.3",
|
"turbo": "^2.4.4",
|
||||||
"turbo": "^2.4.1-canary.2",
|
|
||||||
"vercel": "^39.3.0"
|
"vercel": "^39.3.0"
|
||||||
},
|
},
|
||||||
"packageManager": "bun@1.1.18",
|
"packageManager": "bun@1.2.4",
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"@codemirror/state": "6.4.1",
|
"@codemirror/state": "6.4.1",
|
||||||
"react": "18.3.1",
|
"react": "18.3.1",
|
||||||
@@ -20,23 +20,20 @@
|
|||||||
"build": "turbo run build",
|
"build": "turbo run build",
|
||||||
"build:v2": "turbo run build:v2",
|
"build:v2": "turbo run build:v2",
|
||||||
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
|
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
|
||||||
"lint": "turbo run lint",
|
|
||||||
"lint:fix": "turbo run lint -- --fix",
|
|
||||||
"typecheck": "turbo run typecheck",
|
"typecheck": "turbo run typecheck",
|
||||||
"format": "prettier ./ --ignore-unknown --write",
|
"format": "biome check --write ./",
|
||||||
"format:check": "prettier ./ --ignore-unknown --list-different",
|
"format:check": "biome check --diagnostic-level=error ./",
|
||||||
"unit": "turbo run unit",
|
"unit": "turbo run unit",
|
||||||
"e2e": "turbo run e2e",
|
"e2e": "turbo run e2e",
|
||||||
"e2e-customers": "turbo run e2e-customers",
|
"e2e-customers": "turbo run e2e-customers",
|
||||||
"changeset": "changeset",
|
"changeset": "changeset",
|
||||||
|
"changeset-version": "changeset version && bun run format",
|
||||||
"release": "turbo run release && changeset publish",
|
"release": "turbo run release && changeset publish",
|
||||||
"release:preview": "turbo run release:preview",
|
"release:preview": "turbo run release:preview",
|
||||||
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
|
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
|
||||||
"clean": "turbo run clean"
|
"clean": "turbo run clean"
|
||||||
},
|
},
|
||||||
"workspaces": [
|
"workspaces": ["packages/*"],
|
||||||
"packages/*"
|
|
||||||
],
|
|
||||||
"patchedDependencies": {
|
"patchedDependencies": {
|
||||||
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
|
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
|
||||||
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch"
|
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch"
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5.5.3",
|
"typescript": "^5.5.3",
|
||||||
"wrangler": "3.82.0"
|
"wrangler": "^3.109.2"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"generate": "wrangler types --experimental-include-runtime",
|
"generate": "wrangler types --experimental-include-runtime",
|
||||||
@@ -31,12 +31,5 @@
|
|||||||
"release": "wrangler deploy",
|
"release": "wrangler deploy",
|
||||||
"release:preview": "wrangler deploy && wrangler deploy --env preview"
|
"release:preview": "wrangler deploy && wrangler deploy --env preview"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": ["dist", "src", "bin", "data", "README.md", "CHANGELOG.md"]
|
||||||
"dist",
|
|
||||||
"src",
|
|
||||||
"bin",
|
|
||||||
"data",
|
|
||||||
"README.md",
|
|
||||||
"CHANGELOG.md"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { encode, decode } from '@msgpack/msgpack';
|
|
||||||
import { DurableObject } from 'cloudflare:workers';
|
import { DurableObject } from 'cloudflare:workers';
|
||||||
|
import { decode, encode } from '@msgpack/msgpack';
|
||||||
import { LRUMap } from 'lru_map';
|
import { LRUMap } from 'lru_map';
|
||||||
|
|
||||||
export interface CacheObjectDescriptor {
|
export interface CacheObjectDescriptor {
|
||||||
@@ -137,7 +137,7 @@ export class CacheObject extends DurableObject {
|
|||||||
*/
|
*/
|
||||||
public async purge() {
|
public async purge() {
|
||||||
return this.logOperation({ operation: 'purge' }, async (setLog) => {
|
return this.logOperation({ operation: 'purge' }, async (setLog) => {
|
||||||
let result = new Set<string>();
|
const result = new Set<string>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// List all the keys in the cache object.
|
// List all the keys in the cache object.
|
||||||
@@ -149,11 +149,7 @@ export class CacheObject extends DurableObject {
|
|||||||
entries.forEach((exp) => {
|
entries.forEach((exp) => {
|
||||||
result.add(exp.k);
|
result.add(exp.k);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (_error) {}
|
||||||
// If an error occurs, reset the cache object.
|
|
||||||
// This is a safety mechanism to prevent the cache object from being stuck in a bad state.
|
|
||||||
console.error('Error during purge, resetting the cache object', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.reset();
|
await this.reset();
|
||||||
return Array.from(result);
|
return Array.from(result);
|
||||||
@@ -174,7 +170,7 @@ export class CacheObject extends DurableObject {
|
|||||||
const toDeleteSet = new Set<string>();
|
const toDeleteSet = new Set<string>();
|
||||||
|
|
||||||
for (const [key, exp] of entries) {
|
for (const [key, exp] of entries) {
|
||||||
const timestamp = parseInt(key.split('.')[1]);
|
const timestamp = Number.parseInt(key.split('.')[1]);
|
||||||
if (timestamp < Date.now()) {
|
if (timestamp < Date.now()) {
|
||||||
toDeleteSet.add(key);
|
toDeleteSet.add(key);
|
||||||
for (let i = 0; i < exp.c; i++) {
|
for (let i = 0; i < exp.c; i++) {
|
||||||
@@ -194,10 +190,7 @@ export class CacheObject extends DurableObject {
|
|||||||
if (toDelete.length) {
|
if (toDelete.length) {
|
||||||
await this.ctx.storage.setAlarm(Date.now() + 12 * 60 * 60 * 1000);
|
await this.ctx.storage.setAlarm(Date.now() + 12 * 60 * 60 * 1000);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (_error) {
|
||||||
// If an error occurs, reset the cache object.
|
|
||||||
// This is a safety mechanism to prevent the cache object from being stuck in a bad state.
|
|
||||||
console.error('Error during alarm, reset the cache object', error);
|
|
||||||
await this.reset();
|
await this.reset();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -218,10 +211,10 @@ export class CacheObject extends DurableObject {
|
|||||||
*/
|
*/
|
||||||
async logOperation<T>(
|
async logOperation<T>(
|
||||||
log: Record<string, unknown>,
|
log: Record<string, unknown>,
|
||||||
fn: (update: (log: Record<string, unknown>) => void) => Promise<T>,
|
fn: (update: (log: Record<string, unknown>) => void) => Promise<T>
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const objectId = this.ctx.id.name ?? this.ctx.id.toString();
|
const objectId = this.ctx.id.name ?? this.ctx.id.toString();
|
||||||
let update: Record<string, unknown> = {};
|
const update: Record<string, unknown> = {};
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
try {
|
try {
|
||||||
return await fn((arg) => {
|
return await fn((arg) => {
|
||||||
@@ -265,7 +258,7 @@ function encodeChunks<T>(key: string, value: T): Record<string, Uint8Array> {
|
|||||||
function decodeChunks<T>(entries: Map<string, Uint8Array>): { value: T; size: number } | undefined {
|
function decodeChunks<T>(entries: Map<string, Uint8Array>): { value: T; size: number } | undefined {
|
||||||
const chunks = Array.from(entries.entries())
|
const chunks = Array.from(entries.entries())
|
||||||
.map(([key, value]) => {
|
.map(([key, value]) => {
|
||||||
const index = parseInt(key.split('.').pop()!);
|
const index = Number.parseInt(key.split('.').pop()!);
|
||||||
return [index, value] as const;
|
return [index, value] as const;
|
||||||
})
|
})
|
||||||
.sort(([a], [b]) => a - b)
|
.sort(([a], [b]) => a - b)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { CacheObject, CacheObjectDescriptor } from './CacheObject';
|
import type { CacheObject } from './CacheObject';
|
||||||
|
|
||||||
export type CacheLocationId = ContinentCode;
|
export type CacheLocationId = ContinentCode;
|
||||||
const allLocations: CacheLocationId[] = ['AF', 'AS', 'NA', 'SA', 'AN', 'EU', 'OC'];
|
const allLocations: CacheLocationId[] = ['AF', 'AS', 'NA', 'SA', 'AN', 'EU', 'OC'];
|
||||||
@@ -30,7 +30,7 @@ export class CacheObjectStub {
|
|||||||
/** ID of the location to target */
|
/** ID of the location to target */
|
||||||
private locationId: CacheLocationId,
|
private locationId: CacheLocationId,
|
||||||
/** Name of the tag */
|
/** Name of the tag */
|
||||||
private tag: string,
|
private tag: string
|
||||||
) {
|
) {
|
||||||
const groupId = getCacheObjectIdName(this.locationId, this.tag);
|
const groupId = getCacheObjectIdName(this.locationId, this.tag);
|
||||||
this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), {
|
this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), {
|
||||||
@@ -85,7 +85,7 @@ export class CacheObjectStub {
|
|||||||
});
|
});
|
||||||
const locationkeys = await cacheGroup.purge();
|
const locationkeys = await cacheGroup.purge();
|
||||||
locationkeys.forEach((key) => keys.add(key));
|
locationkeys.forEach((key) => keys.add(key));
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
return keys;
|
return keys;
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
dist/
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# @gitbook/colors
|
||||||
|
|
||||||
|
## 0.2.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- 445baaa: Initial release
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# `@gitbook/colors`
|
||||||
|
|
||||||
|
A set of default colors and transformation functions used throughout the GitBook Open and app.
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "@gitbook/colors",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"development": "./src/index.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"version": "0.2.0",
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.5.3"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"typecheck": "tsc --noEmit",
|
||||||
|
"dev": "tsc -w"
|
||||||
|
},
|
||||||
|
"files": ["dist", "src", "README.md", "CHANGELOG.md"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Default primary color throughout the GitBook ecosystem.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_PRIMARY_COLOR = '#346DDB';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The darkest color that exists in GitBook, used as the relative minimum of every generated color scale.
|
||||||
|
*/
|
||||||
|
export const DARK_BASE = '#1D1D1D';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lightest color that exists in GitBook, used as the relative maximum of every generated color scale.
|
||||||
|
*/
|
||||||
|
export const LIGHT_BASE = '#FFFFFF';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used as the basis of all UI elements that are not colored by the primary color. Neutral gray by default, overridden by site customization.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_TINT_COLOR = '#787878';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for informational messages and neutral alerts.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_HINT_INFO_COLOR = '#787878';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for showing important information or non-critical warnings.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_HINT_WARNING_COLOR = '#FE9A00';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for destructive actions or raising attention to critical information.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_HINT_DANGER_COLOR = '#FB2C36';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for showing positive actions or achievements.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_HINT_SUCCESS_COLOR = '#00C950';
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './colors';
|
||||||
|
export * from './transformations';
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { DARK_BASE, DEFAULT_TINT_COLOR, LIGHT_BASE } from './colors';
|
||||||
|
|
||||||
type ColorShades = {
|
type ColorShades = {
|
||||||
[key: string]: string;
|
[key: string]: string;
|
||||||
};
|
};
|
||||||
@@ -6,9 +8,6 @@ type RGBColor = [number, number, number];
|
|||||||
type OKLABColor = { L: number; A: number; B: number };
|
type OKLABColor = { L: number; A: number; B: number };
|
||||||
type OKLCHColor = { L: number; C: number; H: number };
|
type OKLCHColor = { L: number; C: number; H: number };
|
||||||
|
|
||||||
export const DARK_BASE = '#1d1d1d';
|
|
||||||
export const LIGHT_BASE = '#ffffff';
|
|
||||||
export const DEFAULT_TINT_COLOR = '#787878';
|
|
||||||
const D65 = [95.047, 100.0, 108.883]; // Reference white (D65)
|
const D65 = [95.047, 100.0, 108.883]; // Reference white (D65)
|
||||||
|
|
||||||
export enum ColorCategory {
|
export enum ColorCategory {
|
||||||
@@ -179,7 +178,7 @@ export function colorScale(
|
|||||||
background = darkMode ? DARK_BASE : LIGHT_BASE,
|
background = darkMode ? DARK_BASE : LIGHT_BASE,
|
||||||
foreground = darkMode ? LIGHT_BASE : DARK_BASE,
|
foreground = darkMode ? LIGHT_BASE : DARK_BASE,
|
||||||
mix,
|
mix,
|
||||||
}: ColorScaleOptions = {},
|
}: ColorScaleOptions = {}
|
||||||
) {
|
) {
|
||||||
const baseColor = rgbToOklch(hexToRgbArray(hex));
|
const baseColor = rgbToOklch(hexToRgbArray(hex));
|
||||||
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
|
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
|
||||||
@@ -226,7 +225,7 @@ export function colorScale(
|
|||||||
/**
|
/**
|
||||||
* Convert a hex color to an RGB color set.
|
* Convert a hex color to an RGB color set.
|
||||||
*/
|
*/
|
||||||
function hexToRgbArray(hex: string): RGBColor {
|
export function hexToRgbArray(hex: string): RGBColor {
|
||||||
const originalHex = hex;
|
const originalHex = hex;
|
||||||
|
|
||||||
let value = hex.replace('#', '');
|
let value = hex.replace('#', '');
|
||||||
@@ -252,7 +251,7 @@ function hexToRgbArray(hex: string): RGBColor {
|
|||||||
/**
|
/**
|
||||||
* Convert a RGB color set to a hex color.
|
* Convert a RGB color set to a hex color.
|
||||||
*/
|
*/
|
||||||
function rgbArrayToHex(rgb: RGBColor): string {
|
export function rgbArrayToHex(rgb: RGBColor): string {
|
||||||
return `#${rgb
|
return `#${rgb
|
||||||
.map((channel) => {
|
.map((channel) => {
|
||||||
const component = channel.toString(16);
|
const component = channel.toString(16);
|
||||||
@@ -262,7 +261,7 @@ function rgbArrayToHex(rgb: RGBColor): string {
|
|||||||
.join('')}`;
|
.join('')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getColor(percentage: number, start: RGBColor, end: RGBColor) {
|
export function getColor(percentage: number, start: RGBColor, end: RGBColor) {
|
||||||
const rgb = end.map((channel, index) => {
|
const rgb = end.map((channel, index) => {
|
||||||
return Math.round(channel + percentage * (start[index] - channel));
|
return Math.round(channel + percentage * (start[index] - channel));
|
||||||
});
|
});
|
||||||
@@ -271,21 +270,21 @@ function getColor(percentage: number, start: RGBColor, end: RGBColor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Utility constants and helper functions
|
// Utility constants and helper functions
|
||||||
function rgbToLinear(rgb: RGBColor): [number, number, number] {
|
export function rgbToLinear(rgb: RGBColor): [number, number, number] {
|
||||||
return rgb.map((v) => {
|
return rgb.map((v) => {
|
||||||
const scaled = v / 255;
|
const scaled = v / 255;
|
||||||
return scaled <= 0.04045 ? scaled / 12.92 : ((scaled + 0.055) / 1.055) ** 2.4;
|
return scaled <= 0.04045 ? scaled / 12.92 : ((scaled + 0.055) / 1.055) ** 2.4;
|
||||||
}) as [number, number, number];
|
}) as [number, number, number];
|
||||||
}
|
}
|
||||||
|
|
||||||
function linearToRgb(linear: [number, number, number]): RGBColor {
|
export function linearToRgb(linear: [number, number, number]): RGBColor {
|
||||||
return linear.map((v) => {
|
return linear.map((v) => {
|
||||||
const scaled = v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055;
|
const scaled = v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055;
|
||||||
return Math.round(Math.max(0, Math.min(1, scaled)) * 255);
|
return Math.round(Math.max(0, Math.min(1, scaled)) * 255);
|
||||||
}) as RGBColor;
|
}) as RGBColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
function rgbToOklab(rgb: RGBColor): OKLABColor {
|
export function rgbToOklab(rgb: RGBColor): OKLABColor {
|
||||||
const [r, g, b] = rgbToLinear(rgb);
|
const [r, g, b] = rgbToLinear(rgb);
|
||||||
|
|
||||||
const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
|
const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
|
||||||
@@ -303,7 +302,7 @@ function rgbToOklab(rgb: RGBColor): OKLABColor {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function oklabToRgb(oklab: OKLABColor): RGBColor {
|
export function oklabToRgb(oklab: OKLABColor): RGBColor {
|
||||||
const { L, A, B } = oklab;
|
const { L, A, B } = oklab;
|
||||||
|
|
||||||
const lRoot = L + 0.3963377774 * A + 0.2158037573 * B;
|
const lRoot = L + 0.3963377774 * A + 0.2158037573 * B;
|
||||||
@@ -321,14 +320,14 @@ function oklabToRgb(oklab: OKLABColor): RGBColor {
|
|||||||
return linearToRgb([r, g, b]);
|
return linearToRgb([r, g, b]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function oklabToOklch(oklab: OKLABColor): OKLCHColor {
|
export function oklabToOklch(oklab: OKLABColor): OKLCHColor {
|
||||||
const { L, A, B } = oklab;
|
const { L, A, B } = oklab;
|
||||||
const C = Math.sqrt(A ** 2 + B ** 2);
|
const C = Math.sqrt(A ** 2 + B ** 2);
|
||||||
const H = (Math.atan2(B, A) * 180) / Math.PI;
|
const H = (Math.atan2(B, A) * 180) / Math.PI;
|
||||||
return { L, C, H: H < 0 ? H + 360 : H };
|
return { L, C, H: H < 0 ? H + 360 : H };
|
||||||
}
|
}
|
||||||
|
|
||||||
function oklchToOklab(oklch: OKLCHColor): OKLABColor {
|
export function oklchToOklab(oklch: OKLCHColor): OKLABColor {
|
||||||
const { L, C, H } = oklch;
|
const { L, C, H } = oklch;
|
||||||
const rad = (H * Math.PI) / 180;
|
const rad = (H * Math.PI) / 180;
|
||||||
return {
|
return {
|
||||||
@@ -338,15 +337,15 @@ function oklchToOklab(oklch: OKLCHColor): OKLABColor {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function rgbToOklch(rgb: RGBColor): OKLCHColor {
|
export function rgbToOklch(rgb: RGBColor): OKLCHColor {
|
||||||
return oklabToOklch(rgbToOklab(rgb));
|
return oklabToOklch(rgbToOklab(rgb));
|
||||||
}
|
}
|
||||||
|
|
||||||
function oklchToRgb(oklch: OKLCHColor): RGBColor {
|
export function oklchToRgb(oklch: OKLCHColor): RGBColor {
|
||||||
return oklabToRgb(oklchToOklab(oklch));
|
return oklabToRgb(oklchToOklab(oklch));
|
||||||
}
|
}
|
||||||
|
|
||||||
function rgbToXyz(rgb: RGBColor): [number, number, number] {
|
export function rgbToXyz(rgb: RGBColor): [number, number, number] {
|
||||||
const [r, g, b] = rgbToLinear(rgb);
|
const [r, g, b] = rgbToLinear(rgb);
|
||||||
return [
|
return [
|
||||||
(r * 0.4124564 + g * 0.3575761 + b * 0.1804375) * 100,
|
(r * 0.4124564 + g * 0.3575761 + b * 0.1804375) * 100,
|
||||||
@@ -355,7 +354,11 @@ function rgbToXyz(rgb: RGBColor): [number, number, number] {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
function xyzToLab65(xyz: [number, number, number]): { L: number; A: number; B: number } {
|
export function xyzToLab65(xyz: [number, number, number]): {
|
||||||
|
L: number;
|
||||||
|
A: number;
|
||||||
|
B: number;
|
||||||
|
} {
|
||||||
const [x, y, z] = xyz.map((v, i) => {
|
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 scaled > 0.008856 ? Math.cbrt(scaled) : 7.787 * scaled + 16 / 116;
|
||||||
@@ -368,7 +371,7 @@ function xyzToLab65(xyz: [number, number, number]): { L: number; A: number; B: n
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function rgbTolab65(rgb: RGBColor): { L: number; A: number; B: number } {
|
export function rgbTolab65(rgb: RGBColor): { L: number; A: number; B: number } {
|
||||||
return xyzToLab65(rgbToXyz(rgb));
|
return xyzToLab65(rgbToXyz(rgb));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,7 +379,7 @@ function rgbTolab65(rgb: RGBColor): { L: number; A: number; B: number } {
|
|||||||
Delta Phi Star perceptual lightness contrast by Andrew Somers:
|
Delta Phi Star perceptual lightness contrast by Andrew Somers:
|
||||||
https://github.com/Myndex/deltaphistar
|
https://github.com/Myndex/deltaphistar
|
||||||
*/
|
*/
|
||||||
const PHI = 0.5 + Math.sqrt(1.25);
|
export const PHI = 0.5 + Math.sqrt(1.25);
|
||||||
|
|
||||||
export function dpsContrast(a: RGBColor, b: RGBColor) {
|
export function dpsContrast(a: RGBColor, b: RGBColor) {
|
||||||
const dps = Math.abs(rgbTolab65(a).L ** PHI - rgbTolab65(b).L ** PHI);
|
const dps = Math.abs(rgbTolab65(a).L ** PHI - rgbTolab65(b).L ** PHI);
|
||||||
@@ -387,7 +390,10 @@ export function dpsContrast(a: RGBColor, b: RGBColor) {
|
|||||||
export function colorContrast(background: string, foreground: string[] = [LIGHT_BASE, DARK_BASE]) {
|
export function colorContrast(background: string, foreground: string[] = [LIGHT_BASE, DARK_BASE]) {
|
||||||
const bg = hexToRgbArray(background);
|
const bg = hexToRgbArray(background);
|
||||||
|
|
||||||
const best: { color?: RGBColor; contrast: number } = { color: undefined, contrast: 0 };
|
const best: { color?: RGBColor; contrast: number } = {
|
||||||
|
color: undefined,
|
||||||
|
contrast: 0,
|
||||||
|
};
|
||||||
for (const color of foreground) {
|
for (const color of foreground) {
|
||||||
const c = hexToRgbArray(color);
|
const c = hexToRgbArray(color);
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"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": [
|
||||||
|
"bun-types" // add Bun global
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||||
|
"exclude": ["node_modules"]
|
||||||
|
}
|
||||||
@@ -17,12 +17,11 @@ Object.entries(emojis).forEach(([key, value]) => {
|
|||||||
if (emoji && key !== emoji) {
|
if (emoji && key !== emoji) {
|
||||||
output[key] = emoji;
|
output[key] = emoji;
|
||||||
} else if (!emoji) {
|
} else if (!emoji) {
|
||||||
console.log('No emoji for', key);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.mkdirSync(path.resolve(__dirname, 'dist'), { recursive: true });
|
fs.mkdirSync(path.resolve(__dirname, 'dist'), { recursive: true });
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.resolve(__dirname, 'dist/index.ts'),
|
path.resolve(__dirname, 'dist/index.ts'),
|
||||||
`export const emojiCodepoints: Record<string, string> = ${JSON.stringify(output, null, 4)};`,
|
`export const emojiCodepoints: Record<string, string> = ${JSON.stringify(output, null, 4)};`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,3 +6,6 @@
|
|||||||
|
|
||||||
# cloudflare
|
# cloudflare
|
||||||
.open-next
|
.open-next
|
||||||
|
|
||||||
|
# Symbolic links
|
||||||
|
public
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
# gitbook-v2
|
# gitbook-v2
|
||||||
|
|
||||||
|
## 0.1.1
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 3e11678: fix: lost section groups
|
||||||
|
|
||||||
## 0.1.0
|
## 0.1.0
|
||||||
|
|
||||||
### Minor Changes
|
### Minor Changes
|
||||||
|
|||||||
@@ -1,104 +0,0 @@
|
|||||||
# GitBook V2
|
|
||||||
|
|
||||||
This package contains the code for an experiment new version of the GitBook renderer.
|
|
||||||
|
|
||||||
The goals of this new version are:
|
|
||||||
|
|
||||||
- Optimize rendering to leverage Next.js ISR and static rendering
|
|
||||||
- Leverage Node.js runtime and bigger instances to render bigger content
|
|
||||||
- Evaluate Vercel and Cloudflare deployment options
|
|
||||||
|
|
||||||
## Deployment
|
|
||||||
|
|
||||||
#### Vercel
|
|
||||||
|
|
||||||
The new version is deployed automatically to Vercel using a Github Action on every push.
|
|
||||||
|
|
||||||
#### Cloudflare with Opennext-js
|
|
||||||
|
|
||||||
The new version is deployed automatically to Cloudflare using a Github Action on every push.
|
|
||||||
|
|
||||||
## Potential issues
|
|
||||||
|
|
||||||
- [ ] We can't use `dynamicIO` because our root layout needs to fetch the params (https://github.com/vercel/next.js/discussions/71927)
|
|
||||||
- [ ] How to do `nonce` for static resources when we can't use `headers()` (dynamic API) (https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy)
|
|
||||||
|
|
||||||
## Structure
|
|
||||||
|
|
||||||
#### Middleware:
|
|
||||||
|
|
||||||
Goals:
|
|
||||||
- Route between dynamic and static routes
|
|
||||||
- If a token is in the cookies => dynamic route
|
|
||||||
- If a token is in the url => dynamic route
|
|
||||||
- Else => static route
|
|
||||||
- High performances:
|
|
||||||
- It should not rely on data fetching
|
|
||||||
|
|
||||||
#### URLs to support:
|
|
||||||
|
|
||||||
Content URLs:
|
|
||||||
|
|
||||||
- `docs.company.com/page`
|
|
||||||
- Middleware: rewrite to `/static/url/docs.company.com/page`
|
|
||||||
- Page component renders the page
|
|
||||||
- `docs.company2.com/section/page`
|
|
||||||
- Middleware: rewrite to `/static/url/docs.company2.com/section/page`
|
|
||||||
- Page component renders the page
|
|
||||||
- `docs.company3.com/section/sitespace/page`
|
|
||||||
- Middleware: rewrite to `/static/url/docs.company3.com/section/sitespace/page`
|
|
||||||
- Page component renders the page
|
|
||||||
|
|
||||||
Content URLs (adaptive content or visitor authentication):
|
|
||||||
|
|
||||||
- `docs.company.com/page`
|
|
||||||
- Middleware:
|
|
||||||
- If a token is in the cookies or url => dynamic route
|
|
||||||
- Else => rewrite to `/dynamic/url/docs.company.com/page`
|
|
||||||
- Page component renders the page
|
|
||||||
- Read the token from the url query params or cookies
|
|
||||||
|
|
||||||
|
|
||||||
PDF export URLs:
|
|
||||||
|
|
||||||
- `docs.company.com/page/~gitbook/pdf`
|
|
||||||
- Middleware: rewrite to `/dynamic/pdf/docs.company.com/page`
|
|
||||||
- Page component renders the page
|
|
||||||
|
|
||||||
Images resizing:
|
|
||||||
|
|
||||||
- `docs.company.com/~gitbook/image`
|
|
||||||
- Middleware: rewrite to `/dynamic/image/docs.company.com`
|
|
||||||
- Route handlers process the request
|
|
||||||
|
|
||||||
Preview URLs:
|
|
||||||
|
|
||||||
- `open.gitbook.com/sites/site_abc/page` (or `open.gitbook.com/~site/site_abc/page` to stay compatible?)
|
|
||||||
- Middleware: no rewrite
|
|
||||||
- Page component renders the page (dynamic?)
|
|
||||||
- Read the token from the url query params
|
|
||||||
- Read customizations override from the url query params
|
|
||||||
|
|
||||||
|
|
||||||
Space PDF URLs:
|
|
||||||
|
|
||||||
- `open.gitbook.com/spaces/space_abc/pdf`
|
|
||||||
- Middleware: no rewrite
|
|
||||||
- Page component renders the page (dynamic?)
|
|
||||||
- Read the token from the url query params
|
|
||||||
- Read customizations override from the url query params
|
|
||||||
|
|
||||||
Development:
|
|
||||||
|
|
||||||
- `localhost:3000/sites/site_abc/page`
|
|
||||||
|
|
||||||
#### Routes:
|
|
||||||
|
|
||||||
- Content
|
|
||||||
- [ ] `/static/url/[[...url]]/page.tsx`
|
|
||||||
- [ ] `/dynamic/url/[[...url]]/page.tsx`
|
|
||||||
- Images resizing
|
|
||||||
- [ ] `/dynamic/image/[hostname]/route.ts`
|
|
||||||
- PDF export
|
|
||||||
- [ ] `/dynamic/pdf/[[...url]]/page.tsx`
|
|
||||||
- [ ] `/spaces/[space]/pdf/page.tsx`
|
|
||||||
|
|||||||
@@ -6,9 +6,30 @@
|
|||||||
const nextConfig = {
|
const nextConfig = {
|
||||||
experimental: {
|
experimental: {
|
||||||
useCache: true,
|
useCache: true,
|
||||||
|
},
|
||||||
|
|
||||||
// We can't use dynamicIO because it doesn't accept reading params in the root layout
|
env: {
|
||||||
// dynamicIO: true,
|
BUILD_VERSION: (process.env.GITHUB_SHA ?? '').slice(0, 7),
|
||||||
|
GITBOOK_URL: process.env.GITBOOK_URL,
|
||||||
|
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX,
|
||||||
|
GITBOOK_ICONS_URL: process.env.GITBOOK_ICONS_URL,
|
||||||
|
GITBOOK_ICONS_TOKEN: process.env.GITBOOK_ICONS_TOKEN,
|
||||||
|
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY,
|
||||||
|
|
||||||
|
// Used to detect if the app is running in V2 mode
|
||||||
|
GITBOOK_V2: 'true',
|
||||||
|
},
|
||||||
|
|
||||||
|
assetPrefix: process.env.GITBOOK_ASSETS_PREFIX,
|
||||||
|
poweredByHeader: false,
|
||||||
|
|
||||||
|
images: {
|
||||||
|
remotePatterns: [
|
||||||
|
{
|
||||||
|
protocol: 'https',
|
||||||
|
hostname: '*.gitbook.io',
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,29 @@
|
|||||||
{
|
{
|
||||||
"name": "gitbook-v2",
|
"name": "gitbook-v2",
|
||||||
"version": "0.1.0",
|
"version": "0.1.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "canary",
|
"next": "canary",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
"@gitbook/api": "^0.93.0"
|
"@gitbook/api": "0.96.1",
|
||||||
|
"@sindresorhus/fnv1a": "^3.1.0",
|
||||||
|
"server-only": "^0.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@opennextjs/cloudflare": "^0.4.3"
|
"gitbook": "*",
|
||||||
|
"@opennextjs/cloudflare": "^0.5.3",
|
||||||
|
"tailwindcss": "^3.4.0",
|
||||||
|
"postcss": "^8"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev:v2": "next",
|
"generate": "rm -rf ./public && cp -r ../gitbook/public ./public",
|
||||||
|
"dev:v2": "env-cmd --silent -f ../../.env.local next --turbopack",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"build:v2": "next build",
|
"build:v2": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"build:v2:cloudflare": "opennextjs-cloudflare",
|
"build:v2:cloudflare": "opennextjs-cloudflare",
|
||||||
"dev:v2:cloudflare": "wrangler dev --port 8771"
|
"dev:v2:cloudflare": "wrangler dev --port 8771",
|
||||||
|
"unit": "bun test"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module.exports = {
|
||||||
|
plugins: {
|
||||||
|
tailwindcss: {},
|
||||||
|
autoprefixer: {},
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { SiteContentLayout } from '@/components/routes/SiteContentLayout';
|
|
||||||
import { createDynamicSiteContext } from '@/lib/context';
|
|
||||||
|
|
||||||
export default async function RootLayout({
|
|
||||||
params,
|
|
||||||
children,
|
|
||||||
}: {
|
|
||||||
params: Promise<{ url: string[] }>;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}) {
|
|
||||||
const { url } = await params;
|
|
||||||
const context = await createDynamicSiteContext(url);
|
|
||||||
|
|
||||||
return <SiteContentLayout context={context}>{children}</SiteContentLayout>;
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { SiteContentPage } from '@/components/routes/SiteContentPage';
|
|
||||||
import { createDynamicSiteContext } from '@/lib/context';
|
|
||||||
|
|
||||||
export default async function Page({ params }: { params: Promise<{ url: string[] }> }) {
|
|
||||||
const { url } = await params;
|
|
||||||
const context = await createDynamicSiteContext(url);
|
|
||||||
|
|
||||||
return <SiteContentPage context={context} />;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { SitePageSkeleton } from '@/components/SitePage';
|
||||||
|
|
||||||
|
export default function Loading() {
|
||||||
|
return <SitePageSkeleton />;
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { SitePageNotFound } from '@/components/SitePage';
|
||||||
|
|
||||||
|
export default async function NotFound() {
|
||||||
|
return <SitePageNotFound />;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import {
|
||||||
|
SitePage,
|
||||||
|
generateSitePageMetadata,
|
||||||
|
generateSitePageViewport,
|
||||||
|
} from '@/components/SitePage';
|
||||||
|
import { type RouteParams, getDynamicSiteContext, getPagePathFromParams } from '@v2/app/utils';
|
||||||
|
import type { Metadata, Viewport } from 'next';
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<RouteParams>;
|
||||||
|
searchParams: Promise<{ fallback?: string }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page(props: PageProps) {
|
||||||
|
const params = await props.params;
|
||||||
|
const context = await getDynamicSiteContext(params);
|
||||||
|
const pathname = getPagePathFromParams(params);
|
||||||
|
|
||||||
|
return <SitePage context={context} pageParams={{ pathname }} redirectOnFallback={true} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateViewport(props: PageProps): Promise<Viewport> {
|
||||||
|
const context = await getDynamicSiteContext(await props.params);
|
||||||
|
return generateSitePageViewport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateMetadata(props: PageProps): Promise<Metadata> {
|
||||||
|
const [params, searchParams] = await Promise.all([props.params, props.searchParams]);
|
||||||
|
const context = await getDynamicSiteContext(params);
|
||||||
|
const pathname = getPagePathFromParams(params);
|
||||||
|
|
||||||
|
return generateSitePageMetadata({
|
||||||
|
context,
|
||||||
|
pageParams: { pathname },
|
||||||
|
redirectOnFallback: true,
|
||||||
|
fallback: !!searchParams.fallback,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { CustomizationRootLayout } from '@/components/RootLayout';
|
||||||
|
import {
|
||||||
|
SiteLayout,
|
||||||
|
generateSiteLayoutMetadata,
|
||||||
|
generateSiteLayoutViewport,
|
||||||
|
} from '@/components/SiteLayout';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
|
||||||
|
import { getThemeFromMiddleware } from '@v2/lib/middleware';
|
||||||
|
|
||||||
|
interface SiteDynamicLayoutProps {
|
||||||
|
params: Promise<RouteLayoutParams>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function SiteDynamicLayout({
|
||||||
|
params,
|
||||||
|
children,
|
||||||
|
}: React.PropsWithChildren<SiteDynamicLayoutProps>) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
const forcedTheme = await getThemeFromMiddleware();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CustomizationRootLayout customization={context.customization}>
|
||||||
|
<SiteLayout
|
||||||
|
context={context}
|
||||||
|
forcedTheme={forcedTheme}
|
||||||
|
withTracking={!GITBOOK_DISABLE_TRACKING}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</SiteLayout>
|
||||||
|
</CustomizationRootLayout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateViewport({ params }: SiteDynamicLayoutProps) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return generateSiteLayoutViewport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateMetadata({ params }: SiteDynamicLayoutProps) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return generateSiteLayoutMetadata(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveLLMsTxt } from '@/routes/llms';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return serveLLMsTxt(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveRobotsTxt } from '@/routes/robots';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return serveRobotsTxt(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { servePagesSitemap } from '@/routes/sitemap';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return servePagesSitemap(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveRootSitemap } from '@/routes/sitemap';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return serveRootSitemap(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveIcon } from '@/routes/icon';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return serveIcon(context, request);
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import type { PageIdParams } from '@/components/SitePage';
|
||||||
|
import { serveOGImage } from '@/routes/ogimage';
|
||||||
|
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams & PageIdParams> }
|
||||||
|
) {
|
||||||
|
const context = await getDynamicSiteContext(await params);
|
||||||
|
return serveOGImage(context, await params);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { SitePageNotFound } from '@/components/SitePage';
|
||||||
|
|
||||||
|
export default async function NotFound() {
|
||||||
|
return <SitePageNotFound />;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import {
|
||||||
|
SitePage,
|
||||||
|
generateSitePageMetadata,
|
||||||
|
generateSitePageViewport,
|
||||||
|
} from '@/components/SitePage';
|
||||||
|
import { type RouteParams, getPagePathFromParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
import { getSiteCacheTag } from '@v2/lib/cache';
|
||||||
|
import type { Metadata, Viewport } from 'next';
|
||||||
|
import { unstable_cacheTag as cacheTag } from 'next/cache';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<RouteParams>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page(props: PageProps) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
const params = await props.params;
|
||||||
|
const context = await getStaticSiteContext(params);
|
||||||
|
const pathname = getPagePathFromParams(params);
|
||||||
|
|
||||||
|
cacheTag(getSiteCacheTag(context.site.id));
|
||||||
|
|
||||||
|
return <SitePage context={context} pageParams={{ pathname }} redirectOnFallback={true} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateViewport(props: PageProps): Promise<Viewport> {
|
||||||
|
const context = await getStaticSiteContext(await props.params);
|
||||||
|
return generateSitePageViewport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateMetadata(props: PageProps): Promise<Metadata> {
|
||||||
|
const params = await props.params;
|
||||||
|
const context = await getStaticSiteContext(params);
|
||||||
|
const pathname = getPagePathFromParams(params);
|
||||||
|
|
||||||
|
return generateSitePageMetadata({
|
||||||
|
context,
|
||||||
|
pageParams: { pathname },
|
||||||
|
redirectOnFallback: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { CustomizationRootLayout } from '@/components/RootLayout';
|
||||||
|
import {
|
||||||
|
SiteLayout,
|
||||||
|
generateSiteLayoutMetadata,
|
||||||
|
generateSiteLayoutViewport,
|
||||||
|
} from '@/components/SiteLayout';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
import { getSiteCacheTag } from '@v2/lib/cache';
|
||||||
|
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
|
||||||
|
import { unstable_cacheTag as cacheTag } from 'next/cache';
|
||||||
|
|
||||||
|
interface SiteStaticLayoutProps {
|
||||||
|
params: Promise<RouteLayoutParams>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function SiteStaticLayout({
|
||||||
|
params,
|
||||||
|
children,
|
||||||
|
}: React.PropsWithChildren<SiteStaticLayoutProps>) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
|
||||||
|
cacheTag(getSiteCacheTag(context.site.id));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CustomizationRootLayout customization={context.customization}>
|
||||||
|
<SiteLayout context={context} withTracking={!GITBOOK_DISABLE_TRACKING}>
|
||||||
|
{children}
|
||||||
|
</SiteLayout>
|
||||||
|
</CustomizationRootLayout>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateViewport({ params }: SiteStaticLayoutProps) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return generateSiteLayoutViewport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateMetadata({ params }: SiteStaticLayoutProps) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return generateSiteLayoutMetadata(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveLLMsTxt } from '@/routes/llms';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return serveLLMsTxt(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveRobotsTxt } from '@/routes/robots';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return serveRobotsTxt(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { servePagesSitemap } from '@/routes/sitemap';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return servePagesSitemap(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveRootSitemap } from '@/routes/sitemap';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return serveRootSitemap(context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import { serveIcon } from '@/routes/icon';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return serveIcon(context, request);
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
|
import type { PageIdParams } from '@/components/SitePage';
|
||||||
|
import { serveOGImage } from '@/routes/ogimage';
|
||||||
|
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
|
||||||
|
|
||||||
|
export const dynamic = 'force-static';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
_request: NextRequest,
|
||||||
|
{ params }: { params: Promise<RouteLayoutParams & PageIdParams> }
|
||||||
|
) {
|
||||||
|
const context = await getStaticSiteContext(await params);
|
||||||
|
return serveOGImage(context, await params);
|
||||||
|
}
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import { unstable_cacheTag as cacheTag } from 'next/cache';
|
|
||||||
import { SiteContentLayout } from '@/components/routes/SiteContentLayout';
|
|
||||||
import { getSiteCacheTag } from '@/lib/cache';
|
|
||||||
import { createStaticSiteContext } from '@/lib/context';
|
|
||||||
|
|
||||||
export default async function RootLayout({
|
|
||||||
params,
|
|
||||||
children,
|
|
||||||
}: {
|
|
||||||
params: Promise<{ url: string[] }>;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
const { url } = await params;
|
|
||||||
const context = await createStaticSiteContext(url);
|
|
||||||
|
|
||||||
cacheTag(getSiteCacheTag(context.siteId));
|
|
||||||
|
|
||||||
return <SiteContentLayout context={context}>{children}</SiteContentLayout>;
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { unstable_cacheTag as cacheTag } from 'next/cache';
|
|
||||||
import { createStaticSiteContext } from '@/lib/context';
|
|
||||||
import { getSiteCacheTag } from '@/lib/cache';
|
|
||||||
import { SiteContentPage } from '@/components/routes/SiteContentPage';
|
|
||||||
|
|
||||||
export default async function Page({ params }: { params: Promise<{ url: string[] }> }) {
|
|
||||||
'use cache';
|
|
||||||
|
|
||||||
const { url } = await params;
|
|
||||||
const context = await createStaticSiteContext(url);
|
|
||||||
|
|
||||||
cacheTag(getSiteCacheTag(context.siteId));
|
|
||||||
|
|
||||||
return <SiteContentPage context={context} />;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
import { fetchSiteContextByURL } from '@v2/lib/context';
|
||||||
|
import { createDataFetcher } from '@v2/lib/data';
|
||||||
|
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_URL } from '@v2/lib/env';
|
||||||
|
import { createImageResizer } from '@v2/lib/images';
|
||||||
|
import { createLinker } from '@v2/lib/links';
|
||||||
|
import { headers } from 'next/headers';
|
||||||
|
|
||||||
|
export type RouteParamMode = 'url-host' | 'url';
|
||||||
|
|
||||||
|
export type RouteLayoutParams = {
|
||||||
|
mode: string;
|
||||||
|
|
||||||
|
/** URL encoded site URL */
|
||||||
|
siteURL: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RouteParams = RouteLayoutParams & {
|
||||||
|
pagePath: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the static context when rendering statically a site.
|
||||||
|
*/
|
||||||
|
export async function getStaticSiteContext(params: RouteLayoutParams) {
|
||||||
|
const url = getSiteURLFromParams(params);
|
||||||
|
|
||||||
|
const dataFetcher = createDataFetcher();
|
||||||
|
const { linker, host } = createLinkerFromParams(params);
|
||||||
|
const context = await fetchSiteContextByURL(
|
||||||
|
{
|
||||||
|
dataFetcher,
|
||||||
|
linker,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: url.toString(),
|
||||||
|
visitorAuthToken: null,
|
||||||
|
redirectOnError: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
context.imageResizer = createImageResizer({
|
||||||
|
host,
|
||||||
|
linker: context.linker,
|
||||||
|
});
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the site context when rendering dynamically.
|
||||||
|
* The context will depend on the request.
|
||||||
|
*/
|
||||||
|
export async function getDynamicSiteContext(params: RouteLayoutParams) {
|
||||||
|
const url = getSiteURLFromParams(params);
|
||||||
|
const headersSet = await headers();
|
||||||
|
|
||||||
|
const dataFetcher = createDataFetcher({
|
||||||
|
apiToken: headersSet.get('x-gitbook-token') ?? GITBOOK_API_TOKEN,
|
||||||
|
apiEndpoint: headersSet.get('x-gitbook-api') ?? GITBOOK_API_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { linker, host } = createLinkerFromParams(params);
|
||||||
|
|
||||||
|
const context = await fetchSiteContextByURL(
|
||||||
|
{
|
||||||
|
dataFetcher,
|
||||||
|
linker,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: url.toString(),
|
||||||
|
visitorAuthToken: headersSet.get('x-gitbook-visitor-token'),
|
||||||
|
|
||||||
|
// TODO: set it only when the token comes from the cookies.
|
||||||
|
redirectOnError: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
context.imageResizer = createImageResizer({
|
||||||
|
host,
|
||||||
|
linker: context.linker,
|
||||||
|
});
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the decoded page path from the params.
|
||||||
|
*/
|
||||||
|
export function getPagePathFromParams(params: RouteParams) {
|
||||||
|
const decoded = decodeURIComponent(params.pagePath);
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createLinkerFromParams(params: RouteLayoutParams) {
|
||||||
|
const url = getSiteURLFromParams(params);
|
||||||
|
const mode = getModeFromParams(params.mode);
|
||||||
|
|
||||||
|
if (mode === 'url-host') {
|
||||||
|
return {
|
||||||
|
linker: createLinker({
|
||||||
|
host: url.host,
|
||||||
|
pathname: '/',
|
||||||
|
}),
|
||||||
|
host: url.host,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const gitbookURL = new URL(GITBOOK_URL);
|
||||||
|
const linker = createLinker({
|
||||||
|
protocol: gitbookURL.protocol,
|
||||||
|
host: gitbookURL.host,
|
||||||
|
pathname: `/url/${url.host}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create link in the same format for links to other sites/sections.
|
||||||
|
linker.toLinkForContent = (rawURL: string) => {
|
||||||
|
const urlObject = new URL(rawURL);
|
||||||
|
return `/url/${urlObject.host}${urlObject.pathname}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
linker,
|
||||||
|
host: gitbookURL.host,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSiteURLFromParams(params: RouteLayoutParams) {
|
||||||
|
const decoded = decodeURIComponent(params.siteURL);
|
||||||
|
const url = new URL(`https://${decoded}`);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getModeFromParams(mode: string): RouteParamMode {
|
||||||
|
if (mode === 'url-host') {
|
||||||
|
return 'url-host';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'url';
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import { GitBookSiteContext } from '@/lib/context';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Layout component to render the site content.
|
|
||||||
*/
|
|
||||||
export async function SiteContentLayout({
|
|
||||||
context,
|
|
||||||
children,
|
|
||||||
}: {
|
|
||||||
context: GitBookSiteContext;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}) {
|
|
||||||
const { api } = context;
|
|
||||||
const { data: publishedSite } = await api.orgs.getPublishedContentSite(
|
|
||||||
context.organizationId,
|
|
||||||
context.siteId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<html lang="en">
|
|
||||||
<body>
|
|
||||||
<h1>{publishedSite.site.title}</h1>
|
|
||||||
{children}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { GitBookSiteContext } from '@/lib/context';
|
|
||||||
|
|
||||||
export async function SiteContentPage({ context }: { context: GitBookSiteContext }) {
|
|
||||||
const { api } = context;
|
|
||||||
const { data } = await api.orgs.getPublishedContentSite(context.organizationId, context.siteId);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<p>Found site {data.site.id}</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Get the cache tag for a site.
|
||||||
|
*/
|
||||||
export function getSiteCacheTag(siteId: string) {
|
export function getSiteCacheTag(siteId: string) {
|
||||||
return `sites/${siteId}`;
|
return `site:${siteId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache tag for a hostname.
|
||||||
|
*/
|
||||||
|
export function getHostnameCacheTag(hostname: string) {
|
||||||
|
return `url:${hostname}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache tag for an OpenAPI spec.
|
||||||
|
*/
|
||||||
|
export function getOpenAPISpecCacheTag(organizationId: string, slug: string) {
|
||||||
|
return `organization:${organizationId}:openapi:${slug}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache tag for a space.
|
||||||
|
*/
|
||||||
|
export function getSpaceCacheTag(spaceId: string) {
|
||||||
|
return `space:${spaceId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache tag for a change request.
|
||||||
|
*/
|
||||||
|
export function getChangeRequestCacheTag(spaceId: string, changeRequestId: string) {
|
||||||
|
return `space:${spaceId}:change-request:${changeRequestId}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,112 +1,323 @@
|
|||||||
import { headers } from 'next/headers';
|
import { getSiteStructureSections } from '@/lib/sites';
|
||||||
|
import type {
|
||||||
|
ChangeRequest,
|
||||||
|
RevisionPage,
|
||||||
|
RevisionPageDocument,
|
||||||
|
Site,
|
||||||
|
SiteCustomizationSettings,
|
||||||
|
SiteIntegrationScript,
|
||||||
|
SiteSection,
|
||||||
|
SiteSectionGroup,
|
||||||
|
SiteSpace,
|
||||||
|
SiteStructure,
|
||||||
|
Space,
|
||||||
|
} from '@gitbook/api';
|
||||||
|
import { type GitBookDataFetcher, createDataFetcher } from '@v2/lib/data';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import { GitBookAPI } from '@gitbook/api';
|
import { assert } from 'ts-essentials';
|
||||||
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@/lib/env';
|
import type { ImageResizer } from './images';
|
||||||
|
import { type GitBookSpaceLinker, appendBasePathToLinker } from './links';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic context about rendering content.
|
* Generic context when rendering content.
|
||||||
*/
|
*/
|
||||||
export interface GitBookContext {
|
export type GitBookBaseContext = {
|
||||||
/**
|
/**
|
||||||
* API client to fetch data from GitBook.
|
* Data fetcher to fetch data from GitBook.
|
||||||
*/
|
*/
|
||||||
api: GitBookAPI;
|
dataFetcher: GitBookDataFetcher;
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Linker to generate links in the current space.
|
||||||
|
*/
|
||||||
|
linker: GitBookSpaceLinker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Image resizer to resize images.
|
||||||
|
*/
|
||||||
|
imageResizer?: ImageResizer;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Any context when rendering content.
|
||||||
|
*/
|
||||||
|
export type GitBookAnyContext = GitBookSpaceContext | GitBookSiteContext | GitBookPageContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context when rendering a space content.
|
||||||
|
*/
|
||||||
|
export type GitBookSpaceContext = GitBookBaseContext & {
|
||||||
|
organizationId: string;
|
||||||
|
|
||||||
|
space: Space;
|
||||||
|
changeRequest: ChangeRequest | null;
|
||||||
|
|
||||||
|
/** ID of the current revision. */
|
||||||
|
revisionId: string;
|
||||||
|
|
||||||
|
/** Pages of the space. */
|
||||||
|
pages: RevisionPage[];
|
||||||
|
|
||||||
|
/** Share key of the space. */
|
||||||
|
shareKey: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SiteSections = {
|
||||||
|
list: (SiteSectionGroup | SiteSection)[];
|
||||||
|
current: SiteSection;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context when rendering a site.
|
* Context when rendering a site.
|
||||||
*/
|
*/
|
||||||
export interface GitBookSiteContext extends GitBookContext {
|
export type GitBookSiteContext = GitBookSpaceContext & {
|
||||||
/**
|
site: Site;
|
||||||
* ID of the organization.
|
|
||||||
*/
|
|
||||||
organizationId: string;
|
|
||||||
|
|
||||||
/**
|
/** Current site space. */
|
||||||
* Site ID.
|
siteSpace: SiteSpace;
|
||||||
*/
|
|
||||||
siteId: string;
|
/** All site spaces in the current section / or entire site */
|
||||||
}
|
siteSpaces: SiteSpace[];
|
||||||
|
|
||||||
|
/** Sections of the site. */
|
||||||
|
sections: null | SiteSections;
|
||||||
|
|
||||||
|
/** Customizations of the site. */
|
||||||
|
customization: SiteCustomizationSettings;
|
||||||
|
|
||||||
|
/** Structure of the site. */
|
||||||
|
structure: SiteStructure;
|
||||||
|
|
||||||
|
/** Scripts to load for the site. */
|
||||||
|
scripts: SiteIntegrationScript[];
|
||||||
|
|
||||||
|
/** Visitor token used to fetch the site */
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a site context, when rendering a static page.
|
* Context when rendering a page.
|
||||||
*/
|
*/
|
||||||
export async function createStaticSiteContext(url: string[]): Promise<GitBookSiteContext> {
|
export type GitBookPageContext = (GitBookSpaceContext | GitBookSiteContext) & {
|
||||||
const context = createStaticContext();
|
page: RevisionPageDocument;
|
||||||
return fetchSiteContext(url, context);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a site context, when rendering a dynamic page.
|
|
||||||
*/
|
|
||||||
export async function createDynamicSiteContext(url: string[]): Promise<GitBookSiteContext> {
|
|
||||||
const context = await createDynamicContext();
|
|
||||||
return fetchSiteContext(url, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the context of a site for a given URL and a base context.
|
* Fetch the context of a site for a given URL and a base context.
|
||||||
*/
|
*/
|
||||||
async function fetchSiteContext(
|
export async function fetchSiteContextByURL(
|
||||||
urlParts: string[],
|
baseContext: GitBookBaseContext,
|
||||||
baseContext: GitBookContext,
|
input: {
|
||||||
|
url: string;
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
redirectOnError: boolean;
|
||||||
|
}
|
||||||
): Promise<GitBookSiteContext> {
|
): Promise<GitBookSiteContext> {
|
||||||
const { api } = baseContext;
|
const { dataFetcher } = baseContext;
|
||||||
const url = getURLFromParams(urlParts);
|
const data = await dataFetcher.getPublishedContentByUrl({
|
||||||
const { data } = await api.urls.getPublishedContentByUrl({
|
url: input.url,
|
||||||
url,
|
visitorAuthToken: input.visitorAuthToken,
|
||||||
|
redirectOnError: input.redirectOnError,
|
||||||
});
|
});
|
||||||
|
|
||||||
if ('redirect' in data) {
|
if ('redirect' in data) {
|
||||||
redirect(data.redirect);
|
redirect(data.redirect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const context = await fetchSiteContextByIds(
|
||||||
|
{
|
||||||
|
...baseContext,
|
||||||
|
dataFetcher: createDataFetcher({
|
||||||
|
apiEndpoint: dataFetcher.apiEndpoint,
|
||||||
|
apiToken: data.apiToken,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
organization: data.organization,
|
||||||
|
site: data.site,
|
||||||
|
siteSection: data.siteSection,
|
||||||
|
siteSpace: data.siteSpace,
|
||||||
|
space: data.space,
|
||||||
|
shareKey: data.shareKey,
|
||||||
|
changeRequest: data.changeRequest,
|
||||||
|
revision: data.revision,
|
||||||
|
visitorAuthToken: input.visitorAuthToken,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const siteContext = {
|
||||||
|
...context,
|
||||||
|
linker: appendBasePathToLinker(context.linker, data.basePath),
|
||||||
|
};
|
||||||
|
|
||||||
|
return siteContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a site context by IDs.
|
||||||
|
*/
|
||||||
|
export async function fetchSiteContextByIds(
|
||||||
|
baseContext: GitBookBaseContext,
|
||||||
|
ids: {
|
||||||
|
organization: string;
|
||||||
|
site: string;
|
||||||
|
siteSection: string | undefined;
|
||||||
|
siteSpace: string | undefined;
|
||||||
|
space: string;
|
||||||
|
shareKey: string | undefined;
|
||||||
|
changeRequest: string | undefined;
|
||||||
|
revision: string | undefined;
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
}
|
||||||
|
): Promise<GitBookSiteContext> {
|
||||||
|
const { dataFetcher } = baseContext;
|
||||||
|
|
||||||
|
const [{ site: orgSite, structure: siteStructure, customizations, scripts }, spaceContext] =
|
||||||
|
await Promise.all([
|
||||||
|
dataFetcher.getPublishedContentSite({
|
||||||
|
organizationId: ids.organization,
|
||||||
|
siteId: ids.site,
|
||||||
|
siteShareKey: ids.shareKey,
|
||||||
|
}),
|
||||||
|
fetchSpaceContextByIds(baseContext, ids),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// override the title with the customization title
|
||||||
|
// TODO: remove this hack once we have a proper way to handle site customizations
|
||||||
|
const site = {
|
||||||
|
...orgSite,
|
||||||
|
...(customizations.site?.title ? { title: customizations.site.title } : {}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const sections = ids.siteSection
|
||||||
|
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const siteSpace = (
|
||||||
|
siteStructure.type === 'siteSpaces' && siteStructure.structure
|
||||||
|
? siteStructure.structure
|
||||||
|
: sections?.current.siteSpaces
|
||||||
|
)?.find((siteSpace) => siteSpace.id === ids.siteSpace);
|
||||||
|
if (!siteSpace) {
|
||||||
|
throw new Error('Site space not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const siteSpaces =
|
||||||
|
siteStructure.type === 'siteSpaces'
|
||||||
|
? siteStructure.structure
|
||||||
|
: (sections?.current.siteSpaces ?? []);
|
||||||
|
|
||||||
|
const customization = (() => {
|
||||||
|
if (ids.siteSpace) {
|
||||||
|
const siteSpaceSettings = customizations.siteSpaces[ids.siteSpace];
|
||||||
|
if (siteSpaceSettings) {
|
||||||
|
return siteSpaceSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We got the pointer from an API and customizations from another.
|
||||||
|
// It's possible that the two are unsynced leading to not found customizations for the space.
|
||||||
|
// It's better to fallback on customization of the site that displaying an error.
|
||||||
|
console.warn('Customization not found for site space', ids.siteSpace);
|
||||||
|
}
|
||||||
|
|
||||||
|
return customizations.site;
|
||||||
|
})();
|
||||||
|
|
||||||
|
return {
|
||||||
|
...spaceContext,
|
||||||
|
organizationId: ids.organization,
|
||||||
|
site,
|
||||||
|
siteSpaces,
|
||||||
|
siteSpace,
|
||||||
|
customization,
|
||||||
|
structure: siteStructure,
|
||||||
|
sections,
|
||||||
|
scripts,
|
||||||
|
visitorAuthToken: ids.visitorAuthToken,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a space context by IDs.
|
||||||
|
*/
|
||||||
|
export async function fetchSpaceContextByIds(
|
||||||
|
baseContext: GitBookBaseContext,
|
||||||
|
ids: {
|
||||||
|
space: string;
|
||||||
|
shareKey: string | undefined;
|
||||||
|
changeRequest: string | undefined;
|
||||||
|
revision: string | undefined;
|
||||||
|
}
|
||||||
|
): Promise<GitBookSpaceContext> {
|
||||||
|
const { dataFetcher } = baseContext;
|
||||||
|
|
||||||
|
const [space, changeRequest] = await Promise.all([
|
||||||
|
dataFetcher.getSpace({
|
||||||
|
spaceId: ids.space,
|
||||||
|
shareKey: ids.shareKey,
|
||||||
|
}),
|
||||||
|
ids.changeRequest
|
||||||
|
? dataFetcher.getChangeRequest({
|
||||||
|
spaceId: ids.space,
|
||||||
|
changeRequestId: ids.changeRequest,
|
||||||
|
})
|
||||||
|
: null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const revisionId = changeRequest?.revision ?? ids.revision ?? space.revision;
|
||||||
|
|
||||||
|
const pages = await dataFetcher.getRevisionPages({
|
||||||
|
spaceId: ids.space,
|
||||||
|
revisionId,
|
||||||
|
// We only care about the Git metadata when the Git sync is enabled,
|
||||||
|
// otherwise we can optimize performance by not fetching it
|
||||||
|
metadata: !!space.gitSync,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...baseContext,
|
...baseContext,
|
||||||
api: new GitBookAPI({
|
organizationId: space.organization,
|
||||||
endpoint: api.endpoint,
|
space,
|
||||||
authToken: data.apiToken,
|
pages,
|
||||||
userAgent: api.userAgent,
|
changeRequest,
|
||||||
}),
|
revisionId,
|
||||||
siteId: data.site,
|
shareKey: ids.shareKey,
|
||||||
organizationId: data.organization,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the base context when rendering statically.
|
* Check if the context is the root one for a site.
|
||||||
|
* Meaning we are on the default section / space.
|
||||||
*/
|
*/
|
||||||
function createStaticContext(): GitBookContext {
|
export function checkIsRootSiteContext(context: GitBookSiteContext): boolean {
|
||||||
const api = new GitBookAPI({
|
const { structure } = context;
|
||||||
authToken: GITBOOK_API_TOKEN,
|
switch (structure.type) {
|
||||||
endpoint: GITBOOK_API_URL,
|
case 'sections': {
|
||||||
userAgent: GITBOOK_USER_AGENT,
|
return getSiteStructureSections(structure, { ignoreGroups: true }).some(
|
||||||
});
|
(structure) =>
|
||||||
|
structure.default &&
|
||||||
return {
|
structure.id === context.sections?.current.id &&
|
||||||
api,
|
structure.siteSpaces.some(
|
||||||
};
|
(siteSpace) => siteSpace.default && siteSpace.id === context.siteSpace.id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
case 'siteSpaces': {
|
||||||
|
return structure.structure.some(
|
||||||
|
(siteSpace) => siteSpace.default && siteSpace.id === context.siteSpace.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
|
||||||
* Create the base context when rendering dynamically.
|
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
|
||||||
* The context will depend on the request.
|
const section = parseCurrentSection(structure, siteSectionId);
|
||||||
*/
|
assert(section, 'A section must be defined when there are multiple sections');
|
||||||
async function createDynamicContext(): Promise<GitBookContext> {
|
return { list: sectionsAndGroups, current: section } satisfies SiteSections;
|
||||||
const headersSet = await headers();
|
|
||||||
|
|
||||||
const api = new GitBookAPI({
|
|
||||||
authToken: headersSet.get('x-gitbook-token') ?? GITBOOK_API_TOKEN,
|
|
||||||
endpoint: headersSet.get('x-gitbook-api') ?? GITBOOK_API_URL,
|
|
||||||
userAgent: GITBOOK_USER_AGENT,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
api,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getURLFromParams(input: string[]) {
|
function parseCurrentSection(structure: SiteStructure, siteSectionId: string) {
|
||||||
const url = new URL('https://' + input.join('/'));
|
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
|
||||||
return url.toString();
|
return sections.find((section) => section.id === siteSectionId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,514 @@
|
|||||||
|
import { type ComputedContentSource, GitBookAPI } from '@gitbook/api';
|
||||||
|
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
|
||||||
|
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
|
||||||
|
import {
|
||||||
|
getChangeRequestCacheTag,
|
||||||
|
getHostnameCacheTag,
|
||||||
|
getOpenAPISpecCacheTag,
|
||||||
|
getSiteCacheTag,
|
||||||
|
getSpaceCacheTag,
|
||||||
|
} from '../cache';
|
||||||
|
import type { GitBookDataFetcher } from './types';
|
||||||
|
|
||||||
|
interface DataFetcherInput {
|
||||||
|
/**
|
||||||
|
* API host to use.
|
||||||
|
*/
|
||||||
|
apiEndpoint: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API token.
|
||||||
|
*/
|
||||||
|
apiToken: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const commonInput: DataFetcherInput = {
|
||||||
|
apiEndpoint: GITBOOK_API_URL,
|
||||||
|
apiToken: GITBOOK_API_TOKEN,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a data fetcher using an API token.
|
||||||
|
* The data are being cached by Next.js built-in cache.
|
||||||
|
*/
|
||||||
|
export function createDataFetcher(input: DataFetcherInput = commonInput): GitBookDataFetcher {
|
||||||
|
return {
|
||||||
|
apiEndpoint: input.apiEndpoint,
|
||||||
|
|
||||||
|
//
|
||||||
|
// API that are tied to the token
|
||||||
|
//
|
||||||
|
getPublishedContentSite(params) {
|
||||||
|
return getPublishedContentSite(input, {
|
||||||
|
organizationId: params.organizationId,
|
||||||
|
siteId: params.siteId,
|
||||||
|
siteShareKey: params.siteShareKey,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getSiteRedirectBySource(params) {
|
||||||
|
return getSiteRedirectBySource(input, {
|
||||||
|
organizationId: params.organizationId,
|
||||||
|
siteId: params.siteId,
|
||||||
|
siteShareKey: params.siteShareKey,
|
||||||
|
source: params.source,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getRevision(params) {
|
||||||
|
return getRevision(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
revisionId: params.revisionId,
|
||||||
|
metadata: params.metadata,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getRevisionPages(params) {
|
||||||
|
return getRevisionPages(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
revisionId: params.revisionId,
|
||||||
|
metadata: params.metadata,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getRevisionFile(params) {
|
||||||
|
return getRevisionFile(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
revisionId: params.revisionId,
|
||||||
|
fileId: params.fileId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getRevisionPageByPath(params) {
|
||||||
|
return getRevisionPageByPath(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
revisionId: params.revisionId,
|
||||||
|
path: params.path,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getReusableContent(params) {
|
||||||
|
return getReusableContent(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
revisionId: params.revisionId,
|
||||||
|
reusableContentId: params.reusableContentId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getLatestOpenAPISpecVersionContent(params) {
|
||||||
|
return getLatestOpenAPISpecVersionContent(input, {
|
||||||
|
organizationId: params.organizationId,
|
||||||
|
slug: params.slug,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getSpace(params) {
|
||||||
|
return getSpace(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
shareKey: params.shareKey,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getChangeRequest(params) {
|
||||||
|
return getChangeRequest(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
changeRequestId: params.changeRequestId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getDocument(params) {
|
||||||
|
return getDocument(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
documentId: params.documentId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getComputedDocument(params) {
|
||||||
|
return getComputedDocument(input, {
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
source: params.source,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getEmbedByUrl(params) {
|
||||||
|
return getEmbedByUrl(input, {
|
||||||
|
url: params.url,
|
||||||
|
spaceId: params.spaceId,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
//
|
||||||
|
// API that are not tied to the token
|
||||||
|
// where the data is the same for all users
|
||||||
|
//
|
||||||
|
getUserById(userId) {
|
||||||
|
return getUserById(commonInput, userId);
|
||||||
|
},
|
||||||
|
getPublishedContentByUrl(params) {
|
||||||
|
return getPublishedContentByUrl(commonInput, {
|
||||||
|
url: params.url,
|
||||||
|
visitorAuthToken: params.visitorAuthToken,
|
||||||
|
redirectOnError: params.redirectOnError,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getUserById(input: DataFetcherInput, userId: string) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).users.getUserById(userId);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getSpace(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
shareKey: string | undefined;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('days');
|
||||||
|
cacheTag(getSpaceCacheTag(params.spaceId));
|
||||||
|
|
||||||
|
const res = await getAPI(input).spaces.getSpaceById(params.spaceId, {
|
||||||
|
shareKey: params.shareKey,
|
||||||
|
});
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getChangeRequest(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
changeRequestId: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('minutes');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).spaces.getChangeRequestById(
|
||||||
|
params.spaceId,
|
||||||
|
params.changeRequestId
|
||||||
|
);
|
||||||
|
cacheTag(getChangeRequestCacheTag(params.spaceId, res.data.id));
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRevision(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
metadata: boolean;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
const res = await getAPI(input).spaces.getRevisionById(params.spaceId, params.revisionId, {
|
||||||
|
metadata: params.metadata,
|
||||||
|
});
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRevisionPages(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
metadata: boolean;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
const res = await getAPI(input).spaces.listPagesInRevisionById(
|
||||||
|
params.spaceId,
|
||||||
|
params.revisionId,
|
||||||
|
{
|
||||||
|
metadata: params.metadata,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return res.data.pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRevisionFile(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
fileId: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).spaces.getFileInRevisionById(
|
||||||
|
params.spaceId,
|
||||||
|
params.revisionId,
|
||||||
|
params.fileId
|
||||||
|
);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRevisionPageByPath(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
const encodedPath = encodeURIComponent(params.path);
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).spaces.getPageInRevisionByPath(
|
||||||
|
params.spaceId,
|
||||||
|
params.revisionId,
|
||||||
|
encodedPath
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getDocument(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
documentId: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
const res = await getAPI(input).spaces.getDocumentById(params.spaceId, params.documentId);
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getComputedDocument(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
source: ComputedContentSource;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
// TODO: we need to resolve dependencies and pass them in the cache key
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
const res = await getAPI(input).spaces.getComputedDocument(params.spaceId, {
|
||||||
|
source: params.source,
|
||||||
|
});
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getReusableContent(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
reusableContentId: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('max');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).spaces.getReusableContentInRevisionById(
|
||||||
|
params.spaceId,
|
||||||
|
params.revisionId,
|
||||||
|
params.reusableContentId
|
||||||
|
);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLatestOpenAPISpecVersionContent(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
organizationId: string;
|
||||||
|
slug: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheTag(getOpenAPISpecCacheTag(params.organizationId, params.slug));
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).orgs.getLatestOpenApiSpecVersionContent(
|
||||||
|
params.organizationId,
|
||||||
|
params.slug
|
||||||
|
);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPublishedContentByUrl(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
url: string;
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
redirectOnError: boolean;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
const { url, visitorAuthToken, redirectOnError } = params;
|
||||||
|
|
||||||
|
const hostname = new URL(url).hostname;
|
||||||
|
cacheTag(getHostnameCacheTag(hostname));
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
const res = await getAPI(input).urls.getPublishedContentByUrl({
|
||||||
|
url,
|
||||||
|
visitorAuthToken: visitorAuthToken ?? undefined,
|
||||||
|
redirectOnError,
|
||||||
|
});
|
||||||
|
|
||||||
|
if ('site' in res.data) {
|
||||||
|
cacheTag(getSiteCacheTag(res.data.site));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPublishedContentSite(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
organizationId: string;
|
||||||
|
siteId: string;
|
||||||
|
siteShareKey: string | undefined;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheTag(getSiteCacheTag(params.siteId));
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
const res = await getAPI(input).orgs.getPublishedContentSite(
|
||||||
|
params.organizationId,
|
||||||
|
params.siteId,
|
||||||
|
{
|
||||||
|
shareKey: params.siteShareKey,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getSiteRedirectBySource(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
organizationId: string;
|
||||||
|
siteId: string;
|
||||||
|
siteShareKey: string | undefined;
|
||||||
|
source: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheTag(getSiteCacheTag(params.siteId));
|
||||||
|
cacheLife('days');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await getAPI(input).orgs.getSiteRedirectBySource(
|
||||||
|
params.organizationId,
|
||||||
|
params.siteId,
|
||||||
|
{
|
||||||
|
shareKey: params.siteShareKey,
|
||||||
|
source: params.source,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
// 422 is returned when the source is invalid
|
||||||
|
// we don't want to throw but just return null
|
||||||
|
if (checkHasErrorCode(error, 422)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkHasErrorCode(error, 404)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getEmbedByUrl(
|
||||||
|
input: DataFetcherInput,
|
||||||
|
params: {
|
||||||
|
url: string;
|
||||||
|
spaceId: string;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
'use cache';
|
||||||
|
|
||||||
|
cacheLife('weeks');
|
||||||
|
|
||||||
|
const api = getAPI(input);
|
||||||
|
const res = await api.spaces.getEmbedByUrlInSpace(params.spaceId, { url: params.url });
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAPI(input: DataFetcherInput) {
|
||||||
|
const { apiEndpoint, apiToken } = input;
|
||||||
|
const api = new GitBookAPI({
|
||||||
|
authToken: apiToken ?? undefined,
|
||||||
|
endpoint: apiEndpoint,
|
||||||
|
userAgent: GITBOOK_USER_AGENT,
|
||||||
|
});
|
||||||
|
|
||||||
|
return api;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkHasErrorCode(error: unknown, code: number) {
|
||||||
|
return error instanceof Error && 'code' in error && error.code === code;
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export * from './api';
|
||||||
|
export * from './types';
|
||||||
|
export * from './utils';
|
||||||
|
export * from './urls';
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
import type * as api from '@gitbook/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic fetcher for GitBook data.
|
||||||
|
* It is used between v1 and v2.
|
||||||
|
*/
|
||||||
|
export interface GitBookDataFetcher {
|
||||||
|
/**
|
||||||
|
* Endpoint of the API.
|
||||||
|
*/
|
||||||
|
apiEndpoint: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a user by its ID.
|
||||||
|
*/
|
||||||
|
getUserById(userId: string): Promise<api.User | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a published content by its URL.
|
||||||
|
*/
|
||||||
|
getPublishedContentByUrl(params: {
|
||||||
|
url: string;
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
redirectOnError: boolean;
|
||||||
|
}): Promise<api.PublishedSiteContentLookup>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a published content site by its organization ID and site ID.
|
||||||
|
*/
|
||||||
|
getPublishedContentSite(params: {
|
||||||
|
organizationId: string;
|
||||||
|
siteId: string;
|
||||||
|
siteShareKey: string | undefined;
|
||||||
|
}): Promise<api.PublishedContentSite>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a space by its ID.
|
||||||
|
*/
|
||||||
|
getSpace(params: { spaceId: string; shareKey: string | undefined }): Promise<api.Space>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a change request by its space ID and change request ID.
|
||||||
|
*/
|
||||||
|
getChangeRequest(params: {
|
||||||
|
spaceId: string;
|
||||||
|
changeRequestId: string;
|
||||||
|
}): Promise<api.ChangeRequest | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the revision by its space ID and revision ID.
|
||||||
|
*/
|
||||||
|
getRevision(params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
metadata: boolean;
|
||||||
|
}): Promise<api.Revision>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the revision pages by its space ID and revision ID.
|
||||||
|
*/
|
||||||
|
getRevisionPages(params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
metadata: boolean;
|
||||||
|
}): Promise<api.RevisionPage[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a revision file by its space ID, revision ID and file ID.
|
||||||
|
*/
|
||||||
|
getRevisionFile(params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
fileId: string;
|
||||||
|
}): Promise<api.RevisionFile | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a revision page by its path.
|
||||||
|
*/
|
||||||
|
getRevisionPageByPath(params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
path: string;
|
||||||
|
}): Promise<api.RevisionPageDocument | api.RevisionPageGroup | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a document by its space ID and document ID.
|
||||||
|
*/
|
||||||
|
getDocument(params: { spaceId: string; documentId: string }): Promise<api.JSONDocument>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a computed document by its space ID and computed source.
|
||||||
|
*/
|
||||||
|
getComputedDocument(params: {
|
||||||
|
spaceId: string;
|
||||||
|
source: api.ComputedContentSource;
|
||||||
|
}): Promise<api.JSONDocument>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a reusable content by its space ID, revision ID and reusable content ID.
|
||||||
|
*/
|
||||||
|
getReusableContent(params: {
|
||||||
|
spaceId: string;
|
||||||
|
revisionId: string;
|
||||||
|
reusableContentId: string;
|
||||||
|
}): Promise<api.RevisionReusableContent | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the latest OpenAPI spec version content by its organization ID and slug.
|
||||||
|
*/
|
||||||
|
getLatestOpenAPISpecVersionContent(params: {
|
||||||
|
organizationId: string;
|
||||||
|
slug: string;
|
||||||
|
}): Promise<api.OpenAPISpecContent | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a site redirect by its source path.
|
||||||
|
*/
|
||||||
|
getSiteRedirectBySource(params: {
|
||||||
|
organizationId: string;
|
||||||
|
siteId: string;
|
||||||
|
siteShareKey: string | undefined;
|
||||||
|
source: string;
|
||||||
|
}): Promise<{ redirect: api.SiteRedirect | null; target: string } | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an embed by its URL.
|
||||||
|
*/
|
||||||
|
getEmbedByUrl(params: { url: string; spaceId: string }): Promise<api.Embed>;
|
||||||
|
}
|
||||||
+15
-15
@@ -1,6 +1,6 @@
|
|||||||
import { describe, it, expect } from 'bun:test';
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
|
||||||
import { getURLLookupAlternatives, normalizeURL } from './middleware';
|
import { getURLLookupAlternatives, normalizeURL } from './urls';
|
||||||
|
|
||||||
describe('getURLLookupAlternatives', () => {
|
describe('getURLLookupAlternatives', () => {
|
||||||
it('should return all URLs up to the root', () => {
|
it('should return all URLs up to the root', () => {
|
||||||
@@ -68,7 +68,7 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
|
|
||||||
it('should not match before the variant for a variant url', () => {
|
it('should not match before the variant for a variant url', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(new URL('https://test.gitbook.io/v/variant/space')),
|
getURLLookupAlternatives(new URL('https://test.gitbook.io/v/variant/space'))
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: undefined,
|
revision: undefined,
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -85,7 +85,7 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
|
|
||||||
it('should not match before the variant for a variant in a share link', () => {
|
it('should not match before the variant for a variant in a share link', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space')),
|
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space'))
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: undefined,
|
revision: undefined,
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -103,8 +103,8 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
it('should not match before a revision in a variant', () => {
|
it('should not match before a revision in a variant', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(
|
getURLLookupAlternatives(
|
||||||
new URL('https://test.gitbook.io/v/variant/~/revisions/id/rest'),
|
new URL('https://test.gitbook.io/v/variant/~/revisions/id/rest')
|
||||||
),
|
)
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: 'id',
|
revision: 'id',
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -121,7 +121,7 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
|
|
||||||
it('should not match before a revision ID', () => {
|
it('should not match before a revision ID', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/revisions/id/a/b/c')),
|
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/revisions/id/a/b/c'))
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: 'id',
|
revision: 'id',
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -138,7 +138,7 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
|
|
||||||
it('should not match before a change request ID', () => {
|
it('should not match before a change request ID', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/changes/id/hello')),
|
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/changes/id/hello'))
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: undefined,
|
revision: undefined,
|
||||||
changeRequest: 'id',
|
changeRequest: 'id',
|
||||||
@@ -195,7 +195,7 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
|
|
||||||
it('should match a variant in a share-link', () => {
|
it('should match a variant in a share-link', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space')),
|
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space'))
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: undefined,
|
revision: undefined,
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -213,8 +213,8 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
it('should match a revision in a variant in a share-link', () => {
|
it('should match a revision in a variant in a share-link', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(
|
getURLLookupAlternatives(
|
||||||
new URL('https://test.gitbook.io/sharelink/v/variant/~/revisions/id/a/b/c'),
|
new URL('https://test.gitbook.io/sharelink/v/variant/~/revisions/id/a/b/c')
|
||||||
),
|
)
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: 'id',
|
revision: 'id',
|
||||||
changeRequest: undefined,
|
changeRequest: undefined,
|
||||||
@@ -232,8 +232,8 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
it('should match a change request in a variant in a share-link', () => {
|
it('should match a change request in a variant in a share-link', () => {
|
||||||
expect(
|
expect(
|
||||||
getURLLookupAlternatives(
|
getURLLookupAlternatives(
|
||||||
new URL('https://test.gitbook.io/sharelink/v/variant/~/changes/id/a/b/c'),
|
new URL('https://test.gitbook.io/sharelink/v/variant/~/changes/id/a/b/c')
|
||||||
),
|
)
|
||||||
).toEqual({
|
).toEqual({
|
||||||
revision: undefined,
|
revision: undefined,
|
||||||
changeRequest: 'id',
|
changeRequest: 'id',
|
||||||
@@ -367,13 +367,13 @@ describe('getURLLookupAlternatives', () => {
|
|||||||
describe('normalizeURL', () => {
|
describe('normalizeURL', () => {
|
||||||
it('should remove trailing slashes', () => {
|
it('should remove trailing slashes', () => {
|
||||||
expect(normalizeURL(new URL('https://docs.mycompany.com/hello/'))).toEqual(
|
expect(normalizeURL(new URL('https://docs.mycompany.com/hello/'))).toEqual(
|
||||||
new URL('https://docs.mycompany.com/hello'),
|
new URL('https://docs.mycompany.com/hello')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove duplicate slashes', () => {
|
it('should remove duplicate slashes', () => {
|
||||||
expect(normalizeURL(new URL('https://docs.mycompany.com//hello//there'))).toEqual(
|
expect(normalizeURL(new URL('https://docs.mycompany.com//hello//there'))).toEqual(
|
||||||
new URL('https://docs.mycompany.com/hello/there'),
|
new URL('https://docs.mycompany.com/hello/there')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
import { race, tryCatch } from '@/lib/async';
|
||||||
|
import { joinPath } from '@/lib/paths';
|
||||||
|
import { GitBookAPI, type GitBookAPIError, type PublishedSiteContentLookup } from '@gitbook/api';
|
||||||
|
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup a content by its URL using the GitBook API.
|
||||||
|
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
|
||||||
|
*/
|
||||||
|
export async function getPublishedContentByURL(input: {
|
||||||
|
url: string;
|
||||||
|
visitorAuthToken: string | null;
|
||||||
|
redirectOnError: boolean;
|
||||||
|
}): Promise<
|
||||||
|
| { data: PublishedSiteContentLookup; error?: undefined }
|
||||||
|
| { data?: undefined; error: Error | GitBookAPIError }
|
||||||
|
> {
|
||||||
|
const lookupURL = new URL(input.url);
|
||||||
|
const url = stripURLSearch(lookupURL);
|
||||||
|
const lookup = getURLLookupAlternatives(url);
|
||||||
|
|
||||||
|
const result = await race(lookup.urls, async (alternative, { signal }) => {
|
||||||
|
const api = new GitBookAPI({
|
||||||
|
authToken: GITBOOK_API_TOKEN ?? undefined,
|
||||||
|
endpoint: GITBOOK_API_URL,
|
||||||
|
userAgent: GITBOOK_USER_AGENT,
|
||||||
|
});
|
||||||
|
|
||||||
|
const callResult = await tryCatch(
|
||||||
|
api.urls.getPublishedContentByUrl(
|
||||||
|
{
|
||||||
|
url: alternative.url,
|
||||||
|
visitorAuthToken: input.visitorAuthToken ?? undefined,
|
||||||
|
redirectOnError: input.redirectOnError,
|
||||||
|
cache: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
signal,
|
||||||
|
headers: {
|
||||||
|
'x-gitbook-force-cache': 'true',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (callResult.error) {
|
||||||
|
if (alternative.primary) {
|
||||||
|
// We only return an error for the primary alternative (full URL),
|
||||||
|
// as other parts could result in errors due to the URL being incomplete (share links, etc).
|
||||||
|
return { error: callResult.error };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: { data },
|
||||||
|
} = callResult;
|
||||||
|
|
||||||
|
if ('redirect' in data) {
|
||||||
|
if (alternative.primary) {
|
||||||
|
// Append the path to the redirect URL
|
||||||
|
// because we might have matched a shorter path and the redirect is relative to it
|
||||||
|
if (alternative.extraPath) {
|
||||||
|
if (data.target === 'content') {
|
||||||
|
const redirect = new URL(data.redirect);
|
||||||
|
redirect.pathname = joinPath(redirect.pathname, alternative.extraPath);
|
||||||
|
data.redirect = redirect.toString();
|
||||||
|
} else {
|
||||||
|
const redirect = new URL(data.redirect);
|
||||||
|
if (redirect.searchParams.has('location')) {
|
||||||
|
redirect.searchParams.set(
|
||||||
|
'location',
|
||||||
|
joinPath(
|
||||||
|
redirect.searchParams.get('location') ?? '',
|
||||||
|
alternative.extraPath
|
||||||
|
)
|
||||||
|
);
|
||||||
|
data.redirect = redirect.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { data };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We use the following criteria to determine if the lookup result is the right one:
|
||||||
|
* - the primary alternative was resolved (because that's the longest or most inclusive path)
|
||||||
|
* - the resolution of the site URL is complete (because we want to resolve the deepest path possible)
|
||||||
|
*
|
||||||
|
* In both cases, the idea is to use the deepest/longest/most inclusive path to resolve the content.
|
||||||
|
*/
|
||||||
|
if (alternative.primary || ('site' in data && data.complete)) {
|
||||||
|
const siteResult: PublishedSiteContentLookup = {
|
||||||
|
...data,
|
||||||
|
changeRequest: data.changeRequest ?? lookup.changeRequest,
|
||||||
|
revision: data.revision ?? lookup.revision,
|
||||||
|
basePath: joinPath(data.basePath, lookup.basePath ?? ''),
|
||||||
|
pathname: joinPath(data.pathname, alternative.extraPath),
|
||||||
|
};
|
||||||
|
return { data: siteResult };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return {
|
||||||
|
error: new Error('No content found'),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For a given GitBook URL, return a list of alternative URLs that could be matched against to lookup the content.
|
||||||
|
* The approach is optimized to aim at reusing cached lookup results as much as possible.
|
||||||
|
*
|
||||||
|
* The cases can be:
|
||||||
|
*
|
||||||
|
* Custom hostname
|
||||||
|
* - Public content has a custom hostname: docs.company.com/<path>
|
||||||
|
* - Public content has a custom hostname with a share-link: docs.company.com/<link>/<path>
|
||||||
|
* - Public content has a custom hostname with a variant: docs.company.com/v/<variant>/<path>
|
||||||
|
* - Public content has a custom hostname with a variant and a share-link: docs.company.com/<link>/v/<variant>/<path>
|
||||||
|
* - Revisions in a custom hostname: docs.company.com/~/revisions/<id>/<path>
|
||||||
|
* - Changes in a custom hostname: docs.company.com/~/changes/<id>/<path>
|
||||||
|
*
|
||||||
|
* Custom hostname in the organization or GitBook domain (company.gitbook.io)
|
||||||
|
* - Public content has a custom hostname in the organization: docs.company.com/<space>/<path>
|
||||||
|
* - Public content has a custom hostname in the organization with a share-link: docs.company.com/<space>/<link>/<path>
|
||||||
|
* - Public content has a custom hostname in the organization with a variant: docs.company.com/<space>/v/<variant>/<path>
|
||||||
|
* - Public content has a custom hostname in the organization with a variant and a share-link: docs.company.com/<space>/<link>/v/<variant>/<path>
|
||||||
|
*/
|
||||||
|
export function getURLLookupAlternatives(input: URL) {
|
||||||
|
const url = normalizeURL(input);
|
||||||
|
|
||||||
|
let basePath: string | undefined = undefined;
|
||||||
|
let changeRequest: string | undefined = undefined;
|
||||||
|
let revision: string | undefined = undefined;
|
||||||
|
const alternatives: Array<{ url: string; extraPath: string; primary: boolean }> = [];
|
||||||
|
|
||||||
|
const pushAlternative = (adding: URL, extraPath: string) => {
|
||||||
|
const existing = alternatives.find((alt) => alt.url === adding.toString());
|
||||||
|
if (existing) {
|
||||||
|
if (existing.extraPath !== extraPath) {
|
||||||
|
throw new Error(
|
||||||
|
`Invalid extraPath ${extraPath} for url ${adding.toString()}, already set to ${
|
||||||
|
existing.extraPath
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
alternatives.push({
|
||||||
|
url: adding.toString(),
|
||||||
|
extraPath,
|
||||||
|
primary: adding.toString() === url.toString(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const pathSegments = url.pathname.slice(1).split('/');
|
||||||
|
const tildeIndex = pathSegments.indexOf('~');
|
||||||
|
|
||||||
|
// URL looks like a specific content url (with ~/revisions/ or ~/changes/ in the path)
|
||||||
|
// We only start matching after the ~/revisions/ or ~/changes/ segment and we ignore everything before it
|
||||||
|
if (
|
||||||
|
tildeIndex >= 0 &&
|
||||||
|
(pathSegments[tildeIndex + 1] === 'revisions' || pathSegments[tildeIndex + 1] === 'changes')
|
||||||
|
) {
|
||||||
|
const tildeIndex = pathSegments.indexOf('~');
|
||||||
|
const revisionOrChangeIdIndex = tildeIndex + 2;
|
||||||
|
|
||||||
|
basePath = pathSegments.slice(tildeIndex, revisionOrChangeIdIndex + 1).join('/');
|
||||||
|
if (pathSegments[tildeIndex + 1] === 'revisions') {
|
||||||
|
revision = pathSegments[revisionOrChangeIdIndex];
|
||||||
|
} else {
|
||||||
|
changeRequest = pathSegments[revisionOrChangeIdIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match up to the tilde
|
||||||
|
const contentURL = new URL(url);
|
||||||
|
contentURL.pathname = pathSegments.slice(0, tildeIndex).join('/');
|
||||||
|
pushAlternative(contentURL, pathSegments.slice(revisionOrChangeIdIndex + 1).join('/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL looks like a collection url (with /v/ in the path)
|
||||||
|
// We only start matching after the /v/ segment and we ignore everything before it
|
||||||
|
// to avoid potentially matching as a page not found under the default space in the collection
|
||||||
|
else if (pathSegments.includes('v')) {
|
||||||
|
const collectionURL = new URL(url);
|
||||||
|
const vIndex = pathSegments.indexOf('v');
|
||||||
|
collectionURL.pathname = pathSegments.slice(0, vIndex + 2).join('/');
|
||||||
|
|
||||||
|
pushAlternative(collectionURL, pathSegments.slice(vIndex + 2).join('/'));
|
||||||
|
} else {
|
||||||
|
// Match only with the host, if it can be a custom hostname
|
||||||
|
// It should cover most cases of custom domains, and with caching, it should be fast.
|
||||||
|
if (!url.hostname.includes('.gitbook.io') || pathSegments.length === 0) {
|
||||||
|
const noPathURL = new URL(url);
|
||||||
|
noPathURL.pathname = '/';
|
||||||
|
|
||||||
|
pushAlternative(noPathURL, url.pathname.slice(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise match with the first four segments of the path
|
||||||
|
for (let i = 1; i <= 4; i++) {
|
||||||
|
if (pathSegments.length >= i) {
|
||||||
|
const shortURL = new URL(url);
|
||||||
|
shortURL.pathname = pathSegments.slice(0, i).join('/');
|
||||||
|
|
||||||
|
pushAlternative(shortURL, pathSegments.slice(i).join('/'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark the longuest entry to lookup as primary
|
||||||
|
alternatives.sort((a, b) => b.extraPath.length - a.extraPath.length);
|
||||||
|
if (alternatives.length > 0) {
|
||||||
|
alternatives[alternatives.length - 1].primary = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { urls: alternatives, basePath, changeRequest, revision };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a URL to remove duplicate slashes and trailing slashes
|
||||||
|
* and transform the pathname to lowercase.
|
||||||
|
*/
|
||||||
|
export function normalizeURL(url: URL) {
|
||||||
|
const result = new URL(url);
|
||||||
|
result.pathname = url.pathname.replace(/\/{2,}/g, '/').replace(/\/$/, '');
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip the search params from a URL
|
||||||
|
*/
|
||||||
|
export function stripURLSearch(url: URL): URL {
|
||||||
|
const stripped = new URL(url.toString());
|
||||||
|
stripped.search = '';
|
||||||
|
return stripped;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import type { RevisionPageDocument } from '@gitbook/api';
|
||||||
|
import type { GitBookDataFetcher } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the document for a page.
|
||||||
|
*/
|
||||||
|
export async function getPageDocument(
|
||||||
|
dataFetcher: GitBookDataFetcher,
|
||||||
|
spaceId: string,
|
||||||
|
page: RevisionPageDocument
|
||||||
|
) {
|
||||||
|
if (page.documentId) {
|
||||||
|
return dataFetcher.getDocument({ spaceId, documentId: page.documentId });
|
||||||
|
}
|
||||||
|
if (page.computed) {
|
||||||
|
return dataFetcher.getComputedDocument({ spaceId, source: page.computed });
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -1,3 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* Main host on which GitBook is running.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_URL =
|
||||||
|
process.env.NODE_ENV === 'development'
|
||||||
|
? 'http://localhost:3000'
|
||||||
|
: ((process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : undefined) ??
|
||||||
|
process.env.GITBOOK_URL ??
|
||||||
|
'');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL at which static assets are served.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_ASSETS_URL =
|
||||||
|
process.env.NODE_ENV === 'development'
|
||||||
|
? 'http://localhost:3000'
|
||||||
|
: (process.env.GITBOOK_ASSETS_PREFIX ?? GITBOOK_URL);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GitBook app URL.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_APP_URL = process.env.NEXT_PUBLIC_GITBOOK_APP_URL ?? 'https://app.gitbook.com';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default GitBook API URL endpoint.
|
* Default GitBook API URL endpoint.
|
||||||
*/
|
*/
|
||||||
@@ -13,3 +36,25 @@ export const GITBOOK_API_TOKEN = process.env.GITBOOK_API_TOKEN ?? null;
|
|||||||
* User agent to use for API requests.
|
* User agent to use for API requests.
|
||||||
*/
|
*/
|
||||||
export const GITBOOK_USER_AGENT = process.env.GITBOOK_USER_AGENT ?? 'GitBook-Open/2.0.0';
|
export const GITBOOK_USER_AGENT = process.env.GITBOOK_USER_AGENT ?? 'GitBook-Open/2.0.0';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to disable tracking of events into site insights.
|
||||||
|
* This is used to disable tracking in development mode.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_DISABLE_TRACKING = Boolean(
|
||||||
|
!!process.env.GITBOOK_DISABLE_TRACKING || process.env.NODE_ENV !== 'production'
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hostname serving the integrations.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_INTEGRATIONS_HOST =
|
||||||
|
process.env.GITBOOK_INTEGRATIONS_HOST ?? 'integrations.gitbook.com';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint to use for resizing images.
|
||||||
|
* It should be a Cloudflare domain with image resizing enabled.
|
||||||
|
*/
|
||||||
|
export const GITBOOK_IMAGE_RESIZE_URL = process.env.GITBOOK_IMAGE_RESIZE_URL ?? null;
|
||||||
|
export const GITBOOK_IMAGE_RESIZE_SIGNING_KEY =
|
||||||
|
process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY ?? null;
|
||||||
|
|||||||
@@ -0,0 +1,244 @@
|
|||||||
|
import 'server-only';
|
||||||
|
|
||||||
|
import { GITBOOK_IMAGE_RESIZE_SIGNING_KEY, GITBOOK_IMAGE_RESIZE_URL } from '../env';
|
||||||
|
import type { GitBookSpaceLinker } from '../links';
|
||||||
|
import { type SignatureVersion, generateImageSignature } from './signatures';
|
||||||
|
import type { ImageResizer } from './types';
|
||||||
|
|
||||||
|
interface CloudflareImageJsonFormat {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
original: {
|
||||||
|
file_size: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
format: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developers.cloudflare.com/images/image-resizing/resize-with-workers/
|
||||||
|
*/
|
||||||
|
export interface CloudflareImageOptions {
|
||||||
|
format?: 'webp' | 'avif' | 'json' | 'jpeg';
|
||||||
|
fit?: 'scale-down' | 'contain' | 'cover' | 'crop' | 'pad';
|
||||||
|
width?: number;
|
||||||
|
height?: number;
|
||||||
|
dpr?: number;
|
||||||
|
anim?: boolean;
|
||||||
|
quality?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an image resizer for a rendering context.
|
||||||
|
*/
|
||||||
|
export function createImageResizer({
|
||||||
|
host,
|
||||||
|
linker,
|
||||||
|
}: {
|
||||||
|
/** The linker to use to create URLs. */
|
||||||
|
linker: GitBookSpaceLinker;
|
||||||
|
/** The host name of the current site. */
|
||||||
|
host: string;
|
||||||
|
}): ImageResizer {
|
||||||
|
if (!GITBOOK_IMAGE_RESIZE_URL || !GITBOOK_IMAGE_RESIZE_SIGNING_KEY) {
|
||||||
|
return createNoopImageResizer();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
getResizedImageURL: (urlInput) => {
|
||||||
|
if (!checkIsSizableImageURL(urlInput)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cachedSignature: {
|
||||||
|
signature: string;
|
||||||
|
version: SignatureVersion;
|
||||||
|
} | null = null;
|
||||||
|
|
||||||
|
return async (options) => {
|
||||||
|
cachedSignature ??= await generateImageSignature({
|
||||||
|
host,
|
||||||
|
url: urlInput,
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = new URL(
|
||||||
|
linker.toAbsoluteURL(linker.toPathInContent('/~gitbook/image'))
|
||||||
|
);
|
||||||
|
url.searchParams.set('url', getImageAPIUrl(urlInput));
|
||||||
|
|
||||||
|
if (options.width) {
|
||||||
|
url.searchParams.set('width', options.width.toString());
|
||||||
|
}
|
||||||
|
if (options.height) {
|
||||||
|
url.searchParams.set('height', options.height.toString());
|
||||||
|
}
|
||||||
|
if (options.dpr) {
|
||||||
|
url.searchParams.set('dpr', options.dpr.toString());
|
||||||
|
}
|
||||||
|
if (options.quality) {
|
||||||
|
url.searchParams.set('quality', options.quality.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
url.searchParams.set('sign', cachedSignature.signature);
|
||||||
|
url.searchParams.set('sv', cachedSignature.version);
|
||||||
|
|
||||||
|
return url.toString();
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
getImageSize: async (input, options) => {
|
||||||
|
if (!checkIsSizableImageURL(input)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getImageSize(input, options);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an image resizer that doesn't do any resizing.
|
||||||
|
*/
|
||||||
|
export function createNoopImageResizer(): ImageResizer {
|
||||||
|
return {
|
||||||
|
getResizedImageURL: () => null,
|
||||||
|
getImageSize: async (_input) => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a URL is an HTTP URL.
|
||||||
|
*/
|
||||||
|
export function checkIsHttpURL(input: string | URL): boolean {
|
||||||
|
if (!URL.canParse(input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const parsed = new URL(input);
|
||||||
|
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an image URL is resizable.
|
||||||
|
* Skip it for non-http(s) URLs (data, etc).
|
||||||
|
* Skip it for SVGs.
|
||||||
|
* Skip it for GitBook images (to avoid recursion).
|
||||||
|
*/
|
||||||
|
export function checkIsSizableImageURL(input: string): boolean {
|
||||||
|
if (!URL.canParse(input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.includes('/~gitbook/image')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = new URL(input);
|
||||||
|
if (parsed.pathname.endsWith('.svg') || parsed.pathname.endsWith('.avif')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!checkIsHttpURL(parsed)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the size of an image.
|
||||||
|
*/
|
||||||
|
export async function getImageSize(
|
||||||
|
input: string,
|
||||||
|
defaultSize: Partial<CloudflareImageOptions> = {}
|
||||||
|
): Promise<{ width: number; height: number } | null> {
|
||||||
|
if (!checkIsSizableImageURL(input)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await resizeImage(input, {
|
||||||
|
// Abort the request after 2 seconds to avoid blocking rendering for too long
|
||||||
|
signal: AbortSignal.timeout(2000),
|
||||||
|
// Measure size and resize it to the most common size
|
||||||
|
// to optimize caching
|
||||||
|
...defaultSize,
|
||||||
|
format: 'json',
|
||||||
|
anim: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = (await response.json()) as CloudflareImageJsonFormat;
|
||||||
|
return {
|
||||||
|
width: json.original.width,
|
||||||
|
height: json.original.height,
|
||||||
|
};
|
||||||
|
} catch (_error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a Cloudflare Image Resize operation on an image.
|
||||||
|
*/
|
||||||
|
export async function resizeImage(
|
||||||
|
input: string,
|
||||||
|
options: CloudflareImageOptions & {
|
||||||
|
signal?: AbortSignal;
|
||||||
|
}
|
||||||
|
): Promise<Response> {
|
||||||
|
const { signal, ...resizeOptions } = options;
|
||||||
|
|
||||||
|
const parsed = new URL(input);
|
||||||
|
if (parsed.protocol === 'data:') {
|
||||||
|
throw new Error('Cannot resize data: URLs');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.hostname === 'localhost') {
|
||||||
|
throw new Error('Cannot resize localhost URLs');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since Cloudflare Images options on fetch are not supported on Cloudflare Pages,
|
||||||
|
// we need to use the Cloudflare Image Resize API directly.
|
||||||
|
if (!GITBOOK_IMAGE_RESIZE_URL) {
|
||||||
|
throw new Error('GITBOOK_IMAGE_RESIZE_URL is not set');
|
||||||
|
}
|
||||||
|
|
||||||
|
return await fetch(
|
||||||
|
`${GITBOOK_IMAGE_RESIZE_URL}${stringifyOptions(
|
||||||
|
resizeOptions
|
||||||
|
)}/${encodeURIComponent(input)}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
|
||||||
|
Accept:
|
||||||
|
resizeOptions.format === 'json'
|
||||||
|
? 'application/json'
|
||||||
|
: `image/${resizeOptions.format || 'jpeg'}`,
|
||||||
|
},
|
||||||
|
signal,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringifyOptions(options: CloudflareImageOptions): string {
|
||||||
|
return Object.entries({ ...options }).reduce((rest, [key, value]) => {
|
||||||
|
return `${rest}${rest ? ',' : ''}${key}=${value}`;
|
||||||
|
}, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Because of a bug in Cloudflare, 127.0.0.1 is replaced by localhost.
|
||||||
|
* We protect against it by converting to a special token, and then parsing
|
||||||
|
* the token in the image API.
|
||||||
|
*/
|
||||||
|
const GITBOOK_LOCALHOST_TOKEN = '$GITBOOK_LOCALHOST$';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare a URL for the GitBook Open Image API.
|
||||||
|
*/
|
||||||
|
export function getImageAPIUrl(url: string): string {
|
||||||
|
return url.replaceAll('127.0.0.1', GITBOOK_LOCALHOST_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseImageAPIURL(url: string): string {
|
||||||
|
return url.replaceAll(GITBOOK_LOCALHOST_TOKEN, '127.0.0.1');
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export * from './types';
|
||||||
|
export * from './createImageResizer';
|
||||||
|
export * from './signatures';
|
||||||
|
export * from './utils';
|
||||||
+36
-28
@@ -2,8 +2,8 @@ import 'server-only';
|
|||||||
|
|
||||||
import fnv1a from '@sindresorhus/fnv1a';
|
import fnv1a from '@sindresorhus/fnv1a';
|
||||||
import type { MaybePromise } from 'p-map';
|
import type { MaybePromise } from 'p-map';
|
||||||
|
import { assert } from 'ts-essentials';
|
||||||
import { getHost } from './links';
|
import { GITBOOK_IMAGE_RESIZE_SIGNING_KEY } from '../env';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GitBook has supported different version of image signing in the past. To maintain backwards
|
* GitBook has supported different version of image signing in the past. To maintain backwards
|
||||||
@@ -16,26 +16,19 @@ export type SignatureVersion = '0' | '1' | '2';
|
|||||||
*/
|
*/
|
||||||
export const CURRENT_SIGNATURE_VERSION: SignatureVersion = '2';
|
export const CURRENT_SIGNATURE_VERSION: SignatureVersion = '2';
|
||||||
|
|
||||||
/**
|
type SignFnInput = {
|
||||||
* A mapping of signature versions to signature functions.
|
url: string;
|
||||||
*/
|
host: string;
|
||||||
const IMAGE_SIGNATURE_FUNCTIONS: Record<SignatureVersion, (input: string) => MaybePromise<string>> =
|
};
|
||||||
{
|
|
||||||
'0': generateSignatureV0,
|
|
||||||
'1': generateSignatureV1,
|
|
||||||
'2': generateSignatureV2,
|
|
||||||
};
|
|
||||||
|
|
||||||
export function isSignatureVersion(input: string): input is SignatureVersion {
|
type SignFn = (input: SignFnInput) => MaybePromise<string>;
|
||||||
return Object.keys(IMAGE_SIGNATURE_FUNCTIONS).includes(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify a signature of an image URL
|
* Verify a signature of an image URL
|
||||||
*/
|
*/
|
||||||
export async function verifyImageSignature(
|
export async function verifyImageSignature(
|
||||||
input: string,
|
input: SignFnInput,
|
||||||
{ signature, version }: { signature: string; version: SignatureVersion },
|
{ signature, version }: { signature: string; version: SignatureVersion }
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const generator = IMAGE_SIGNATURE_FUNCTIONS[version];
|
const generator = IMAGE_SIGNATURE_FUNCTIONS[version];
|
||||||
const generated = await generator(input);
|
const generated = await generator(input);
|
||||||
@@ -48,7 +41,7 @@ export async function verifyImageSignature(
|
|||||||
* This function is sync. If you need to implement an async version of image signing, you'll need to change
|
* This function is sync. If you need to implement an async version of image signing, you'll need to change
|
||||||
* ths signature of this fn and where it's used.
|
* ths signature of this fn and where it's used.
|
||||||
*/
|
*/
|
||||||
export async function generateImageSignature(input: string): Promise<{
|
export async function generateImageSignature(input: SignFnInput): Promise<{
|
||||||
signature: string;
|
signature: string;
|
||||||
version: SignatureVersion;
|
version: SignatureVersion;
|
||||||
}> {
|
}> {
|
||||||
@@ -63,17 +56,17 @@ const fnv1aUtf8Buffer = new Uint8Array(512);
|
|||||||
* Generate a signature for an image.
|
* Generate a signature for an image.
|
||||||
* The signature is relative to the current site being rendered to avoid serving images from other sites on the same domain.
|
* The signature is relative to the current site being rendered to avoid serving images from other sites on the same domain.
|
||||||
*/
|
*/
|
||||||
async function generateSignatureV2(input: string): Promise<string> {
|
const generateSignatureV2: SignFn = async (input) => {
|
||||||
const hostName = await getHost();
|
assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
|
||||||
const all = [
|
const all = [
|
||||||
input,
|
input.url,
|
||||||
hostName, // The hostname is used to avoid serving images from other sites on the same domain
|
input.host, // The hostname is used to avoid serving images from other sites on the same domain
|
||||||
process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
|
||||||
]
|
]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join(':');
|
.join(':');
|
||||||
return fnv1a(all, { utf8Buffer: fnv1aUtf8Buffer }).toString(16);
|
return fnv1a(all, { utf8Buffer: fnv1aUtf8Buffer }).toString(16);
|
||||||
}
|
};
|
||||||
|
|
||||||
// Reused buffer for FNV-1a hashing in the v1 algorithm
|
// Reused buffer for FNV-1a hashing in the v1 algorithm
|
||||||
const fnv1aUtf8BufferV1 = new Uint8Array(512);
|
const fnv1aUtf8BufferV1 = new Uint8Array(512);
|
||||||
@@ -83,22 +76,37 @@ const fnv1aUtf8BufferV1 = new Uint8Array(512);
|
|||||||
* When setting it in a URL, we use version '1' for the 'sv' querystring parameneter
|
* When setting it in a URL, we use version '1' for the 'sv' querystring parameneter
|
||||||
* to know that it was the algorithm that was used.
|
* to know that it was the algorithm that was used.
|
||||||
*/
|
*/
|
||||||
function generateSignatureV1(input: string): string {
|
const generateSignatureV1: SignFn = async (input) => {
|
||||||
const all = [input, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
|
||||||
|
const all = [input.url, GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
||||||
return fnv1a(all, { utf8Buffer: fnv1aUtf8BufferV1 }).toString(16);
|
return fnv1a(all, { utf8Buffer: fnv1aUtf8BufferV1 }).toString(16);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initial algorithm used to generate a signature for an image. It didn't use any versioning in the URL.
|
* Initial algorithm used to generate a signature for an image. It didn't use any versioning in the URL.
|
||||||
* We still need it to validate older signatures that were generated without versioning
|
* We still need it to validate older signatures that were generated without versioning
|
||||||
* but still exist in previously generated and cached content.
|
* but still exist in previously generated and cached content.
|
||||||
*/
|
*/
|
||||||
async function generateSignatureV0(input: string): Promise<string> {
|
const generateSignatureV0: SignFn = async (input) => {
|
||||||
const all = [input, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
|
||||||
|
const all = [input.url, GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
|
||||||
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all));
|
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all));
|
||||||
|
|
||||||
// Convert ArrayBuffer to hex string
|
// Convert ArrayBuffer to hex string
|
||||||
const hashArray = Array.from(new Uint8Array(hash));
|
const hashArray = Array.from(new Uint8Array(hash));
|
||||||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
|
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
|
||||||
return hashHex;
|
return hashHex;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A mapping of signature versions to signature functions.
|
||||||
|
*/
|
||||||
|
const IMAGE_SIGNATURE_FUNCTIONS: Record<SignatureVersion, SignFn> = {
|
||||||
|
'0': generateSignatureV0,
|
||||||
|
'1': generateSignatureV1,
|
||||||
|
'2': generateSignatureV2,
|
||||||
|
};
|
||||||
|
|
||||||
|
export function isSignatureVersion(input: string): input is SignatureVersion {
|
||||||
|
return Object.keys(IMAGE_SIGNATURE_FUNCTIONS).includes(input);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
export type GetImageSizeOptions = {
|
||||||
|
dpr?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResizeImageOptions = GetImageSizeOptions & {
|
||||||
|
width?: number;
|
||||||
|
height?: number;
|
||||||
|
dpr?: number;
|
||||||
|
quality?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
interface ImageSize {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ImageResizer = {
|
||||||
|
/**
|
||||||
|
* Resize an image.
|
||||||
|
* @param input - The image URL to resize.
|
||||||
|
* @param options - The options to resize the image.
|
||||||
|
*/
|
||||||
|
getResizedImageURL(imageURL: string): null | ((options: ResizeImageOptions) => Promise<string>);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the size of an image.
|
||||||
|
*/
|
||||||
|
getImageSize(imageURL: string, options: GetImageSizeOptions): Promise<ImageSize | null>;
|
||||||
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { ImageResizer, ResizeImageOptions } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quick utility to get a resized image URL.
|
||||||
|
*/
|
||||||
|
export async function getResizedImageURL(
|
||||||
|
resizer: ImageResizer | undefined,
|
||||||
|
url: string,
|
||||||
|
options: ResizeImageOptions
|
||||||
|
) {
|
||||||
|
const getURL = resizer?.getResizedImageURL(url);
|
||||||
|
if (!getURL) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await getURL(options);
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { appendBasePathToLinker, createLinker } from './links';
|
||||||
|
|
||||||
|
const root = createLinker({
|
||||||
|
host: 'docs.company.com',
|
||||||
|
pathname: '/',
|
||||||
|
});
|
||||||
|
|
||||||
|
const variantInSection = createLinker({
|
||||||
|
host: 'docs.company.com',
|
||||||
|
pathname: '/section/variant',
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toPathInContent', () => {
|
||||||
|
it('should return the correct path', () => {
|
||||||
|
expect(root.toPathInContent('some/path')).toBe('/some/path');
|
||||||
|
expect(variantInSection.toPathInContent('some/path')).toBe('/section/variant/some/path');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle leading slash', () => {
|
||||||
|
expect(root.toPathInContent('/some/path')).toBe('/some/path');
|
||||||
|
expect(variantInSection.toPathInContent('/some/path')).toBe('/section/variant/some/path');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toAbsoluteURL', () => {
|
||||||
|
it('should return the correct path', () => {
|
||||||
|
expect(root.toAbsoluteURL('some/path')).toBe('https://docs.company.com/some/path');
|
||||||
|
expect(variantInSection.toAbsoluteURL('some/path')).toBe(
|
||||||
|
'https://docs.company.com/some/path'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('appendBasePathToLinker', () => {
|
||||||
|
const prefixedRoot = appendBasePathToLinker(root, '/section/variant');
|
||||||
|
const prefixedVariantInSection = appendBasePathToLinker(variantInSection, '/base');
|
||||||
|
|
||||||
|
describe('toPathInContent', () => {
|
||||||
|
it('should return the correct path', () => {
|
||||||
|
expect(prefixedRoot.toPathInContent('some/path')).toBe('/section/variant/some/path');
|
||||||
|
expect(prefixedVariantInSection.toPathInContent('some/path')).toBe(
|
||||||
|
'/section/variant/base/some/path'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toAbsoluteURL', () => {
|
||||||
|
it('should return the correct path', () => {
|
||||||
|
expect(prefixedRoot.toAbsoluteURL('some/path')).toBe(
|
||||||
|
'https://docs.company.com/some/path'
|
||||||
|
);
|
||||||
|
expect(prefixedVariantInSection.toAbsoluteURL('some/path')).toBe(
|
||||||
|
'https://docs.company.com/some/path'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import { getPagePath } from '@/lib/pages';
|
||||||
|
import type { RevisionPage, RevisionPageDocument, RevisionPageGroup } from '@gitbook/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic interface to generate links based on a given context.
|
||||||
|
*
|
||||||
|
* URL levels:
|
||||||
|
*
|
||||||
|
* https://docs.company.com/section/variant/page
|
||||||
|
*
|
||||||
|
* toPathInContent('some/path') => /section/variant/some/path
|
||||||
|
* toPathForPage({ pages, page }) => /section/variant/some/path
|
||||||
|
* toAbsoluteURL('some/path') => https://docs.company.com/some/path
|
||||||
|
*/
|
||||||
|
export interface GitBookSpaceLinker {
|
||||||
|
/**
|
||||||
|
* Generate an absolute path for a relative path to the current content.
|
||||||
|
*/
|
||||||
|
toPathInContent(relativePath: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an absolute path for a page in the current content.
|
||||||
|
* The result should NOT be passed to `toPathInContent`.
|
||||||
|
*/
|
||||||
|
toPathForPage(input: {
|
||||||
|
pages: RevisionPage[];
|
||||||
|
page: RevisionPageDocument | RevisionPageGroup;
|
||||||
|
anchor?: string;
|
||||||
|
}): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an absolute URL for a given path relative to the host of the current content.
|
||||||
|
*/
|
||||||
|
toAbsoluteURL(absolutePath: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a link (URL or path) for a GitBook content URL (url of another site)
|
||||||
|
*/
|
||||||
|
toLinkForContent(url: string): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a linker to resolve links in a context being served on a specific URL.
|
||||||
|
*/
|
||||||
|
export function createLinker(
|
||||||
|
/** Where the top of the space is served on */
|
||||||
|
servedOn: {
|
||||||
|
protocol?: string;
|
||||||
|
host: string;
|
||||||
|
pathname: string;
|
||||||
|
}
|
||||||
|
): GitBookSpaceLinker {
|
||||||
|
if (servedOn.host.includes('/')) {
|
||||||
|
throw new Error('Host cannot include a slash');
|
||||||
|
}
|
||||||
|
|
||||||
|
const linker: GitBookSpaceLinker = {
|
||||||
|
toPathInContent(relativePath: string): string {
|
||||||
|
return joinPaths(servedOn.pathname, relativePath);
|
||||||
|
},
|
||||||
|
|
||||||
|
toAbsoluteURL(absolutePath: string): string {
|
||||||
|
return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`;
|
||||||
|
},
|
||||||
|
|
||||||
|
toPathForPage({ pages, page, anchor }) {
|
||||||
|
return linker.toPathInContent(getPagePath(pages, page)) + (anchor ? `#${anchor}` : '');
|
||||||
|
},
|
||||||
|
|
||||||
|
toLinkForContent(url: string): string {
|
||||||
|
return url;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return linker;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a prefix to a linker.
|
||||||
|
*/
|
||||||
|
export function appendBasePathToLinker(
|
||||||
|
linker: GitBookSpaceLinker,
|
||||||
|
basePath: string
|
||||||
|
): GitBookSpaceLinker {
|
||||||
|
const linkerWithPrefix: GitBookSpaceLinker = {
|
||||||
|
toPathInContent(relativePath: string): string {
|
||||||
|
return linker.toPathInContent(joinPaths(basePath, relativePath));
|
||||||
|
},
|
||||||
|
|
||||||
|
toAbsoluteURL(absolutePath: string): string {
|
||||||
|
return linker.toAbsoluteURL(absolutePath);
|
||||||
|
},
|
||||||
|
|
||||||
|
toPathForPage({ pages, page, anchor }) {
|
||||||
|
return (
|
||||||
|
linkerWithPrefix.toPathInContent(getPagePath(pages, page)) +
|
||||||
|
(anchor ? `#${anchor}` : '')
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
toLinkForContent(url: string): string {
|
||||||
|
return linker.toLinkForContent(url);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return linkerWithPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
function joinPaths(prefix: string, path: string): string {
|
||||||
|
const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`;
|
||||||
|
const suffixPath = path.startsWith('/') ? path.slice(1) : path;
|
||||||
|
return prefixPath + suffixPath;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { CustomizationThemeMode } from '@gitbook/api';
|
||||||
|
import { headers } from 'next/headers';
|
||||||
|
|
||||||
|
export enum MiddlewareHeaders {
|
||||||
|
URLMode = 'x-gitbook-url-mode',
|
||||||
|
Theme = 'x-gitbook-theme',
|
||||||
|
Customization = 'x-gitbook-customization',
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the URL mode from the middleware headers.
|
||||||
|
*/
|
||||||
|
export async function getURLModeFromMiddleware(): Promise<'url' | 'url-host'> {
|
||||||
|
const headersList = await headers();
|
||||||
|
const mode = headersList.get(MiddlewareHeaders.URLMode);
|
||||||
|
if (!mode) {
|
||||||
|
throw new Error('URL mode is not set by the middleware');
|
||||||
|
}
|
||||||
|
|
||||||
|
return mode as 'url' | 'url-host';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For preview, the theme can be set via query string (?theme=light).
|
||||||
|
*/
|
||||||
|
export async function getThemeFromMiddleware() {
|
||||||
|
const headersList = await headers();
|
||||||
|
const queryStringTheme = headersList.get(MiddlewareHeaders.Theme);
|
||||||
|
if (!queryStringTheme) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryStringTheme === 'light'
|
||||||
|
? CustomizationThemeMode.Light
|
||||||
|
: CustomizationThemeMode.Dark;
|
||||||
|
}
|
||||||
@@ -1,11 +1,175 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { GitBookAPIError } from '@gitbook/api';
|
||||||
import type { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { getContentSecurityPolicy } from '@/lib/csp';
|
||||||
|
import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
|
||||||
|
import { serveResizedImage } from '@/routes/image';
|
||||||
|
import { getPublishedContentByURL } from '@v2/lib/data';
|
||||||
|
import { MiddlewareHeaders } from '@v2/lib/middleware';
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ['/((?!_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)'],
|
matcher: ['/((?!_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)'],
|
||||||
};
|
};
|
||||||
|
|
||||||
export function middleware(request: NextRequest) {
|
type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
|
||||||
// No rewrite
|
|
||||||
return NextResponse.next();
|
export async function middleware(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
/**
|
||||||
|
* Serve image resizing requests (all requests containing `/~gitbook/image`).
|
||||||
|
* All URLs containing `/~gitbook/image` are rewritten to `/~gitbook/image`
|
||||||
|
* and serve from a single route handler.
|
||||||
|
*
|
||||||
|
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
|
||||||
|
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
|
||||||
|
*/
|
||||||
|
if (request.nextUrl.pathname.endsWith('/~gitbook/image')) {
|
||||||
|
return serveResizedImage(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route all requests to a site
|
||||||
|
const extracted = extractURL(request);
|
||||||
|
if (extracted) {
|
||||||
|
return serveSiteByURL(request, extracted);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the rest with the router default logic
|
||||||
|
return NextResponse.next();
|
||||||
|
} catch (error) {
|
||||||
|
return serveErrorResponse(error as Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serve site by URL.
|
||||||
|
*/
|
||||||
|
async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) {
|
||||||
|
const dynamicHeaders = getDynamicHeaders(request);
|
||||||
|
const { url, mode } = urlWithMode;
|
||||||
|
|
||||||
|
const result = await getPublishedContentByURL({
|
||||||
|
url: url.toString(),
|
||||||
|
visitorAuthToken: null,
|
||||||
|
redirectOnError: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
throw result.error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = result;
|
||||||
|
|
||||||
|
if ('redirect' in data) {
|
||||||
|
return NextResponse.redirect(data.redirect);
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestHeaders = new Headers(request.headers);
|
||||||
|
requestHeaders.set(MiddlewareHeaders.URLMode, mode);
|
||||||
|
if (dynamicHeaders) {
|
||||||
|
for (const [key, value] of Object.entries(dynamicHeaders)) {
|
||||||
|
requestHeaders.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const route = [
|
||||||
|
'sites',
|
||||||
|
dynamicHeaders ? 'dynamic' : 'static',
|
||||||
|
mode,
|
||||||
|
encodeURIComponent(url.host + data.basePath),
|
||||||
|
encodePathInSiteContent(data.pathname),
|
||||||
|
].join('/');
|
||||||
|
|
||||||
|
console.log('route', route);
|
||||||
|
|
||||||
|
const response = NextResponse.rewrite(new URL(`/${route}`, request.url), {
|
||||||
|
headers: requestHeaders,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add Content Security Policy header
|
||||||
|
response.headers.set('content-security-policy', getContentSecurityPolicy());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serve an error response.
|
||||||
|
*/
|
||||||
|
function serveErrorResponse(error: Error) {
|
||||||
|
if (error instanceof GitBookAPIError) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: error.message },
|
||||||
|
{ status: 500, headers: { 'content-type': 'application/json' } }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URL of the GitBook content can be passed in 3 different ways:
|
||||||
|
* - The request URL is in the `X-GitBook-URL` header.
|
||||||
|
* - Hostname is in the `X-GitBook-Host` header and the pathname is the path in the request URL.
|
||||||
|
* - The request URL is matching `/url/:url`
|
||||||
|
*/
|
||||||
|
function extractURL(request: NextRequest): URLWithMode | null {
|
||||||
|
const xGitbookUrl = request.headers.get('x-gitbook-url');
|
||||||
|
if (xGitbookUrl) {
|
||||||
|
return {
|
||||||
|
url: new URL(xGitbookUrl),
|
||||||
|
mode: 'url-host',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const xGitbookHost = request.headers.get('x-gitbook-host');
|
||||||
|
if (xGitbookHost) {
|
||||||
|
return {
|
||||||
|
url: new URL(`https://${xGitbookHost}${request.nextUrl.pathname}`),
|
||||||
|
mode: 'url-host',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const prefix = '/url/';
|
||||||
|
if (request.nextUrl.pathname.startsWith(prefix)) {
|
||||||
|
return {
|
||||||
|
url: new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
|
||||||
|
mode: 'url',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate if a request is dynamic or static.
|
||||||
|
*/
|
||||||
|
function getDynamicHeaders(_request: NextRequest): null | Record<string, string> {
|
||||||
|
// TODO:
|
||||||
|
// - check token in query string
|
||||||
|
// - check token in cookies
|
||||||
|
// - check special headers or query string
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode path in a site content.
|
||||||
|
* Special paths are not encoded and passed to be handled by the route handlers.
|
||||||
|
*/
|
||||||
|
function encodePathInSiteContent(rawPathname: string) {
|
||||||
|
const pathname = removeLeadingSlash(removeTrailingSlash(rawPathname));
|
||||||
|
|
||||||
|
if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) {
|
||||||
|
return pathname;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (pathname) {
|
||||||
|
case '~gitbook/icon':
|
||||||
|
case '~gitbook/image':
|
||||||
|
case 'llms.txt':
|
||||||
|
case 'sitemap.xml':
|
||||||
|
case 'robots.txt':
|
||||||
|
return pathname;
|
||||||
|
default:
|
||||||
|
return encodeURIComponent(pathname || '/');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import config from '../gitbook/tailwind.config';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
...config,
|
||||||
|
content: [
|
||||||
|
'../gitbook/src/pages/**/*.{js,ts,jsx,tsx,mdx}',
|
||||||
|
'../gitbook/src/components/**/*.{js,ts,jsx,tsx,mdx}',
|
||||||
|
'../gitbook/src/app/**/*.{js,ts,jsx,tsx,mdx}',
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": false,
|
"strict": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"module": "esnext",
|
"module": "esnext",
|
||||||
@@ -19,10 +19,26 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./src/*"],
|
"@v2/*": ["./src/*"],
|
||||||
"@v1/*": ["../gitbook/src/*"]
|
"@/*": ["../gitbook/src/*"]
|
||||||
}
|
},
|
||||||
|
"types": [
|
||||||
|
"bun-types" // add Bun global
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
|
"include": [
|
||||||
"exclude": ["node_modules"]
|
"next-env.d.ts",
|
||||||
|
".next/types/**/*.ts",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.tsx",
|
||||||
|
"../gitbook/types/**/*.d.ts",
|
||||||
|
"../gitbook/cf-env.d.ts"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"packages/openapi-parser",
|
||||||
|
"packages/react-openapi",
|
||||||
|
"packages/react-math",
|
||||||
|
"packages/gitbook"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "next/core-web-vitals",
|
|
||||||
"plugins": ["import"],
|
|
||||||
"rules": {
|
|
||||||
"import/order": [
|
|
||||||
"error",
|
|
||||||
{
|
|
||||||
"groups": ["builtin", "external", ["internal", "parent", "sibling", "index"]],
|
|
||||||
"newlines-between": "always",
|
|
||||||
"distinctGroup": true,
|
|
||||||
"pathGroups": [
|
|
||||||
{
|
|
||||||
"pattern": "@/**",
|
|
||||||
"group": "external",
|
|
||||||
"position": "after"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "@gitbook/**",
|
|
||||||
"group": "external",
|
|
||||||
"position": "after"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"alphabetize": {
|
|
||||||
"order": "asc",
|
|
||||||
"caseInsensitive": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"@next/next/no-img-element": ["off"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,57 @@
|
|||||||
# gitbook
|
# gitbook
|
||||||
|
|
||||||
|
## 0.6.4
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 9b914d1: Fix getProxyModeBasePath that was computing incorrect base path in some scenarios
|
||||||
|
- 2ae76f9: Change how a site in proxy mode is resolved
|
||||||
|
- 027a859: Add support for links style customization option
|
||||||
|
- 3e11678: fix: lost section groups
|
||||||
|
- 3319375: Support OpenAPI operation block
|
||||||
|
- Updated dependencies [722f02e]
|
||||||
|
- Updated dependencies [0924259]
|
||||||
|
- @gitbook/react-openapi@1.0.4
|
||||||
|
- @gitbook/openapi-parser@2.0.1
|
||||||
|
|
||||||
|
## 0.6.3
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- a820739: Remove unused search api method from gitbook/api/lib
|
||||||
|
- a054554: Implement a trusted mode to speed up OpenAPI spec validation
|
||||||
|
- 66d0fc0: Update design for hint block: use semantic colors (info, warning, danger, success) and add alternative styling for hints with headings
|
||||||
|
- 9f0de74: Add support for new OpenAPI ref
|
||||||
|
- da55fac: Render GitBook blocks in OpenAPI operation description
|
||||||
|
- Updated dependencies [c808bb1]
|
||||||
|
- Updated dependencies [dc2dbc5]
|
||||||
|
- Updated dependencies [f1d1d2f]
|
||||||
|
- Updated dependencies [e24206e]
|
||||||
|
- Updated dependencies [a054554]
|
||||||
|
- Updated dependencies [05e1d8c]
|
||||||
|
- Updated dependencies [b4a12d6]
|
||||||
|
- Updated dependencies [9f0de74]
|
||||||
|
- Updated dependencies [da55fac]
|
||||||
|
- @gitbook/openapi-parser@2.0.0
|
||||||
|
- @gitbook/react-openapi@1.0.3
|
||||||
|
|
||||||
|
## 0.6.2
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 359bb97: Fix opening external links when the GitBook page is embedded in an iframe
|
||||||
|
- 6157583: Improve Markdown parsing
|
||||||
|
- 82cd9f2: Add support for anchor links in OpenAPI blocks
|
||||||
|
- Updated dependencies [445baaa]
|
||||||
|
- Updated dependencies [bb5c6a4]
|
||||||
|
- Updated dependencies [a3f1fea]
|
||||||
|
- Updated dependencies [6157583]
|
||||||
|
- Updated dependencies [7419ee7]
|
||||||
|
- Updated dependencies [82cd9f2]
|
||||||
|
- @gitbook/colors@0.2.0
|
||||||
|
- @gitbook/react-openapi@1.0.2
|
||||||
|
- @gitbook/openapi-parser@1.0.1
|
||||||
|
|
||||||
## 0.6.1
|
## 0.6.1
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { runTestCases, TestsCase, waitForCookiesDialog } from './util';
|
import { type TestsCase, runTestCases } from './util';
|
||||||
|
|
||||||
/** A list of test cases to run on the customers' docs sites. */
|
/** A list of test cases to run on the customers' docs sites. */
|
||||||
const testCases: TestsCase[] = [
|
const testCases: TestsCase[] = [
|
||||||
|
|||||||
@@ -9,12 +9,13 @@ import { expect } from '@playwright/test';
|
|||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
VISITOR_TOKEN_COOKIE,
|
||||||
getVisitorAuthCookieName,
|
getVisitorAuthCookieName,
|
||||||
getVisitorAuthCookieValue,
|
getVisitorAuthCookieValue,
|
||||||
VISITOR_TOKEN_COOKIE,
|
|
||||||
} from '@/lib/visitor-token';
|
} from '@/lib/visitor-token';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
type TestsCase,
|
||||||
allDeprecatedThemePresets,
|
allDeprecatedThemePresets,
|
||||||
allLocales,
|
allLocales,
|
||||||
allSidebarBackgroundStyles,
|
allSidebarBackgroundStyles,
|
||||||
@@ -24,7 +25,6 @@ import {
|
|||||||
getCustomizationURL,
|
getCustomizationURL,
|
||||||
headerLinks,
|
headerLinks,
|
||||||
runTestCases,
|
runTestCases,
|
||||||
TestsCase,
|
|
||||||
waitForCookiesDialog,
|
waitForCookiesDialog,
|
||||||
} from './util';
|
} from './util';
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ const testCases: TestsCase[] = [
|
|||||||
url: '',
|
url: '',
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(page.locator('[data-testid="space-dropdown-button"]')).toHaveCount(
|
await expect(page.locator('[data-testid="space-dropdown-button"]')).toHaveCount(
|
||||||
0,
|
0
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -114,20 +114,20 @@ const testCases: TestsCase[] = [
|
|||||||
await spaceDrowpdown.click();
|
await spaceDrowpdown.click();
|
||||||
|
|
||||||
const variantSelectionDropdown = page.locator(
|
const variantSelectionDropdown = page.locator(
|
||||||
'css=[data-testid="space-dropdown-button"] + div',
|
'css=[data-testid="space-dropdown-button"] + div'
|
||||||
);
|
);
|
||||||
// the customized space title
|
// the customized space title
|
||||||
await expect(
|
await expect(
|
||||||
variantSelectionDropdown.getByRole('link', {
|
variantSelectionDropdown.getByRole('link', {
|
||||||
name: 'Multi-Variants',
|
name: 'Multi-Variants',
|
||||||
}),
|
})
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
|
|
||||||
// the NON-customized space title
|
// the NON-customized space title
|
||||||
await expect(
|
await expect(
|
||||||
variantSelectionDropdown.getByRole('link', {
|
variantSelectionDropdown.getByRole('link', {
|
||||||
name: 'RFCs',
|
name: 'RFCs',
|
||||||
}),
|
})
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -156,7 +156,7 @@ const testCases: TestsCase[] = [
|
|||||||
|
|
||||||
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
||||||
await page.waitForURL(
|
await page.waitForURL(
|
||||||
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions/2.0/reference/api-reference/pets?fallback=true',
|
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions/2.0/reference/api-reference/pets?fallback=true'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -179,7 +179,7 @@ const testCases: TestsCase[] = [
|
|||||||
|
|
||||||
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
||||||
await page.waitForURL(
|
await page.waitForURL(
|
||||||
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-share-links/8tNo6MeXg7CkFMzSSz81/2.0/reference/api-reference/pets?fallback=true',
|
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-share-links/8tNo6MeXg7CkFMzSSz81/2.0/reference/api-reference/pets?fallback=true'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -195,7 +195,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `api-multi-versions-va/reference/api-reference/pets?jwt_token=${token}`;
|
return `api-multi-versions-va/reference/api-reference/pets?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -214,7 +214,38 @@ const testCases: TestsCase[] = [
|
|||||||
|
|
||||||
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
// It should keep the current page path, i.e "reference/api-reference/pets" when navigating to the new variant
|
||||||
await page.waitForURL(
|
await page.waitForURL(
|
||||||
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-va/2.0/reference/api-reference/pets?fallback=true',
|
'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-va/2.0/reference/api-reference/pets?fallback=true'
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'GitBook Site (Sections and Section Groups)',
|
||||||
|
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/sections/',
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
name: 'Site with sections and section groups',
|
||||||
|
url: '',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Section group dropdown',
|
||||||
|
url: '',
|
||||||
|
run: async (page) => {
|
||||||
|
await page.getByRole('button', { name: 'Test Section Group 1' }).hover();
|
||||||
|
await expect(page.getByRole('link', { name: /Section B/ })).toBeVisible();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Section group link',
|
||||||
|
url: '',
|
||||||
|
screenshot: false,
|
||||||
|
run: async (page) => {
|
||||||
|
const sectionGroupDropdown = await page.getByText('Test Section Group 1');
|
||||||
|
await sectionGroupDropdown.hover();
|
||||||
|
await page.getByText('Section B').click();
|
||||||
|
await page.waitForURL(
|
||||||
|
'https://gitbook-open-e2e-sites.gitbook.io/sections/sections-4'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -331,7 +362,7 @@ const testCases: TestsCase[] = [
|
|||||||
for (const p of document.querySelectorAll('p')) {
|
for (const p of document.querySelectorAll('p')) {
|
||||||
if (
|
if (
|
||||||
p.textContent?.includes(
|
p.textContent?.includes(
|
||||||
'This image has intrinsic 400px width, but renders as 300px:',
|
'This image has intrinsic 400px width, but renders as 300px:'
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
p.style.color = 'transparent';
|
p.style.color = 'transparent';
|
||||||
@@ -413,7 +444,7 @@ const testCases: TestsCase[] = [
|
|||||||
await page.waitForFunction(() => {
|
await page.waitForFunction(() => {
|
||||||
const fonts = Array.from(document.fonts.values());
|
const fonts = Array.from(document.fonts.values());
|
||||||
const mjxFonts = fonts.filter(
|
const mjxFonts = fonts.filter(
|
||||||
(font) => font.family === 'MJXZERO' || font.family === 'MJXTEX',
|
(font) => font.family === 'MJXZERO' || font.family === 'MJXTEX'
|
||||||
);
|
);
|
||||||
return (
|
return (
|
||||||
mjxFonts.length === 2 &&
|
mjxFonts.length === 2 &&
|
||||||
@@ -505,17 +536,15 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: `With duotone icons - Theme mode ${themeMode}`,
|
name: `With duotone icons - Theme mode ${themeMode}`,
|
||||||
url:
|
url: `page-options/page-with-icon${getCustomizationURL({
|
||||||
'page-options/page-with-icon' +
|
styling: {
|
||||||
getCustomizationURL({
|
icons: CustomizationIconsStyle.Duotone,
|
||||||
styling: {
|
},
|
||||||
icons: CustomizationIconsStyle.Duotone,
|
themes: {
|
||||||
},
|
default: themeMode,
|
||||||
themes: {
|
toggeable: false,
|
||||||
default: themeMode,
|
},
|
||||||
toggeable: false,
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -643,7 +672,7 @@ const testCases: TestsCase[] = [
|
|||||||
const sharedSpaceLink = page.locator('a.underline');
|
const sharedSpaceLink = page.locator('a.underline');
|
||||||
await sharedSpaceLink.click();
|
await sharedSpaceLink.click();
|
||||||
await expect(
|
await expect(
|
||||||
page.getByRole('heading', { level: 1, name: 'shared' }),
|
page.getByRole('heading', { level: 1, name: 'shared' })
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
const url = page.url();
|
const url = page.url();
|
||||||
expect(url.includes('shared-space-uno')).toBeTruthy(); // same uno site
|
expect(url.includes('shared-space-uno')).toBeTruthy(); // same uno site
|
||||||
@@ -663,7 +692,7 @@ const testCases: TestsCase[] = [
|
|||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await page.locator('a.underline').click();
|
await page.locator('a.underline').click();
|
||||||
await expect(
|
await expect(
|
||||||
page.getByRole('heading', { level: 1, name: 'shared' }),
|
page.getByRole('heading', { level: 1, name: 'shared' })
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
const url = page.url();
|
const url = page.url();
|
||||||
expect(url.includes('shared-space-dos')).toBeTruthy(); // same dos site
|
expect(url.includes('shared-space-dos')).toBeTruthy(); // same dos site
|
||||||
@@ -700,7 +729,7 @@ const testCases: TestsCase[] = [
|
|||||||
url: 'invalid/',
|
url: 'invalid/',
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(
|
await expect(
|
||||||
page.getByText('Authentication missing to access this content'),
|
page.getByText('Authentication missing to access this content')
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
},
|
},
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
@@ -709,7 +738,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Visitor Auth - Space',
|
name: 'Visitor Auth - Space',
|
||||||
baseUrl: `https://gitbook.gitbook.io/gbo-va-space/`,
|
baseUrl: 'https://gitbook.gitbook.io/gbo-va-space/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'First',
|
name: 'First',
|
||||||
@@ -722,13 +751,13 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `first?jwt_token=${token}`;
|
return `first?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(
|
await expect(
|
||||||
page.getByRole('heading', { level: 1, name: 'first' }),
|
page.getByRole('heading', { level: 1, name: 'first' })
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
},
|
},
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
@@ -744,13 +773,13 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `second?jwt_token=${token}`;
|
return `second?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(
|
await expect(
|
||||||
page.getByRole('heading', { level: 1, name: 'second' }),
|
page.getByRole('heading', { level: 1, name: 'second' })
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
},
|
},
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
@@ -759,7 +788,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Visitor Auth - Collection',
|
name: 'Visitor Auth - Collection',
|
||||||
baseUrl: `https://gitbook.gitbook.io/gbo-va-collection/`,
|
baseUrl: 'https://gitbook.gitbook.io/gbo-va-collection/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'Root',
|
name: 'Root',
|
||||||
@@ -772,7 +801,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -789,7 +818,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `spacea?jwt_token=${token}`;
|
return `spacea?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -806,7 +835,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `spaceb?jwt_token=${token}`;
|
return `spaceb?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -823,7 +852,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `spacec?jwt_token=${token}`;
|
return `spacec?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -833,7 +862,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Visitor Auth - Space (custom domain)',
|
name: 'Visitor Auth - Space (custom domain)',
|
||||||
baseUrl: `https://test.gitbook.community/`,
|
baseUrl: 'https://test.gitbook.community/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'Root',
|
name: 'Root',
|
||||||
@@ -846,7 +875,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -863,13 +892,13 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `first?jwt_token=${token}`;
|
return `first?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(
|
await expect(
|
||||||
page.getByRole('heading', { level: 1, name: 'first' }),
|
page.getByRole('heading', { level: 1, name: 'first' })
|
||||||
).toBeVisible();
|
).toBeVisible();
|
||||||
},
|
},
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
@@ -885,7 +914,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `custom-page?jwt_token=${token}`;
|
return `custom-page?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -902,7 +931,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `custom-page/inner-page?jwt_token=${token}`;
|
return `custom-page/inner-page?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -912,7 +941,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Visitor Auth - Site (redirects to fallback/auth URL)',
|
name: 'Visitor Auth - Site (redirects to fallback/auth URL)',
|
||||||
baseUrl: `https://gitbook-open-e2e-sites.gitbook.io/va-site-redirects-fallback/`,
|
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/va-site-redirects-fallback/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'Redirect to fallback on invalid token pulled from cookie',
|
name: 'Redirect to fallback on invalid token pulled from cookie',
|
||||||
@@ -927,7 +956,7 @@ const testCases: TestsCase[] = [
|
|||||||
'invalidKey',
|
'invalidKey',
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -952,13 +981,13 @@ const testCases: TestsCase[] = [
|
|||||||
'invalidKey',
|
'invalidKey',
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
await expect(page.locator('pre')).toContainText(
|
await expect(page.locator('pre')).toContainText(
|
||||||
'Error while validating the JWT token. Reason: The token signature is invalid.',
|
'Error while validating the JWT token. Reason: The token signature is invalid.'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -985,7 +1014,7 @@ const testCases: TestsCase[] = [
|
|||||||
baseUrl: 'https://gitbook.gitbook.io/test-gitbook-open/',
|
baseUrl: 'https://gitbook.gitbook.io/test-gitbook-open/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: `Index by default`,
|
name: 'Index by default',
|
||||||
url: '?x-gitbook-search-indexation=true',
|
url: '?x-gitbook-search-indexation=true',
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
run: async (page) => {
|
run: async (page) => {
|
||||||
@@ -1033,7 +1062,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Adaptive Content - VA',
|
name: 'Adaptive Content - VA',
|
||||||
baseUrl: `https://gitbook-open-e2e-sites.gitbook.io/adaptive-content-va/`,
|
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/adaptive-content-va/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'isAlphaUser',
|
name: 'isAlphaUser',
|
||||||
@@ -1047,7 +1076,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -1074,7 +1103,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -1102,7 +1131,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return `?jwt_token=${token}`;
|
return `?jwt_token=${token}`;
|
||||||
})(),
|
})(),
|
||||||
@@ -1121,7 +1150,7 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Adaptive Content - Public',
|
name: 'Adaptive Content - Public',
|
||||||
baseUrl: `https://gitbook-open-e2e-sites.gitbook.io/adaptive-content-public/`,
|
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/adaptive-content-public/',
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
name: 'No custom cookie',
|
name: 'No custom cookie',
|
||||||
@@ -1154,7 +1183,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -1193,7 +1222,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -1233,7 +1262,7 @@ const testCases: TestsCase[] = [
|
|||||||
privateKey,
|
privateKey,
|
||||||
{
|
{
|
||||||
expiresIn: '24h',
|
expiresIn: '24h',
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -1274,25 +1303,21 @@ const testCases: TestsCase[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Table with straight corners',
|
name: 'Table with straight corners',
|
||||||
url:
|
url: `blocks/tables${getCustomizationURL({
|
||||||
'blocks/tables' +
|
styling: {
|
||||||
getCustomizationURL({
|
corners: CustomizationCorners.Straight,
|
||||||
styling: {
|
},
|
||||||
corners: CustomizationCorners.Straight,
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
fullPage: true,
|
fullPage: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Table with primary color',
|
name: 'Table with primary color',
|
||||||
url:
|
url: `blocks/tables${getCustomizationURL({
|
||||||
'blocks/tables' +
|
styling: {
|
||||||
getCustomizationURL({
|
tint: { color: { light: '#346DDB', dark: '#346DDB' } },
|
||||||
styling: {
|
},
|
||||||
tint: { color: { light: '#346DDB', dark: '#346DDB' } },
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
fullPage: true,
|
fullPage: true,
|
||||||
},
|
},
|
||||||
@@ -1300,46 +1325,40 @@ const testCases: TestsCase[] = [
|
|||||||
...allThemeModes.flatMap((theme) => [
|
...allThemeModes.flatMap((theme) => [
|
||||||
{
|
{
|
||||||
name: `Table in ${theme} mode`,
|
name: `Table in ${theme} mode`,
|
||||||
url:
|
url: `blocks/tables${getCustomizationURL({
|
||||||
'blocks/tables' +
|
themes: {
|
||||||
getCustomizationURL({
|
default: theme,
|
||||||
themes: {
|
toggeable: false,
|
||||||
default: theme,
|
},
|
||||||
toggeable: false,
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
fullPage: true,
|
fullPage: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: `Table with straight corners in ${theme} mode`,
|
name: `Table with straight corners in ${theme} mode`,
|
||||||
url:
|
url: `blocks/tables${getCustomizationURL({
|
||||||
'blocks/tables' +
|
styling: {
|
||||||
getCustomizationURL({
|
corners: CustomizationCorners.Straight,
|
||||||
styling: {
|
},
|
||||||
corners: CustomizationCorners.Straight,
|
themes: {
|
||||||
},
|
default: theme,
|
||||||
themes: {
|
toggeable: false,
|
||||||
default: theme,
|
},
|
||||||
toggeable: false,
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
fullPage: true,
|
fullPage: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: `Table with primary color in ${theme} mode`,
|
name: `Table with primary color in ${theme} mode`,
|
||||||
url:
|
url: `blocks/tables${getCustomizationURL({
|
||||||
'blocks/tables' +
|
styling: {
|
||||||
getCustomizationURL({
|
tint: { color: { light: '#346DDB', dark: '#346DDB' } },
|
||||||
styling: {
|
},
|
||||||
tint: { color: { light: '#346DDB', dark: '#346DDB' } },
|
themes: {
|
||||||
},
|
default: theme,
|
||||||
themes: {
|
toggeable: false,
|
||||||
default: theme,
|
},
|
||||||
toggeable: false,
|
})}`,
|
||||||
},
|
|
||||||
}),
|
|
||||||
run: waitForCookiesDialog,
|
run: waitForCookiesDialog,
|
||||||
fullPage: true,
|
fullPage: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,21 +3,22 @@ import {
|
|||||||
CustomizationBackground,
|
CustomizationBackground,
|
||||||
CustomizationCorners,
|
CustomizationCorners,
|
||||||
CustomizationFont,
|
CustomizationFont,
|
||||||
CustomizationHeaderItem,
|
type CustomizationHeaderItem,
|
||||||
CustomizationHeaderPreset,
|
CustomizationHeaderPreset,
|
||||||
CustomizationIconsStyle,
|
CustomizationIconsStyle,
|
||||||
|
CustomizationLinksStyle,
|
||||||
CustomizationLocale,
|
CustomizationLocale,
|
||||||
CustomizationSidebarBackgroundStyle,
|
CustomizationSidebarBackgroundStyle,
|
||||||
CustomizationSidebarListStyle,
|
CustomizationSidebarListStyle,
|
||||||
CustomizationTheme,
|
CustomizationTheme,
|
||||||
CustomizationThemedColor,
|
|
||||||
CustomizationThemeMode,
|
CustomizationThemeMode,
|
||||||
SiteCustomizationSettings,
|
type CustomizationThemedColor,
|
||||||
|
type SiteCustomizationSettings,
|
||||||
} from '@gitbook/api';
|
} from '@gitbook/api';
|
||||||
import { test, expect, Page, BrowserContext } from '@playwright/test';
|
import { type BrowserContext, type Page, expect, test } from '@playwright/test';
|
||||||
import deepMerge from 'deepmerge';
|
import deepMerge from 'deepmerge';
|
||||||
import rison from 'rison';
|
import rison from 'rison';
|
||||||
import { DeepPartial } from 'ts-essentials';
|
import type { DeepPartial } from 'ts-essentials';
|
||||||
|
|
||||||
import { getContentTestURL } from '../tests/utils';
|
import { getContentTestURL } from '../tests/utils';
|
||||||
|
|
||||||
@@ -150,9 +151,10 @@ export function runTestCases(testCases: TestsCase[]) {
|
|||||||
...cookie,
|
...cookie,
|
||||||
domain: new URL(url).host,
|
domain: new URL(url).host,
|
||||||
path: '/',
|
path: '/',
|
||||||
})),
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.goto(url);
|
await page.goto(url);
|
||||||
if (testEntry.run) {
|
if (testEntry.run) {
|
||||||
await testEntry.run(page);
|
await testEntry.run(page);
|
||||||
@@ -222,10 +224,15 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
|
|||||||
styling: {
|
styling: {
|
||||||
theme: newTheme,
|
theme: newTheme,
|
||||||
primaryColor: { light: '#346DDB', dark: '#346DDB' },
|
primaryColor: { light: '#346DDB', dark: '#346DDB' },
|
||||||
|
infoColor: { light: '#787878', dark: '#787878' },
|
||||||
|
warningColor: { light: '#FE9A00', dark: '#FE9A00' },
|
||||||
|
dangerColor: { light: '#FB2C36', dark: '#FB2C36' },
|
||||||
|
successColor: { light: '#00C950', dark: '#00C950' },
|
||||||
corners: CustomizationCorners.Rounded,
|
corners: CustomizationCorners.Rounded,
|
||||||
font: CustomizationFont.Inter,
|
font: CustomizationFont.Inter,
|
||||||
background: CustomizationBackground.Plain,
|
background: CustomizationBackground.Plain,
|
||||||
icons: CustomizationIconsStyle.Regular,
|
icons: CustomizationIconsStyle.Regular,
|
||||||
|
links: CustomizationLinksStyle.Default,
|
||||||
sidebar: {
|
sidebar: {
|
||||||
background: CustomizationSidebarBackgroundStyle.Default,
|
background: CustomizationSidebarBackgroundStyle.Default,
|
||||||
list: CustomizationSidebarListStyle.Default,
|
list: CustomizationSidebarListStyle.Default,
|
||||||
@@ -290,7 +297,7 @@ async function waitForIcons(page: Page) {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => resolve(img);
|
img.onload = () => resolve(img);
|
||||||
img.onerror = (error) => reject(new Error(`Failed to load image: ${src}`));
|
img.onerror = (_error) => reject(new Error(`Failed to load image: ${src}`));
|
||||||
img.src = src;
|
img.src = src;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -306,7 +313,7 @@ async function waitForIcons(page: Page) {
|
|||||||
throw new Error('No mask-image');
|
throw new Error('No mask-image');
|
||||||
}
|
}
|
||||||
await loadImage(url);
|
await loadImage(url);
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -321,7 +328,7 @@ async function waitForTOCScrolling(page: Page) {
|
|||||||
await expect(toc).toBeVisible();
|
await expect(toc).toBeVisible();
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
const tocScrollContainer = document.querySelector(
|
const tocScrollContainer = document.querySelector(
|
||||||
'[data-testid="table-of-contents"] [data-testid="toc-scroll-container"]',
|
'[data-testid="table-of-contents"] [data-testid="toc-scroll-container"]'
|
||||||
);
|
);
|
||||||
if (!tocScrollContainer) {
|
if (!tocScrollContainer) {
|
||||||
throw new Error('TOC scroll container not found');
|
throw new Error('TOC scroll container not found');
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ module.exports = withSentryConfig(
|
|||||||
__RRWEB_EXCLUDE_IFRAME__: true,
|
__RRWEB_EXCLUDE_IFRAME__: true,
|
||||||
__RRWEB_EXCLUDE_SHADOW_DOM__: true,
|
__RRWEB_EXCLUDE_SHADOW_DOM__: true,
|
||||||
__SENTRY_EXCLUDE_REPLAY_WORKER__: true,
|
__SENTRY_EXCLUDE_REPLAY_WORKER__: true,
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,5 +79,5 @@ module.exports = withSentryConfig(
|
|||||||
// Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
|
// Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
|
||||||
tunnelRoute: '/~gitbook/monitoring',
|
tunnelRoute: '/~gitbook/monitoring',
|
||||||
disableLogger: true,
|
disableLogger: true,
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "gitbook",
|
"name": "gitbook",
|
||||||
"version": "0.6.1",
|
"version": "0.6.4",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "env-cmd --silent -f ../../.env.local next dev",
|
"dev": "env-cmd --silent -f ../../.env.local next dev",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"build:cloudflare": "next-on-pages --custom-entrypoint=./src/cloudflare-entrypoint.ts",
|
"build:cloudflare": "next-on-pages --custom-entrypoint=./src/cloudflare-entrypoint.ts",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "next lint",
|
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"e2e": "playwright test e2e/internal.spec.ts",
|
"e2e": "playwright test e2e/internal.spec.ts",
|
||||||
"e2e-customers": "playwright test e2e/customers.spec.ts",
|
"e2e-customers": "playwright test e2e/customers.spec.ts",
|
||||||
@@ -17,8 +16,9 @@
|
|||||||
"clean": "rm -rf ./.next && rm -rf ./public/~gitbook/static/icons && rm -rf ./public/~gitbook/static/math"
|
"clean": "rm -rf ./.next && rm -rf ./public/~gitbook/static/icons && rm -rf ./public/~gitbook/static/math"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@gitbook/api": "^0.93.0",
|
"@gitbook/api": "0.96.1",
|
||||||
"@gitbook/cache-do": "workspace:*",
|
"@gitbook/cache-do": "workspace:*",
|
||||||
|
"@gitbook/colors": "workspace:*",
|
||||||
"@gitbook/emoji-codepoints": "workspace:*",
|
"@gitbook/emoji-codepoints": "workspace:*",
|
||||||
"@gitbook/icons": "workspace:*",
|
"@gitbook/icons": "workspace:*",
|
||||||
"@gitbook/openapi-parser": "workspace:*",
|
"@gitbook/openapi-parser": "workspace:*",
|
||||||
@@ -37,7 +37,6 @@
|
|||||||
"assert-never": "^1.2.1",
|
"assert-never": "^1.2.1",
|
||||||
"bun-types": "^1.1.20",
|
"bun-types": "^1.1.20",
|
||||||
"classnames": "^2.5.1",
|
"classnames": "^2.5.1",
|
||||||
"content-security-policy-merger": "^1.0.0",
|
|
||||||
"framer-motion": "^10.16.14",
|
"framer-motion": "^10.16.14",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"jsontoxml": "^1.0.1",
|
"jsontoxml": "^1.0.1",
|
||||||
@@ -45,6 +44,8 @@
|
|||||||
"mathjax": "^3.2.2",
|
"mathjax": "^3.2.2",
|
||||||
"mdast-util-to-markdown": "^2.1.2",
|
"mdast-util-to-markdown": "^2.1.2",
|
||||||
"memoizee": "^0.4.15",
|
"memoizee": "^0.4.15",
|
||||||
|
"micromark": "^4.0.1",
|
||||||
|
"micromark-extension-gfm": "^3.0.0",
|
||||||
"next": "14.2.23",
|
"next": "14.2.23",
|
||||||
"next-themes": "^0.2.1",
|
"next-themes": "^0.2.1",
|
||||||
"nuqs": "^2.2.3",
|
"nuqs": "^2.2.3",
|
||||||
@@ -55,17 +56,11 @@
|
|||||||
"react": "18.3.1",
|
"react": "18.3.1",
|
||||||
"react-dom": "18.3.1",
|
"react-dom": "18.3.1",
|
||||||
"react-hotkeys-hook": "^4.4.1",
|
"react-hotkeys-hook": "^4.4.1",
|
||||||
"rehype-sanitize": "^6.0.0",
|
|
||||||
"rehype-stringify": "^10.0.0",
|
|
||||||
"remark-gfm": "^4.0.0",
|
|
||||||
"remark-parse": "^11.0.0",
|
|
||||||
"remark-rehype": "^11.1.0",
|
|
||||||
"rison": "^0.1.1",
|
"rison": "^0.1.1",
|
||||||
"server-only": "^0.0.1",
|
"server-only": "^0.0.1",
|
||||||
"shiki": "^1.27.2",
|
"shiki": "^1.27.2",
|
||||||
"tailwind-merge": "^2.2.0",
|
"tailwind-merge": "^2.2.0",
|
||||||
"tailwind-shades": "^1.1.2",
|
"tailwind-shades": "^1.1.2",
|
||||||
"unified": "^11.0.4",
|
|
||||||
"url-join": "^5.0.0",
|
"url-join": "^5.0.0",
|
||||||
"usehooks-ts": "^3.1.0"
|
"usehooks-ts": "^3.1.0"
|
||||||
},
|
},
|
||||||
@@ -89,9 +84,6 @@
|
|||||||
"autoprefixer": "^10",
|
"autoprefixer": "^10",
|
||||||
"deepmerge": "^4.3.1",
|
"deepmerge": "^4.3.1",
|
||||||
"env-cmd": "^10.1.0",
|
"env-cmd": "^10.1.0",
|
||||||
"eslint": "^8",
|
|
||||||
"eslint-config-next": "^14.2.5",
|
|
||||||
"eslint-plugin-import": "^2.29.1",
|
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"postcss": "^8",
|
"postcss": "^8",
|
||||||
"psi": "^4.1.0",
|
"psi": "^4.1.0",
|
||||||
|
|||||||
@@ -1,115 +1,9 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
import {
|
import { serveResizedImage } from '@/routes/image';
|
||||||
CURRENT_SIGNATURE_VERSION,
|
|
||||||
isSignatureVersion,
|
|
||||||
SignatureVersion,
|
|
||||||
verifyImageSignature,
|
|
||||||
} from '@/lib/image-signatures';
|
|
||||||
import { resizeImage, CloudflareImageOptions, checkIsSizableImageURL } from '@/lib/images';
|
|
||||||
import { parseImageAPIURL } from '@/lib/urls';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
/**
|
|
||||||
* Only on Cloudflare Workers:
|
|
||||||
*
|
|
||||||
* Fetch and resize an image.
|
|
||||||
*/
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
let urlParam = request.nextUrl.searchParams.get('url');
|
return serveResizedImage(request);
|
||||||
const signature = request.nextUrl.searchParams.get('sign');
|
|
||||||
|
|
||||||
if (!urlParam || !signature) {
|
|
||||||
return new Response('Missing url/sign parameters', { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const signatureVersion = parseSignatureVersion(request.nextUrl.searchParams.get('sv'));
|
|
||||||
if (!signatureVersion) {
|
|
||||||
return new Response(`Invalid sv parameter`, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = parseImageAPIURL(urlParam);
|
|
||||||
|
|
||||||
// Check again if the image can be sized, even though we checked when rendering the Image component
|
|
||||||
// Otherwise, it's possible to pass just any link to this endpoint and trigger HTML injection on the domain
|
|
||||||
// Also prevent infinite redirects.
|
|
||||||
if (!checkIsSizableImageURL(url)) {
|
|
||||||
return new Response('Invalid url parameter', { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the signature
|
|
||||||
const verified = await verifyImageSignature(url, { signature, version: signatureVersion });
|
|
||||||
if (!verified) {
|
|
||||||
return new Response(`Invalid signature "${signature ?? ''}" for "${url}"`, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (signatureVersion !== CURRENT_SIGNATURE_VERSION) {
|
|
||||||
return NextResponse.redirect(url, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cloudflare-specific options are in the cf object.
|
|
||||||
const options: CloudflareImageOptions = {
|
|
||||||
fit: 'scale-down',
|
|
||||||
format: 'jpeg',
|
|
||||||
quality: 100,
|
|
||||||
};
|
|
||||||
|
|
||||||
const width = request.nextUrl.searchParams.get('width');
|
|
||||||
if (width) {
|
|
||||||
options.width = Number(width);
|
|
||||||
}
|
|
||||||
|
|
||||||
const height = request.nextUrl.searchParams.get('height');
|
|
||||||
if (height) {
|
|
||||||
options.height = Number(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
const dpr = request.nextUrl.searchParams.get('dpr');
|
|
||||||
if (dpr) {
|
|
||||||
options.dpr = Number(dpr);
|
|
||||||
}
|
|
||||||
|
|
||||||
const quality = request.nextUrl.searchParams.get('quality');
|
|
||||||
if (quality) {
|
|
||||||
options.quality = Number(quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the Accept header to handle content negotiation
|
|
||||||
const accept = request.headers.get('accept');
|
|
||||||
if (accept && /image\/avif/.test(accept)) {
|
|
||||||
options.format = 'avif';
|
|
||||||
} else if (accept && /image\/webp/.test(accept)) {
|
|
||||||
options.format = 'webp';
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await resizeImage(url, options);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to resize image');
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (error) {
|
|
||||||
// Redirect to the original image if resizing fails
|
|
||||||
return NextResponse.redirect(url, 302);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse the image signature version from a query param. Returns null if the version is invalid.
|
|
||||||
*/
|
|
||||||
function parseSignatureVersion(input: string | null): SignatureVersion | null {
|
|
||||||
// Before introducing the sv parameter, all signatures were generated with version 0.
|
|
||||||
if (!input) {
|
|
||||||
return '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the query param explicitly asks for a signature version.
|
|
||||||
if (isSignatureVersion(input)) {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise the version is invalid.
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { type NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
import { revalidateTags } from '@/lib/cache';
|
import { revalidateTags } from '@/lib/cache';
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ export async function POST(req: NextRequest) {
|
|||||||
{
|
{
|
||||||
error: 'tags must be an array',
|
error: 'tags must be an array',
|
||||||
},
|
},
|
||||||
{ status: 400 },
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { captureException } from '@sentry/nextjs';
|
import { captureException } from '@sentry/nextjs';
|
||||||
import Error from 'next/error';
|
import NextError from 'next/error';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
export default function GlobalError({ error }: { error: Error }) {
|
export default function GlobalError({ error }: { error: Error }) {
|
||||||
@@ -10,9 +10,9 @@ export default function GlobalError({ error }: { error: Error }) {
|
|||||||
}, [error]);
|
}, [error]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html>
|
<html lang="en">
|
||||||
<body>
|
<body>
|
||||||
<Error statusCode={undefined as any} />
|
<NextError statusCode={undefined as any} />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,28 +1,8 @@
|
|||||||
import { SkeletonHeading, SkeletonParagraph } from '@/components/primitives';
|
import { SitePageSkeleton } from '@/components/SitePage';
|
||||||
import { tcls } from '@/lib/tailwind';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Placeholder when loading a page.
|
* Placeholder when loading a page.
|
||||||
*/
|
*/
|
||||||
export default function PageSkeleton() {
|
export default function PageSkeleton() {
|
||||||
return (
|
return <SitePageSkeleton />;
|
||||||
<div
|
|
||||||
className={tcls(
|
|
||||||
'flex',
|
|
||||||
'flex-row',
|
|
||||||
'flex-1',
|
|
||||||
'relative',
|
|
||||||
'py-8',
|
|
||||||
'lg:px-16',
|
|
||||||
'xl:mr-56',
|
|
||||||
'items-center',
|
|
||||||
'lg:items-start',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className={tcls('flex-1', 'max-w-3xl', 'mx-auto', 'page-full-width:mx-0')}>
|
|
||||||
<SkeletonHeading style={tcls('mb-8')} />
|
|
||||||
<SkeletonParagraph style={tcls('mb-4')} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,5 @@
|
|||||||
import { TrackPageViewEvent } from '@/components/Insights';
|
import { SitePageNotFound } from '@/components/SitePage';
|
||||||
import { getSpaceLanguage, t } from '@/intl/server';
|
|
||||||
import { getSiteData, getSpaceContentData } from '@/lib/api';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
|
||||||
import { tcls } from '@/lib/tailwind';
|
|
||||||
|
|
||||||
export default async function NotFound() {
|
export default async function NotFound() {
|
||||||
const pointer = await getSiteContentPointer();
|
return <SitePageNotFound />;
|
||||||
const [{ space }, { customization }] = await Promise.all([
|
|
||||||
getSpaceContentData(pointer, pointer.siteShareKey),
|
|
||||||
getSiteData(pointer),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const language = getSpaceLanguage(customization);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={tcls(
|
|
||||||
'flex-1',
|
|
||||||
'flex',
|
|
||||||
'flex-row',
|
|
||||||
'items-center',
|
|
||||||
'justify-center',
|
|
||||||
'py-9',
|
|
||||||
'min-h-[calc(100vh-64px)] lg:min-h-fit',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className={tcls('max-w-80')}>
|
|
||||||
<h2 className={tcls('text-2xl', 'font-semibold', 'mb-2')}>
|
|
||||||
{t(language, 'notfound_title')}
|
|
||||||
</h2>
|
|
||||||
<p className={tcls('text-base', 'mb-4')}>{t(language, 'notfound')}</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Track the page not found as a page view */}
|
|
||||||
<TrackPageViewEvent pageId={null} revisionId={space.revision} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,203 +1,55 @@
|
|||||||
import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api';
|
import type { Metadata, Viewport } from 'next';
|
||||||
import { Metadata, Viewport } from 'next';
|
|
||||||
import { notFound, redirect } from 'next/navigation';
|
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { PageAside } from '@/components/PageAside';
|
import {
|
||||||
import { PageBody, PageCover } from '@/components/PageBody';
|
type PagePathParams,
|
||||||
import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links';
|
SitePage,
|
||||||
import { getPagePath, resolveFirstDocument } from '@/lib/pages';
|
generateSitePageMetadata,
|
||||||
import { ContentRefContext } from '@/lib/references';
|
generateSitePageViewport,
|
||||||
import { isSpaceIndexable, isPageIndexable } from '@/lib/seo';
|
} from '@/components/SitePage';
|
||||||
import { getContentTitle } from '@/lib/utils';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import { PageClientLayout } from './PageClientLayout';
|
|
||||||
import { PagePathParams, fetchPageData, getPathnameParam, normalizePathname } from '../../fetch';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
export const dynamic = 'force-dynamic';
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<PagePathParams>;
|
||||||
|
searchParams: Promise<{ fallback?: string }>;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch and render a page.
|
* Fetch and render a page.
|
||||||
*/
|
*/
|
||||||
export default async function Page(props: {
|
export default async function Page(props: PageProps) {
|
||||||
params: Promise<PagePathParams>;
|
const sitePageProps = await getSitePageProps(props);
|
||||||
searchParams: Promise<{ fallback?: string }>;
|
return <SitePage {...sitePageProps} />;
|
||||||
}) {
|
}
|
||||||
|
|
||||||
|
export async function generateViewport(): Promise<Viewport> {
|
||||||
|
const pointer = await getSiteContentPointer();
|
||||||
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
|
return generateSitePageViewport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateMetadata(props: PageProps): Promise<Metadata> {
|
||||||
|
const sitePageProps = await getSitePageProps(props);
|
||||||
|
return generateSitePageMetadata(sitePageProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getSitePageProps(props: PageProps) {
|
||||||
const { params: rawParams, searchParams: rawSearchParams } = props;
|
const { params: rawParams, searchParams: rawSearchParams } = props;
|
||||||
|
|
||||||
const params = await rawParams;
|
const params = await rawParams;
|
||||||
const searchParams = await rawSearchParams;
|
const searchParams = await rawSearchParams;
|
||||||
|
|
||||||
const {
|
const pointer = await getSiteContentPointer();
|
||||||
content: contentPointer,
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
contentTarget,
|
|
||||||
sections,
|
return {
|
||||||
space,
|
context,
|
||||||
site,
|
pageParams: params,
|
||||||
customization,
|
|
||||||
pages,
|
|
||||||
page,
|
|
||||||
ancestors,
|
|
||||||
document,
|
|
||||||
} = await getPageDataWithFallback({
|
|
||||||
pagePathParams: params,
|
|
||||||
searchParams,
|
|
||||||
redirectOnFallback: true,
|
redirectOnFallback: true,
|
||||||
});
|
fallback: !!searchParams.fallback,
|
||||||
|
|
||||||
const linksContext: PageHrefContext = {};
|
|
||||||
const rawPathname = getPathnameParam(params);
|
|
||||||
if (!page) {
|
|
||||||
const pathname = normalizePathname(rawPathname);
|
|
||||||
if (pathname !== rawPathname) {
|
|
||||||
// If the pathname was not normalized, redirect to the normalized version
|
|
||||||
// before trying to resolve the page again
|
|
||||||
redirect(await getAbsoluteHref(pathname));
|
|
||||||
} else {
|
|
||||||
notFound();
|
|
||||||
}
|
|
||||||
} else if (getPagePath(pages, page) !== rawPathname) {
|
|
||||||
redirect(await getPageHref(pages, page, linksContext));
|
|
||||||
}
|
|
||||||
|
|
||||||
const withTopHeader = customization.header.preset !== CustomizationHeaderPreset.None;
|
|
||||||
const withFullPageCover = !!(
|
|
||||||
page.cover &&
|
|
||||||
page.layout.cover &&
|
|
||||||
page.layout.coverSize === 'full'
|
|
||||||
);
|
|
||||||
const withPageFeedback = customization.feedback.enabled;
|
|
||||||
|
|
||||||
const contentRefContext: ContentRefContext = {
|
|
||||||
siteContext: contentPointer,
|
|
||||||
space,
|
|
||||||
revisionId: contentTarget.revisionId,
|
|
||||||
pages,
|
|
||||||
page,
|
|
||||||
};
|
|
||||||
|
|
||||||
const withSections = Boolean(sections && sections.list.length > 0);
|
|
||||||
const headerOffset = { sectionsHeader: withSections, topHeader: withTopHeader };
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{withFullPageCover && page.cover ? (
|
|
||||||
<PageCover as="full" page={page} cover={page.cover} context={contentRefContext} />
|
|
||||||
) : null}
|
|
||||||
{/* We use a flex row reverse to render the aside first because the page is streamed. */}
|
|
||||||
<div className="flex flex-row-reverse justify-end grow">
|
|
||||||
<PageAside
|
|
||||||
space={space}
|
|
||||||
site={site}
|
|
||||||
customization={customization}
|
|
||||||
page={page}
|
|
||||||
document={document}
|
|
||||||
withHeaderOffset={headerOffset}
|
|
||||||
withFullPageCover={withFullPageCover}
|
|
||||||
withPageFeedback={withPageFeedback}
|
|
||||||
context={contentRefContext}
|
|
||||||
/>
|
|
||||||
<PageBody
|
|
||||||
space={space}
|
|
||||||
pointer={contentPointer}
|
|
||||||
contentTarget={contentTarget}
|
|
||||||
customization={customization}
|
|
||||||
context={contentRefContext}
|
|
||||||
page={page}
|
|
||||||
ancestors={ancestors}
|
|
||||||
document={document}
|
|
||||||
withPageFeedback={withPageFeedback}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<React.Suspense fallback={null}>
|
|
||||||
<PageClientLayout withSections={withSections} />
|
|
||||||
</React.Suspense>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function generateViewport({
|
|
||||||
params,
|
|
||||||
}: {
|
|
||||||
params: Promise<PagePathParams>;
|
|
||||||
}): Promise<Viewport> {
|
|
||||||
const { customization } = await fetchPageData(await params);
|
|
||||||
return {
|
|
||||||
colorScheme: customization.themes.toggeable
|
|
||||||
? customization.themes.default === CustomizationThemeMode.Dark
|
|
||||||
? 'dark light'
|
|
||||||
: 'light dark'
|
|
||||||
: customization.themes.default,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function generateMetadata({
|
|
||||||
params,
|
|
||||||
searchParams,
|
|
||||||
}: {
|
|
||||||
params: Promise<PagePathParams>;
|
|
||||||
searchParams: Promise<{ fallback?: string }>;
|
|
||||||
}): Promise<Metadata> {
|
|
||||||
const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({
|
|
||||||
pagePathParams: await params,
|
|
||||||
searchParams: await searchParams,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!page) {
|
|
||||||
notFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
title: [page.title, getContentTitle(space, customization, site ?? null)]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(' | '),
|
|
||||||
description: page.description ?? '',
|
|
||||||
alternates: {
|
|
||||||
// Trim trailing slashes in canonical URL to match the redirect behavior
|
|
||||||
canonical: (await getAbsoluteHref(getPagePath(pages, page), true)).replace(/\/+$/, ''),
|
|
||||||
},
|
|
||||||
openGraph: {
|
|
||||||
images: [
|
|
||||||
customization.socialPreview.url ??
|
|
||||||
(await getAbsoluteHref(`~gitbook/ogimage/${page.id}`, true)),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
robots:
|
|
||||||
(await isSpaceIndexable({ space, site: site ?? null })) &&
|
|
||||||
isPageIndexable(ancestors, page)
|
|
||||||
? 'index, follow'
|
|
||||||
: 'noindex, nofollow',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches the page data matching the requested pathname and fallback to root page when page is not found.
|
|
||||||
*/
|
|
||||||
async function getPageDataWithFallback(args: {
|
|
||||||
pagePathParams: PagePathParams;
|
|
||||||
searchParams: { fallback?: string };
|
|
||||||
redirectOnFallback?: boolean;
|
|
||||||
}) {
|
|
||||||
const { pagePathParams, searchParams, redirectOnFallback = false } = args;
|
|
||||||
|
|
||||||
const { pages, page: targetPage, ...otherPageData } = await fetchPageData(pagePathParams);
|
|
||||||
|
|
||||||
let page = targetPage;
|
|
||||||
const canFallback = !!searchParams.fallback;
|
|
||||||
if (!page && canFallback) {
|
|
||||||
const rootPage = resolveFirstDocument(pages, []);
|
|
||||||
|
|
||||||
if (redirectOnFallback && rootPage?.page) {
|
|
||||||
redirect(await getPageHref(pages, rootPage?.page));
|
|
||||||
}
|
|
||||||
|
|
||||||
page = rootPage?.page;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...otherPageData,
|
|
||||||
pages,
|
|
||||||
page,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,15 @@
|
|||||||
import { CustomizationThemeMode } from '@gitbook/api';
|
import { getThemeFromMiddleware } from '@v2/lib/middleware';
|
||||||
import { Metadata, Viewport } from 'next';
|
import type { Metadata, Viewport } from 'next';
|
||||||
import { headers } from 'next/headers';
|
import type React from 'react';
|
||||||
import { NuqsAdapter } from 'nuqs/adapters/next/app';
|
|
||||||
import React from 'react';
|
|
||||||
import * as ReactDOM from 'react-dom';
|
|
||||||
|
|
||||||
import { AdminToolbar } from '@/components/AdminToolbar';
|
import {
|
||||||
import { CookiesToast } from '@/components/Cookies';
|
SiteLayout,
|
||||||
import { LoadIntegrations } from '@/components/Integrations';
|
generateSiteLayoutMetadata,
|
||||||
import { SpaceLayout } from '@/components/SpaceLayout';
|
generateSiteLayoutViewport,
|
||||||
import { api } from '@/lib/api';
|
} from '@/components/SiteLayout';
|
||||||
import { assetsDomain } from '@/lib/assets';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { buildVersion } from '@/lib/build';
|
import { shouldTrackEvents } from '@/lib/tracking';
|
||||||
import { getContentSecurityPolicyNonce } from '@/lib/csp';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import { getAbsoluteHref, getBaseUrl } from '@/lib/links';
|
|
||||||
import { isSpaceIndexable } from '@/lib/seo';
|
|
||||||
import { getContentTitle } from '@/lib/utils';
|
|
||||||
|
|
||||||
import { ClientContexts } from './ClientContexts';
|
|
||||||
import { RocketLoaderDetector } from './RocketLoaderDetector';
|
|
||||||
import { fetchContentData } from '../fetch';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
export const dynamic = 'force-dynamic';
|
export const dynamic = 'force-dynamic';
|
||||||
@@ -30,133 +20,31 @@ export const dynamic = 'force-dynamic';
|
|||||||
export default async function ContentLayout(props: { children: React.ReactNode }) {
|
export default async function ContentLayout(props: { children: React.ReactNode }) {
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
|
|
||||||
const nonce = await getContentSecurityPolicyNonce();
|
const context = await fetchLayoutData();
|
||||||
const {
|
const queryStringTheme = await getThemeFromMiddleware();
|
||||||
content,
|
|
||||||
space,
|
|
||||||
contentTarget,
|
|
||||||
customization,
|
|
||||||
pages,
|
|
||||||
site,
|
|
||||||
spaces,
|
|
||||||
ancestors,
|
|
||||||
scripts,
|
|
||||||
sections,
|
|
||||||
} = await fetchContentData();
|
|
||||||
|
|
||||||
const apiCtx = await api();
|
|
||||||
ReactDOM.preconnect(apiCtx.client.endpoint);
|
|
||||||
if (assetsDomain) {
|
|
||||||
ReactDOM.preconnect(assetsDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
scripts.forEach(({ script }) => {
|
|
||||||
ReactDOM.preload(script, {
|
|
||||||
as: 'script',
|
|
||||||
nonce,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const queryStringTheme = await getQueryStringTheme();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NuqsAdapter>
|
<SiteLayout
|
||||||
<ClientContexts
|
context={context}
|
||||||
nonce={nonce}
|
forcedTheme={queryStringTheme}
|
||||||
forcedTheme={
|
withTracking={await shouldTrackEvents()}
|
||||||
queryStringTheme ??
|
>
|
||||||
(customization.themes.toggeable ? undefined : customization.themes.default)
|
{children}
|
||||||
}
|
</SiteLayout>
|
||||||
>
|
|
||||||
<SpaceLayout
|
|
||||||
space={space}
|
|
||||||
contentTarget={contentTarget}
|
|
||||||
site={site}
|
|
||||||
spaces={spaces}
|
|
||||||
sections={sections}
|
|
||||||
customization={customization}
|
|
||||||
pages={pages}
|
|
||||||
ancestors={ancestors}
|
|
||||||
content={content}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</SpaceLayout>
|
|
||||||
|
|
||||||
{scripts.length > 0 ? (
|
|
||||||
<>
|
|
||||||
<LoadIntegrations />
|
|
||||||
{scripts.map(({ script }) => (
|
|
||||||
<script key={script} async src={script} nonce={nonce} />
|
|
||||||
))}
|
|
||||||
</>
|
|
||||||
) : null}
|
|
||||||
|
|
||||||
{scripts.some((script) => script.cookies) || customization.privacyPolicy.url ? (
|
|
||||||
<React.Suspense fallback={null}>
|
|
||||||
<CookiesToast privacyPolicy={customization.privacyPolicy.url} />
|
|
||||||
</React.Suspense>
|
|
||||||
) : null}
|
|
||||||
|
|
||||||
<RocketLoaderDetector nonce={nonce} />
|
|
||||||
|
|
||||||
<AdminToolbar space={space} content={content} />
|
|
||||||
</ClientContexts>
|
|
||||||
</NuqsAdapter>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateViewport(): Promise<Viewport> {
|
export async function generateViewport(): Promise<Viewport> {
|
||||||
const { customization } = await fetchContentData();
|
const context = await fetchLayoutData();
|
||||||
return {
|
return generateSiteLayoutViewport(context);
|
||||||
colorScheme: customization.themes.toggeable
|
|
||||||
? customization.themes.default === CustomizationThemeMode.Dark
|
|
||||||
? 'dark light'
|
|
||||||
: 'light dark'
|
|
||||||
: customization.themes.default,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateMetadata(): Promise<Metadata> {
|
export async function generateMetadata(): Promise<Metadata> {
|
||||||
const { space, site, customization } = await fetchContentData();
|
const context = await fetchLayoutData();
|
||||||
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
|
return generateSiteLayoutMetadata(context);
|
||||||
|
|
||||||
return {
|
|
||||||
title: getContentTitle(space, customization, site),
|
|
||||||
generator: `GitBook (${buildVersion()})`,
|
|
||||||
metadataBase: new URL(await getBaseUrl()),
|
|
||||||
icons: {
|
|
||||||
icon: [
|
|
||||||
{
|
|
||||||
url:
|
|
||||||
customIcon?.light ??
|
|
||||||
(await getAbsoluteHref('~gitbook/icon?size=small&theme=light', true)),
|
|
||||||
type: 'image/png',
|
|
||||||
media: '(prefers-color-scheme: light)',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
url:
|
|
||||||
customIcon?.dark ??
|
|
||||||
(await getAbsoluteHref('~gitbook/icon?size=small&theme=dark', true)),
|
|
||||||
type: 'image/png',
|
|
||||||
media: '(prefers-color-scheme: dark)',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
robots: (await isSpaceIndexable({ space, site })) ? 'index, follow' : 'noindex, nofollow',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function fetchLayoutData() {
|
||||||
* For preview, the theme can be set via query string (?theme=light).
|
const pointer = await getSiteContentPointer();
|
||||||
*/
|
return fetchV1ContextForSitePointer(pointer);
|
||||||
async function getQueryStringTheme() {
|
|
||||||
const headersList = await headers();
|
|
||||||
const queryStringTheme = headersList.get('x-gitbook-theme');
|
|
||||||
if (!queryStringTheme) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return queryStringTheme === 'light'
|
|
||||||
? CustomizationThemeMode.Light
|
|
||||||
: CustomizationThemeMode.Dark;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,154 +1,14 @@
|
|||||||
import { SiteSection, SiteSpace, SiteStructure } from '@gitbook/api';
|
import type { NextRequest } from 'next/server';
|
||||||
import assertNever from 'assert-never';
|
|
||||||
import { ListItem, Paragraph, Root, RootContent } from 'mdast';
|
|
||||||
import { toMarkdown } from 'mdast-util-to-markdown';
|
|
||||||
import { NextRequest } from 'next/server';
|
|
||||||
|
|
||||||
import { getPublishedContentSite, getRevisionPages } from '@/lib/api';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { getAbsoluteHref } from '@/lib/links';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import { getPagePath } from '@/lib/pages';
|
import { serveLLMsTxt } from '@/routes/llms';
|
||||||
import { joinPath } from '@/lib/paths';
|
|
||||||
import { checkIsRootPointer, getSiteContentPointer } from '@/lib/pointer';
|
|
||||||
import { getIndexablePages } from '@/lib/sitemap';
|
|
||||||
import { getSiteStructureSections } from '@/lib/utils';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
/**
|
export async function GET(_req: NextRequest) {
|
||||||
* Generate a llms.txt file for the site.
|
|
||||||
*/
|
|
||||||
export async function GET(req: NextRequest) {
|
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
const { structure: siteStructure, site } = await getPublishedContentSite({
|
return serveLLMsTxt(context);
|
||||||
organizationId: pointer.organizationId,
|
|
||||||
siteId: pointer.siteId,
|
|
||||||
siteShareKey: pointer.siteShareKey,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!checkIsRootPointer(pointer, siteStructure)) {
|
|
||||||
return new Response('llms.txt is only served from the root of the site', { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const tree: Root = {
|
|
||||||
type: 'root',
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
type: 'heading',
|
|
||||||
depth: 1,
|
|
||||||
children: [{ type: 'text', value: site.title }],
|
|
||||||
},
|
|
||||||
...(await getNodesFromSiteStructure(siteStructure)),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Response(
|
|
||||||
toMarkdown(tree, {
|
|
||||||
bullet: '-',
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'text/plain; charset=utf-8',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get MDAST nodes from site structure.
|
|
||||||
*/
|
|
||||||
async function getNodesFromSiteStructure(siteStructure: SiteStructure): Promise<RootContent[]> {
|
|
||||||
switch (siteStructure.type) {
|
|
||||||
case 'sections':
|
|
||||||
return getNodesFromSections(getSiteStructureSections(siteStructure));
|
|
||||||
case 'siteSpaces':
|
|
||||||
return getNodesFromSiteSpaces(siteStructure.structure, { heading: true });
|
|
||||||
default:
|
|
||||||
assertNever(siteStructure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get MDAST nodes from site sections.
|
|
||||||
*/
|
|
||||||
async function getNodesFromSections(siteSections: SiteSection[]): Promise<RootContent[]> {
|
|
||||||
const all = await Promise.all(
|
|
||||||
siteSections.map(async (siteSection): Promise<RootContent[]> => {
|
|
||||||
const siteSpaceNodes = await getNodesFromSiteSpaces(siteSection.siteSpaces, {
|
|
||||||
heading: false,
|
|
||||||
});
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
type: 'heading',
|
|
||||||
depth: 2,
|
|
||||||
children: [{ type: 'text', value: siteSection.title }],
|
|
||||||
},
|
|
||||||
...siteSpaceNodes,
|
|
||||||
];
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return all.flat();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get MDAST nodes from site spaces.
|
|
||||||
*/
|
|
||||||
async function getNodesFromSiteSpaces(
|
|
||||||
siteSpaces: SiteSpace[],
|
|
||||||
options: {
|
|
||||||
/**
|
|
||||||
* Includes a heading for each site space.
|
|
||||||
*/
|
|
||||||
heading?: boolean;
|
|
||||||
},
|
|
||||||
): Promise<RootContent[]> {
|
|
||||||
const all = await Promise.all(
|
|
||||||
siteSpaces.map(async (siteSpace): Promise<RootContent[]> => {
|
|
||||||
const siteSpaceUrl = siteSpace.urls.published;
|
|
||||||
if (!siteSpaceUrl) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
const rootPages = await getRevisionPages(siteSpace.space.id, siteSpace.space.revision, {
|
|
||||||
metadata: false,
|
|
||||||
});
|
|
||||||
const pages = getIndexablePages(rootPages);
|
|
||||||
const listChildren = await Promise.all(
|
|
||||||
pages.map(async ({ page }): Promise<ListItem> => {
|
|
||||||
const url = await getAbsoluteHref(
|
|
||||||
joinPath(new URL(siteSpaceUrl).pathname, getPagePath(rootPages, page)),
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
const children: Paragraph['children'] = [
|
|
||||||
{
|
|
||||||
type: 'link',
|
|
||||||
url,
|
|
||||||
children: [{ type: 'text', value: page.title }],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
if (page.description) {
|
|
||||||
children.push({ type: 'text', value: `: ${page.description}` });
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: 'listItem',
|
|
||||||
children: [{ type: 'paragraph', children }],
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const nodes: RootContent[] = [];
|
|
||||||
if (options.heading) {
|
|
||||||
nodes.push({
|
|
||||||
type: 'heading',
|
|
||||||
depth: 2,
|
|
||||||
children: [{ type: 'text', value: siteSpace.title }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
nodes.push({
|
|
||||||
type: 'list',
|
|
||||||
spread: false,
|
|
||||||
children: listChildren,
|
|
||||||
});
|
|
||||||
return nodes;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return all.flat();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,14 @@
|
|||||||
import { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
import { getSpace, getSite } from '@/lib/api';
|
|
||||||
import { getAbsoluteHref } from '@/lib/links';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { isSpaceIndexable } from '@/lib/seo';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
|
import { serveRobotsTxt } from '@/routes/robots';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
/**
|
export async function GET(_request: NextRequest) {
|
||||||
* Generate a robots.txt for the current space.
|
|
||||||
*/
|
|
||||||
export async function GET(req: NextRequest) {
|
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
const [site, space] = await Promise.all([
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
getSite(pointer.organizationId, pointer.siteId),
|
|
||||||
getSpace(pointer.spaceId, pointer.siteShareKey),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const lines = [
|
return serveRobotsTxt(context);
|
||||||
`User-agent: *`,
|
|
||||||
'Disallow: /~gitbook/',
|
|
||||||
...((await isSpaceIndexable({ space, site }))
|
|
||||||
? [`Allow: /`, `Sitemap: ${await getAbsoluteHref(`/sitemap.xml`, true)}`]
|
|
||||||
: [`Disallow: /`]),
|
|
||||||
];
|
|
||||||
const content = lines.join('\n');
|
|
||||||
|
|
||||||
return new Response(content, {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'text/plain',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,66 +1,12 @@
|
|||||||
import jsontoxml from 'jsontoxml';
|
|
||||||
|
|
||||||
import { getRevisionPages, getSpace } from '@/lib/api';
|
|
||||||
import { getAbsoluteHref } from '@/lib/links';
|
|
||||||
import { getPagePath } from '@/lib/pages';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { getIndexablePages } from '@/lib/sitemap';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
|
import { servePagesSitemap } from '@/routes/sitemap';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a sitemap.xml for the current section / space.
|
|
||||||
*/
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
const revisionId =
|
return servePagesSitemap(context);
|
||||||
pointer.revisionId ?? (await getSpace(pointer.spaceId, pointer.siteShareKey)).revision;
|
|
||||||
|
|
||||||
const rootPages = await getRevisionPages(pointer.spaceId, revisionId, { metadata: false });
|
|
||||||
|
|
||||||
const pages = getIndexablePages(rootPages);
|
|
||||||
|
|
||||||
const urls = await Promise.all(
|
|
||||||
pages.map(async ({ page, depth }) => {
|
|
||||||
// Decay priority with depth
|
|
||||||
const priority = Math.pow(2, -0.25 * depth);
|
|
||||||
// Normalize to keep 2 decimals
|
|
||||||
const normalizedPriority = Math.floor(100 * priority) / 100;
|
|
||||||
const lastModified = page.updatedAt || page.createdAt;
|
|
||||||
|
|
||||||
const url: { loc: string; priority: number; lastmod?: string } = {
|
|
||||||
priority: normalizedPriority,
|
|
||||||
loc: await getAbsoluteHref(getPagePath(rootPages, page), true),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (lastModified) {
|
|
||||||
url.lastmod = new Date(lastModified).toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return { url };
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
const xml = jsontoxml(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
name: 'urlset',
|
|
||||||
children: urls,
|
|
||||||
attrs: {
|
|
||||||
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
{
|
|
||||||
xmlHeader: true,
|
|
||||||
prettyPrint: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return new Response(xml, {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/xml',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,98 +1,12 @@
|
|||||||
import { SiteSection, SiteSpace, SiteStructure } from '@gitbook/api';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import assertNever from 'assert-never';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import jsontoxml from 'jsontoxml';
|
import { serveRootSitemap } from '@/routes/sitemap';
|
||||||
|
|
||||||
import { getPublishedContentSite } from '@/lib/api';
|
|
||||||
import { joinPath } from '@/lib/paths';
|
|
||||||
import { checkIsRootPointer, getSiteContentPointer } from '@/lib/pointer';
|
|
||||||
import { filterOutNullable } from '@/lib/typescript';
|
|
||||||
import { getSiteStructureSections } from '@/lib/utils';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a root sitemap that point to all sitemap-pages.xml.
|
|
||||||
*/
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
const { structure: siteStructure } = await getPublishedContentSite({
|
return serveRootSitemap(context);
|
||||||
organizationId: pointer.organizationId,
|
|
||||||
siteId: pointer.siteId,
|
|
||||||
siteShareKey: pointer.siteShareKey,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!checkIsRootPointer(pointer, siteStructure)) {
|
|
||||||
return new Response('Root sitemap is only served from the root of the site', {
|
|
||||||
status: 404,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const urls = await getUrlsFromSiteStructure(siteStructure);
|
|
||||||
|
|
||||||
const xml = jsontoxml(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
name: 'sitemapindex',
|
|
||||||
children: urls.map((url) => ({
|
|
||||||
name: 'sitemap',
|
|
||||||
children: [{ name: 'loc', text: url }],
|
|
||||||
})),
|
|
||||||
attrs: {
|
|
||||||
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
{
|
|
||||||
xmlHeader: true,
|
|
||||||
prettyPrint: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return new Response(xml, {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/xml',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Sitemap URLs from site structure.
|
|
||||||
*/
|
|
||||||
async function getUrlsFromSiteStructure(siteStructure: SiteStructure): Promise<string[]> {
|
|
||||||
switch (siteStructure.type) {
|
|
||||||
case 'sections':
|
|
||||||
return getUrlsFromSiteSections(getSiteStructureSections(siteStructure));
|
|
||||||
case 'siteSpaces':
|
|
||||||
return getUrlsFromSiteSpaces(siteStructure.structure);
|
|
||||||
default:
|
|
||||||
assertNever(siteStructure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Sitemap URLs from site sections.
|
|
||||||
*/
|
|
||||||
async function getUrlsFromSiteSections(siteSections: SiteSection[]): Promise<string[]> {
|
|
||||||
const urls = await Promise.all(
|
|
||||||
siteSections.map(async (siteSection) => getUrlsFromSiteSpaces(siteSection.siteSpaces), []),
|
|
||||||
);
|
|
||||||
return urls.flat();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Sitemap URLs from site spaces.
|
|
||||||
*/
|
|
||||||
async function getUrlsFromSiteSpaces(siteSpaces: SiteSpace[]): Promise<string[]> {
|
|
||||||
const urls = await Promise.all(
|
|
||||||
siteSpaces.map(async (siteSpace) => {
|
|
||||||
if (!siteSpace.urls.published) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const url = new URL(siteSpace.urls.published);
|
|
||||||
url.pathname = joinPath(url.pathname, 'sitemap-pages.xml');
|
|
||||||
return url.toString();
|
|
||||||
}, []),
|
|
||||||
);
|
|
||||||
return urls.filter(filterOutNullable);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,99 +1,14 @@
|
|||||||
import { notFound } from 'next/navigation';
|
import type { NextRequest } from 'next/server';
|
||||||
import { ImageResponse } from 'next/og';
|
|
||||||
import { NextRequest } from 'next/server';
|
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { getSite, getSiteData, getSpace } from '@/lib/api';
|
|
||||||
import { getEmojiForCode } from '@/lib/emojis';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { tcls } from '@/lib/tailwind';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import { getContentTitle } from '@/lib/utils';
|
import { serveIcon } from '@/routes/icon';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
const SIZES = {
|
|
||||||
/** Size for a favicon */
|
|
||||||
small: {
|
|
||||||
width: 48,
|
|
||||||
height: 48,
|
|
||||||
textSize: 'text-[32px]',
|
|
||||||
boxStyle: 'rounded-[8px]',
|
|
||||||
},
|
|
||||||
/** Size for display as an app icon or in the header */
|
|
||||||
medium: {
|
|
||||||
width: 256,
|
|
||||||
height: 256,
|
|
||||||
textSize: 'text-[164px]',
|
|
||||||
boxStyle: 'rounded-[32px]',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render an icon for the space.
|
|
||||||
*/
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
const options = getOptions(req.url);
|
|
||||||
const size = SIZES[options.size];
|
|
||||||
|
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
const spaceId = pointer.spaceId;
|
const context = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
const [space, { customization }] = await Promise.all([
|
return serveIcon(context, req);
|
||||||
getSpace(spaceId, pointer.siteShareKey),
|
|
||||||
getSiteData(pointer),
|
|
||||||
]);
|
|
||||||
const site = await getSite(pointer.organizationId, pointer.siteId);
|
|
||||||
const contentTitle = getContentTitle(space, customization, site);
|
|
||||||
|
|
||||||
return new ImageResponse(
|
|
||||||
(
|
|
||||||
<div
|
|
||||||
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
|
|
||||||
style={{
|
|
||||||
width: '100%',
|
|
||||||
height: '100%',
|
|
||||||
display: 'flex',
|
|
||||||
alignItems: 'center',
|
|
||||||
justifyContent: 'center',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<h2
|
|
||||||
tw={tcls(
|
|
||||||
size.textSize,
|
|
||||||
'font-bold',
|
|
||||||
'tracking-tight',
|
|
||||||
options.theme === 'light' ? 'text-black' : 'text-white',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{'emoji' in customization.favicon
|
|
||||||
? getEmojiForCode(customization.favicon.emoji)
|
|
||||||
: contentTitle.slice(0, 1).toUpperCase()}
|
|
||||||
</h2>
|
|
||||||
</div>
|
|
||||||
),
|
|
||||||
{
|
|
||||||
width: size.width,
|
|
||||||
height: size.height,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOptions(inputUrl: string): {
|
|
||||||
size: keyof typeof SIZES;
|
|
||||||
theme: 'light' | 'dark';
|
|
||||||
} {
|
|
||||||
const url = new URL(inputUrl);
|
|
||||||
const sizeParam = (url.searchParams.get('size') ?? 'small') as keyof typeof SIZES;
|
|
||||||
const themeParam = url.searchParams.get('theme') ?? 'light';
|
|
||||||
|
|
||||||
if (!SIZES[sizeParam] || !['light', 'dark'].includes(themeParam)) {
|
|
||||||
notFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
// @ts-ignore
|
|
||||||
size: sizeParam,
|
|
||||||
// @ts-ignore
|
|
||||||
theme: themeParam,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-223
@@ -1,229 +1,15 @@
|
|||||||
import { CustomizationHeaderPreset } from '@gitbook/api';
|
import type { NextRequest } from 'next/server';
|
||||||
import { redirect } from 'next/navigation';
|
|
||||||
import { ImageResponse } from 'next/og';
|
|
||||||
import { NextRequest } from 'next/server';
|
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { googleFontsMap } from '@/fonts';
|
import type { PageIdParams } from '@/components/SitePage';
|
||||||
import { colorContrast } from '@/lib/colors';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
import { getAbsoluteHref } from '@/lib/links';
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
import { filterOutNullable } from '@/lib/typescript';
|
import { serveOGImage } from '@/routes/ogimage';
|
||||||
import { getContentTitle } from '@/lib/utils';
|
|
||||||
|
|
||||||
import { PageIdParams, fetchPageData } from '../../../../fetch';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
async function loadGoogleFont(input: { fontFamily: string; text: string; weight: 400 | 700 }) {
|
export async function GET(_req: NextRequest, { params }: { params: Promise<PageIdParams> }) {
|
||||||
const { fontFamily, text, weight } = input;
|
const pointer = await getSiteContentPointer();
|
||||||
|
const baseContext = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
if (!text.trim()) {
|
return serveOGImage(baseContext, await params);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL('https://fonts.googleapis.com/css2');
|
|
||||||
url.searchParams.set('family', `${fontFamily}:wght@${weight}`);
|
|
||||||
url.searchParams.set('text', text);
|
|
||||||
|
|
||||||
const result = await fetch(url.href);
|
|
||||||
|
|
||||||
if (!result.ok) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const css = await result.text();
|
|
||||||
const resource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/);
|
|
||||||
const resourceUrl = resource ? resource[1] : null;
|
|
||||||
|
|
||||||
if (resourceUrl) {
|
|
||||||
const response = await fetch(resourceUrl);
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.arrayBuffer();
|
|
||||||
return {
|
|
||||||
name: fontFamily,
|
|
||||||
data,
|
|
||||||
style: 'normal' as const,
|
|
||||||
weight,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If for some reason we can't load the font, we'll just use the default one
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render the OpenGraph image for a space.
|
|
||||||
*/
|
|
||||||
export async function GET(req: NextRequest, { params }: { params: Promise<PageIdParams> }) {
|
|
||||||
const { space, page, customization, site } = await fetchPageData(await params);
|
|
||||||
|
|
||||||
// If user configured a custom social preview, we redirect to it.
|
|
||||||
if (customization.socialPreview.url) {
|
|
||||||
redirect(customization.socialPreview.url);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute all text to load only the necessary fonts
|
|
||||||
const contentTitle = customization.header.logo
|
|
||||||
? ''
|
|
||||||
: getContentTitle(space, customization, site ?? null);
|
|
||||||
const pageTitle = page
|
|
||||||
? page.title.length > 64
|
|
||||||
? page.title.slice(0, 64) + '...'
|
|
||||||
: page.title
|
|
||||||
: 'Not found';
|
|
||||||
const pageDescription =
|
|
||||||
page?.description && page?.title.length <= 64
|
|
||||||
? page.description.length > 164
|
|
||||||
? page.description.slice(0, 164) + '...'
|
|
||||||
: page.description
|
|
||||||
: '';
|
|
||||||
|
|
||||||
const fontFamily = googleFontsMap[customization.styling.font] ?? 'Inter';
|
|
||||||
|
|
||||||
const regularText = pageDescription;
|
|
||||||
const boldText = `${contentTitle}${pageTitle}`;
|
|
||||||
|
|
||||||
const fonts = (
|
|
||||||
await Promise.all([
|
|
||||||
loadGoogleFont({ fontFamily, text: regularText, weight: 400 }),
|
|
||||||
loadGoogleFont({ fontFamily, text: boldText, weight: 700 }),
|
|
||||||
])
|
|
||||||
).filter(filterOutNullable);
|
|
||||||
|
|
||||||
const theme = customization.themes.default;
|
|
||||||
const useLightTheme = theme === 'light';
|
|
||||||
|
|
||||||
// We have no access to CSS variables, so we'll have to hardcode some values
|
|
||||||
const baseColors = { light: '#ffffff', dark: '#111827' };
|
|
||||||
|
|
||||||
let colors = {
|
|
||||||
background: baseColors[theme],
|
|
||||||
gradient: customization.styling.primaryColor[theme],
|
|
||||||
title: customization.styling.primaryColor[theme],
|
|
||||||
body: baseColors[useLightTheme ? 'dark' : 'light'], // Invert text on background
|
|
||||||
};
|
|
||||||
|
|
||||||
const [gridWhite, gridBlack] = await Promise.all([
|
|
||||||
getAbsoluteHref('~gitbook/static/images/ogimage-grid-white.png', true),
|
|
||||||
getAbsoluteHref('~gitbook/static/images/ogimage-grid-black.png', true),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let gridAsset = useLightTheme ? gridBlack : gridWhite;
|
|
||||||
|
|
||||||
switch (customization.header.preset) {
|
|
||||||
case CustomizationHeaderPreset.Custom:
|
|
||||||
colors = {
|
|
||||||
background: customization.header.backgroundColor?.[theme] || colors.background,
|
|
||||||
gradient: customization.header.linkColor?.[theme] || colors.gradient,
|
|
||||||
title: customization.header.linkColor?.[theme] || colors.title,
|
|
||||||
body: colorContrast(
|
|
||||||
customization.header.backgroundColor?.[theme] || colors.background,
|
|
||||||
[baseColors.light, baseColors.dark],
|
|
||||||
),
|
|
||||||
};
|
|
||||||
gridAsset = colors.body == baseColors.light ? gridWhite : gridBlack;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CustomizationHeaderPreset.Bold:
|
|
||||||
colors = {
|
|
||||||
background: customization.styling.primaryColor[theme],
|
|
||||||
gradient: colorContrast(customization.styling.primaryColor[theme], [
|
|
||||||
baseColors.light,
|
|
||||||
baseColors.dark,
|
|
||||||
]),
|
|
||||||
title: colorContrast(customization.styling.primaryColor[theme], [
|
|
||||||
baseColors.light,
|
|
||||||
baseColors.dark,
|
|
||||||
]),
|
|
||||||
body: colorContrast(customization.styling.primaryColor[theme], [
|
|
||||||
baseColors.light,
|
|
||||||
baseColors.dark,
|
|
||||||
]),
|
|
||||||
};
|
|
||||||
gridAsset = colors.body == baseColors.light ? gridWhite : gridBlack;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const favicon = await (async () => {
|
|
||||||
if ('icon' in customization.favicon)
|
|
||||||
return (
|
|
||||||
<img
|
|
||||||
src={customization.favicon.icon[theme]}
|
|
||||||
width={40}
|
|
||||||
height={40}
|
|
||||||
tw="mr-4"
|
|
||||||
alt="Icon"
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
if ('emoji' in customization.favicon)
|
|
||||||
return (
|
|
||||||
<span tw="text-4xl mr-4">
|
|
||||||
{String.fromCodePoint(parseInt('0x' + customization.favicon.emoji))}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
const src = await getAbsoluteHref(
|
|
||||||
`~gitbook/icon?size=medium&theme=${customization.themes.default}`,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
return <img src={src} alt="Icon" width={40} height={40} tw="mr-4" />;
|
|
||||||
})();
|
|
||||||
|
|
||||||
return new ImageResponse(
|
|
||||||
(
|
|
||||||
<div
|
|
||||||
tw={`justify-between p-20 relative w-full h-full flex flex-col bg-[${colors.background}] text-[${colors.body}]`}
|
|
||||||
style={{
|
|
||||||
fontFamily,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{/* Gradient */}
|
|
||||||
<div
|
|
||||||
tw="absolute inset-0"
|
|
||||||
style={{
|
|
||||||
backgroundImage: `radial-gradient(ellipse 100% 100% at top right , ${colors.gradient}, ${colors.gradient}00)`,
|
|
||||||
opacity: 0.5,
|
|
||||||
}}
|
|
||||||
></div>
|
|
||||||
|
|
||||||
{/* Grid */}
|
|
||||||
<img tw="absolute inset-0 w-[100vw] h-[100vh]" src={gridAsset} alt="Grid" />
|
|
||||||
|
|
||||||
{/* Logo */}
|
|
||||||
{customization.header.logo ? (
|
|
||||||
<img
|
|
||||||
alt="Logo"
|
|
||||||
height={60}
|
|
||||||
src={
|
|
||||||
useLightTheme
|
|
||||||
? customization.header.logo.light
|
|
||||||
: customization.header.logo.dark
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<div tw="flex">
|
|
||||||
{favicon}
|
|
||||||
<h3 tw="text-4xl my-0 font-bold">{contentTitle}</h3>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Title and description */}
|
|
||||||
<div tw="flex flex-col">
|
|
||||||
<h1
|
|
||||||
tw={`text-8xl my-0 tracking-tight leading-none text-left text-[${colors.title}] font-bold`}
|
|
||||||
>
|
|
||||||
{pageTitle}
|
|
||||||
</h1>
|
|
||||||
{pageDescription ? (
|
|
||||||
<h2 tw="text-4xl mb-0 mt-8 w-[75%] font-normal">{pageDescription}</h2>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
),
|
|
||||||
{
|
|
||||||
width: 1200,
|
|
||||||
height: 630,
|
|
||||||
fonts: fonts.length ? fonts : undefined,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export default function ErrorPage(props: {
|
|||||||
'flex',
|
'flex',
|
||||||
'items-center',
|
'items-center',
|
||||||
'justify-center',
|
'justify-center',
|
||||||
'p-7',
|
'p-7'
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@@ -1,143 +0,0 @@
|
|||||||
import { RevisionPage } from '@gitbook/api';
|
|
||||||
import { redirect } from 'next/navigation';
|
|
||||||
|
|
||||||
import {
|
|
||||||
getRevisionPageByPath,
|
|
||||||
getSpaceContentData,
|
|
||||||
getSiteData,
|
|
||||||
getSiteRedirectBySource,
|
|
||||||
getPageDocument,
|
|
||||||
} from '@/lib/api';
|
|
||||||
import { resolvePagePath, resolvePageId } from '@/lib/pages';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
|
||||||
|
|
||||||
export interface PagePathParams {
|
|
||||||
pathname?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PageIdParams {
|
|
||||||
pageId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch all the data needed to render the content layout.
|
|
||||||
*/
|
|
||||||
export async function fetchContentData() {
|
|
||||||
const content = await getSiteContentPointer();
|
|
||||||
|
|
||||||
const [{ space, contentTarget, pages }, { customization, site, sections, spaces, scripts }] =
|
|
||||||
await Promise.all([
|
|
||||||
getSpaceContentData(content, content.siteShareKey),
|
|
||||||
getSiteData(content),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// we grab the space attached to the parent as it contains overriden customizations
|
|
||||||
const spaceRelativeToParent = spaces?.find((space) => space.id === content.spaceId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
content,
|
|
||||||
contentTarget,
|
|
||||||
space: spaceRelativeToParent ?? space,
|
|
||||||
pages,
|
|
||||||
site,
|
|
||||||
sections,
|
|
||||||
spaces,
|
|
||||||
shareKey: content.siteShareKey,
|
|
||||||
customization,
|
|
||||||
scripts,
|
|
||||||
ancestors: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch all the data needed to render the content.
|
|
||||||
* Optimized to fetch in parallel as much as possible.
|
|
||||||
*/
|
|
||||||
export async function fetchPageData(params: PagePathParams | PageIdParams) {
|
|
||||||
const contentData = await fetchContentData();
|
|
||||||
|
|
||||||
const page = await resolvePage({
|
|
||||||
organizationId: contentData.space.organization,
|
|
||||||
siteId: contentData.site.id,
|
|
||||||
spaceId: contentData.contentTarget.spaceId,
|
|
||||||
revisionId: contentData.contentTarget.revisionId,
|
|
||||||
pages: contentData.pages,
|
|
||||||
shareKey: contentData.shareKey,
|
|
||||||
params,
|
|
||||||
});
|
|
||||||
const document = page ? await getPageDocument(contentData.space.id, page.page) : null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
...contentData,
|
|
||||||
...page,
|
|
||||||
document,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve a page from the params.
|
|
||||||
* If the path can't be found, we try to resolve it from the API to handle redirects.
|
|
||||||
*/
|
|
||||||
async function resolvePage(input: {
|
|
||||||
organizationId: string;
|
|
||||||
siteId: string;
|
|
||||||
spaceId: string;
|
|
||||||
revisionId: string;
|
|
||||||
shareKey: string | undefined;
|
|
||||||
pages: RevisionPage[];
|
|
||||||
params: PagePathParams | PageIdParams;
|
|
||||||
}) {
|
|
||||||
const { organizationId, siteId, spaceId, revisionId, pages, shareKey, params } = input;
|
|
||||||
|
|
||||||
if ('pageId' in params) {
|
|
||||||
return resolvePageId(pages, params.pageId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawPathname = getPathnameParam(params);
|
|
||||||
const pathname = normalizePathname(rawPathname);
|
|
||||||
|
|
||||||
// When resolving a page, we use the lowercased pathname
|
|
||||||
const page = resolvePagePath(pages, pathname);
|
|
||||||
if (page) {
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't test path that are too long as GitBook doesn't support them and will return a 404 anyway.
|
|
||||||
if (rawPathname.length <= 512) {
|
|
||||||
// If page can't be found, we try with the API, in case we have a redirect at space level.
|
|
||||||
// We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
|
|
||||||
// The page rendering will take care of redirecting to a normalized pathname.
|
|
||||||
const resolved = await getRevisionPageByPath(spaceId, revisionId, rawPathname);
|
|
||||||
if (resolved) {
|
|
||||||
return resolvePageId(pages, resolved.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a page still can't be found, we try with the API, in case we have a redirect at site level.
|
|
||||||
const resolvedSiteRedirect = await getSiteRedirectBySource({
|
|
||||||
organizationId,
|
|
||||||
siteId,
|
|
||||||
source: rawPathname.startsWith('/') ? rawPathname : `/${rawPathname}`,
|
|
||||||
siteShareKey: input.shareKey,
|
|
||||||
});
|
|
||||||
if (resolvedSiteRedirect) {
|
|
||||||
return redirect(resolvedSiteRedirect.target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the page path from the params.
|
|
||||||
*/
|
|
||||||
export function getPathnameParam(params: PagePathParams): string {
|
|
||||||
const { pathname } = params;
|
|
||||||
return pathname ? pathname.map((part) => decodeURIComponent(part)).join('/') : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize the URL pathname into the format used in the revision page path.
|
|
||||||
*/
|
|
||||||
export function normalizePathname(pathname: string) {
|
|
||||||
return pathname.toLowerCase();
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CustomizationRootLayout } from '@/components/RootLayout';
|
import { CustomizationRootLayout } from '@/components/RootLayout';
|
||||||
import { getSiteData } from '@/lib/api';
|
|
||||||
import { getSiteContentPointer } from '@/lib/pointer';
|
import { getSiteContentPointer } from '@/lib/pointer';
|
||||||
|
import { fetchV1ContextForSitePointer } from '@/lib/v1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Layout to be used for the site root. It fetches the customization data for the site
|
* Layout to be used for the site root. It fetches the customization data for the site
|
||||||
@@ -10,7 +10,7 @@ export default async function SiteRootLayout(props: { children: React.ReactNode
|
|||||||
const { children } = props;
|
const { children } = props;
|
||||||
|
|
||||||
const pointer = await getSiteContentPointer();
|
const pointer = await getSiteContentPointer();
|
||||||
const { customization } = await getSiteData(pointer);
|
const { customization } = await fetchV1ContextForSitePointer(pointer);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
|
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user