Skip to content

Commit 3319375

Browse files
authored
Support OpenAPI operation block (#2902)
1 parent c5a4619 commit 3319375

File tree

8 files changed

+54
-34
lines changed

8 files changed

+54
-34
lines changed

.changeset/tough-plums-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Support OpenAPI operation block

bun.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"name": "gitbook",
4141
"version": "0.6.3",
4242
"dependencies": {
43-
"@gitbook/api": "^0.96.0",
43+
"@gitbook/api": "0.96.1",
4444
"@gitbook/cache-do": "workspace:*",
4545
"@gitbook/colors": "workspace:*",
4646
"@gitbook/emoji-codepoints": "workspace:*",
@@ -120,7 +120,7 @@
120120
"name": "gitbook-v2",
121121
"version": "0.1.0",
122122
"dependencies": {
123-
"@gitbook/api": "^0.96.0",
123+
"@gitbook/api": "0.96.1",
124124
"@sindresorhus/fnv1a": "^3.1.0",
125125
"next": "canary",
126126
"react": "^19.0.0",
@@ -180,7 +180,7 @@
180180
"name": "@gitbook/react-contentkit",
181181
"version": "0.6.0",
182182
"dependencies": {
183-
"@gitbook/api": "^0.96.0",
183+
"@gitbook/api": "0.96.1",
184184
"assert-never": "^1.2.1",
185185
"classnames": "^2.5.1",
186186
},
@@ -604,7 +604,7 @@
604604

605605
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
606606

607-
"@gitbook/api": ["@gitbook/[email protected].0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-JN6k0PlpyQQhtr8sNnbKIWQ7n9gCFqhrDo33v/41d41M1wgz+eowm1jdiYtX0tsXBH7D1YHmBMbNnDI8jQwbwA=="],
607+
"@gitbook/api": ["@gitbook/[email protected].1", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-E1hpaFNFoEyc8E4rIQyC6UD3kErGXAI0lKt04zYQQYOBLjRIQ57D4oNIIJBjWn4hHeSxRxWvW3NcxzvOGMmIqQ=="],
608608

609609
"@gitbook/cache-do": ["@gitbook/cache-do@workspace:packages/cache-do"],
610610

@@ -4238,7 +4238,7 @@
42384238

42394239
"gaxios/node-fetch": ["[email protected]", "", { "dependencies": { "whatwg-url": "^5.0.0" }, "peerDependencies": { "encoding": "^0.1.0" }, "optionalPeers": ["encoding"] }, "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A=="],
42404240

4241-
"gitbook-v2/next": ["[email protected]", "", { "dependencies": { "@next/env": "15.2.0-canary.74", "@swc/counter": "0.1.3", "@swc/helpers": "0.5.15", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001579", "postcss": "8.4.31", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "15.2.0-canary.74", "@next/swc-darwin-x64": "15.2.0-canary.74", "@next/swc-linux-arm64-gnu": "15.2.0-canary.74", "@next/swc-linux-arm64-musl": "15.2.0-canary.74", "@next/swc-linux-x64-gnu": "15.2.0-canary.74", "@next/swc-linux-x64-musl": "15.2.0-canary.74", "@next/swc-win32-arm64-msvc": "15.2.0-canary.74", "@next/swc-win32-x64-msvc": "15.2.0-canary.74", "sharp": "^0.33.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.41.2", "babel-plugin-react-compiler": "*", "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "react-dom": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "sass": "^1.3.0" }, "optionalPeers": ["@opentelemetry/api", "@playwright/test", "babel-plugin-react-compiler", "sass"], "bin": { "next": "dist/bin/next" } }, "sha512-0BkYq0136wVKLogN0R85cnSKpnlSHg4/mA5lMWJGp/ltfai+vRaejWre1fhpjNVFuNOnzblFhpTQlwxZk/mN8g=="],
4241+
"gitbook-v2/next": ["[email protected]", "", { "dependencies": { "@next/env": "15.2.1-canary.2", "@swc/counter": "0.1.3", "@swc/helpers": "0.5.15", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001579", "postcss": "8.4.31", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "15.2.1-canary.2", "@next/swc-darwin-x64": "15.2.1-canary.2", "@next/swc-linux-arm64-gnu": "15.2.1-canary.2", "@next/swc-linux-arm64-musl": "15.2.1-canary.2", "@next/swc-linux-x64-gnu": "15.2.1-canary.2", "@next/swc-linux-x64-musl": "15.2.1-canary.2", "@next/swc-win32-arm64-msvc": "15.2.1-canary.2", "@next/swc-win32-x64-msvc": "15.2.1-canary.2", "sharp": "^0.33.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.41.2", "babel-plugin-react-compiler": "*", "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "react-dom": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "sass": "^1.3.0" }, "optionalPeers": ["@opentelemetry/api", "@playwright/test", "babel-plugin-react-compiler", "sass"], "bin": { "next": "dist/bin/next" } }, "sha512-Vrl8pSN3I7fuUWYWSqbzRsItKYm0X0OXPwm6/DuX7Pgi6uyYT6uwpvEGiPHSGV5Up1MOOtlsCjIbEQlNJ3t1pg=="],
42424242

42434243
"google-auth-library/jws": ["[email protected]", "", { "dependencies": { "jwa": "^2.0.0", "safe-buffer": "^5.0.1" } }, "sha512-KDncfTmOZoOMTFG4mBlG0qUIOlc03fmzH+ru6RgYVZhPkyiy/92Owlt/8UEN+a4TXR1FQetfIpJE8ApdvdVxTg=="],
42444244

@@ -5138,23 +5138,23 @@
51385138

51395139
"gaxios/https-proxy-agent/debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ=="],
51405140

5141-
"gitbook-v2/next/@next/env": ["@next/[email protected].0-canary.74", "", {}, "sha512-REBWD6TnX8M89GsvyuGJCziV2SJOPOZJjuo2y+uN8qlBmVnJQ9qk4tGTRqTygnQREfiKXjumncsGG0xZaWGZbw=="],
5141+
"gitbook-v2/next/@next/env": ["@next/[email protected].1-canary.2", "", {}, "sha512-8bs9W8rN9ln19PA3IdglEkuRxNI+VAjY/Gxkkp24qpSO19uKHbORFz3I5DGyRzHzTRDpCRGHQDKNbcy2b0buOQ=="],
51425142

5143-
"gitbook-v2/next/@next/swc-darwin-arm64": ["@next/[email protected].0-canary.74", "", { "os": "darwin", "cpu": "arm64" }, "sha512-R5pdSU94H9LSHXtDpvUWwKWL9p9xYlizenImZ7V1AAqbhiksBfORZHoHLZg6M0I+Ko6PTH2g6vPEAnKMYsH/xA=="],
5143+
"gitbook-v2/next/@next/swc-darwin-arm64": ["@next/[email protected].1-canary.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-PvoxT5tucaeREa7uhTxEaSW9sL4Sr8WfluVfSS+bj2axZpQ567gB/I+Eias67FPRW80cZaPMGsorNjyaq+cJuA=="],
51445144

5145-
"gitbook-v2/next/@next/swc-darwin-x64": ["@next/[email protected].0-canary.74", "", { "os": "darwin", "cpu": "x64" }, "sha512-2goUXrPl8iYEuS1JGzO17kmF+OTj7cCWX8nL/s/XpFC/iUJoWdcmWohaKP+DvKnS4SlYDdpSt733WaKGTwzvpw=="],
5145+
"gitbook-v2/next/@next/swc-darwin-x64": ["@next/[email protected].1-canary.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-tPg9F3bT5MMalZpLfd+paiWWaWLvUaWAFZfIGEBgozYgIBd66pROkb+dXsJVd73IlYoBDh3mYcK7me2uYImBOA=="],
51465146

5147-
"gitbook-v2/next/@next/swc-linux-arm64-gnu": ["@next/[email protected].0-canary.74", "", { "os": "linux", "cpu": "arm64" }, "sha512-FoulYpMof4m6LZJ5DR1agKQ0p0DyfX9ylo5CTzvFNfLmKkYmQWGaxG1KKv4YYaivhgYJ6GgubhXExJo/pycZQQ=="],
5147+
"gitbook-v2/next/@next/swc-linux-arm64-gnu": ["@next/[email protected].1-canary.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-LmnZN/psunvUXofINdGENFNDv7yRIET2szi2FREMr1epKL3NsAQD0k4hv3pwRLwkVb8z6Rj+TXzX0pFChHOlhg=="],
51485148

5149-
"gitbook-v2/next/@next/swc-linux-arm64-musl": ["@next/[email protected].0-canary.74", "", { "os": "linux", "cpu": "arm64" }, "sha512-gnmoPYieUhFysvzSVAbp83Z0n6MmPf1FsD300EA0OGsdhGcObRP/bPafporanU0XCHt44IgyA5KHwKIAoM3D3A=="],
5149+
"gitbook-v2/next/@next/swc-linux-arm64-musl": ["@next/[email protected].1-canary.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-5Ma1ATOCdgei1LRAbziBYciunyuG0uUASoiz7DDw40Ox5YuDD4Tznbg3re5ltWRwMAl9NNQa8ns3u+Q3Zs1NsQ=="],
51505150

5151-
"gitbook-v2/next/@next/swc-linux-x64-gnu": ["@next/[email protected].0-canary.74", "", { "os": "linux", "cpu": "x64" }, "sha512-pdkOOtywXSeGvdLr1xYHTSOa4O7zFJ1z79nvCnKCyeO690L5wtc6gxt5Kqjc6/FdDuhxRxCMVE3bEYdLmOGH1w=="],
5151+
"gitbook-v2/next/@next/swc-linux-x64-gnu": ["@next/[email protected].1-canary.2", "", { "os": "linux", "cpu": "x64" }, "sha512-R8bbqvKkVtzjN/KWogQQvWXgBTyWXcOEVgiPMXKgx7/zfybWeL+WjSXeepw4xYyDlQ1vC//HwEqLfFX41Jht4Q=="],
51525152

5153-
"gitbook-v2/next/@next/swc-linux-x64-musl": ["@next/[email protected].0-canary.74", "", { "os": "linux", "cpu": "x64" }, "sha512-WjleLpUuSYHiX8/fezvIWDBnNCmLjxypWloWaTunGy13A492CM+FjHbjmSNljDwI7LwT71jTZnc7+vM9uHz9Sg=="],
5153+
"gitbook-v2/next/@next/swc-linux-x64-musl": ["@next/[email protected].1-canary.2", "", { "os": "linux", "cpu": "x64" }, "sha512-wWr41f7g3H5FcLguoAS7bvqSqaVkhK97/fk7ANG1l5yW13F9qVPboVZvgVFrRsPFZgqANxs4AmhKAjjjT+mL1g=="],
51545154

5155-
"gitbook-v2/next/@next/swc-win32-arm64-msvc": ["@next/[email protected].0-canary.74", "", { "os": "win32", "cpu": "arm64" }, "sha512-E7G6Tj9VB09aog/7SeV3WbFdSrDRDaHeV6gtgHc5OuLpP37FwRVgFzBzL4u2MsOxqAkIw0Iq+SMPmagQLWrAWg=="],
5155+
"gitbook-v2/next/@next/swc-win32-arm64-msvc": ["@next/[email protected].1-canary.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-ogzvioGSaJ9F2VWZXiLQR4K4G6fbBUdlZfKowOJyFKfBhPK+WuwJlEnzWfPAe5YjtiQU2H+eMdZdC25DXDXo6g=="],
51565156

5157-
"gitbook-v2/next/@next/swc-win32-x64-msvc": ["@next/[email protected].0-canary.74", "", { "os": "win32", "cpu": "x64" }, "sha512-VipdINDvcGUpl6XwyhlwIoa3FBLFoMa1+EMyBDBySIH2Teq6HIy6Rqec5AfqDHbf3EKqRNTLx8tWum20cK6tXQ=="],
5157+
"gitbook-v2/next/@next/swc-win32-x64-msvc": ["@next/[email protected].1-canary.2", "", { "os": "win32", "cpu": "x64" }, "sha512-6sss7xGWck4DrJC7h/Gc2LFN3xh2dxnXxYmkxOD9gOq/yFdX7NSrNdCfNvXg5+6EFcTPURRg9gS9VgkzOCxp8Q=="],
51585158

51595159
"gitbook-v2/next/postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } }, "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ=="],
51605160

packages/gitbook-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"next": "canary",
77
"react": "^19.0.0",
88
"react-dom": "^19.0.0",
9-
"@gitbook/api": "^0.96.0",
9+
"@gitbook/api": "0.96.1",
1010
"@sindresorhus/fnv1a": "^3.1.0",
1111
"server-only": "^0.0.1"
1212
},

packages/gitbook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"clean": "rm -rf ./.next && rm -rf ./public/~gitbook/static/icons && rm -rf ./public/~gitbook/static/math"
1717
},
1818
"dependencies": {
19-
"@gitbook/api": "^0.96.0",
19+
"@gitbook/api": "0.96.1",
2020
"@gitbook/cache-do": "workspace:*",
2121
"@gitbook/colors": "workspace:*",
2222
"@gitbook/emoji-codepoints": "workspace:*",

packages/gitbook/src/components/DocumentView/Block.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function Block<T extends DocumentBlock>(props: BlockProps<T>) {
8181
case 'table':
8282
return <Table {...props} block={block} />;
8383
case 'swagger':
84+
case 'openapi-operation':
8485
return <OpenAPI {...props} block={block} />;
8586
case 'embed':
8687
return <Embed {...props} block={block} />;
@@ -154,6 +155,7 @@ export function BlockSkeleton(props: { block: DocumentBlock; style: ClassValue }
154155
case 'expandable':
155156
case 'table':
156157
case 'swagger':
158+
case 'openapi-operation':
157159
case 'math':
158160
case 'divider':
159161
case 'content-ref':

packages/gitbook/src/components/DocumentView/OpenAPI/OpenAPI.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import type { DocumentBlockOpenAPI, JSONDocument } from '@gitbook/api';
1+
import type { JSONDocument } from '@gitbook/api';
22
import { Icon } from '@gitbook/icons';
33
import { OpenAPIOperation } from '@gitbook/react-openapi';
44

5-
import { resolveOpenAPIBlock } from '@/lib/openapi/fetch';
5+
import { type AnyOpenAPIOperationBlock, resolveOpenAPIBlock } from '@/lib/openapi/fetch';
66
import { tcls } from '@/lib/tailwind';
77

88
import type { BlockProps } from '../Block';
99
import { PlainCodeBlock } from '../CodeBlock';
1010
import { DocumentView } from '../DocumentView';
1111
import { Heading } from '../Heading';
1212

13-
import './style.css';
1413
import './scalar.css';
14+
import './style.css';
1515

1616
/**
17-
* Render an OpenAPI block.
17+
* Render an openapi block or an openapi-operation block.
1818
*/
19-
export async function OpenAPI(props: BlockProps<DocumentBlockOpenAPI>) {
19+
export async function OpenAPI(props: BlockProps<AnyOpenAPIOperationBlock>) {
2020
const { style } = props;
2121
return (
22-
<div className={tcls('w-full', 'flex', 'flex-row', style, 'max-w-full')}>
22+
<div className={tcls('flex w-full', style, 'max-w-full')}>
2323
<OpenAPIBody {...props} />
2424
</div>
2525
);
2626
}
2727

28-
async function OpenAPIBody(props: BlockProps<DocumentBlockOpenAPI>) {
28+
async function OpenAPIBody(props: BlockProps<AnyOpenAPIOperationBlock>) {
2929
const { block, context } = props;
3030

3131
if (!context.contentContext) {

packages/gitbook/src/lib/openapi/fetch.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import type { DocumentBlockOpenAPI } from '@gitbook/api';
1+
import type { DocumentBlockOpenAPI, DocumentBlockOpenAPIOperation } from '@gitbook/api';
22
import { OpenAPIParseError, parseOpenAPI } from '@gitbook/openapi-parser';
33
import { type OpenAPIOperationData, resolveOpenAPIOperation } from '@gitbook/react-openapi';
44
import type { GitBookAnyContext } from '@v2/lib/context';
55

66
import { type CacheFunctionOptions, cache, noCacheFetchOptions } from '@/lib/cache';
77

8+
import { assert } from 'ts-essentials';
89
import { resolveContentRef } from '../references';
910
import { isV2 } from '../v2';
1011
import { enrichFilesystem } from './enrich';
1112

12-
const weakmap = new WeakMap<DocumentBlockOpenAPI, ResolveOpenAPIBlockResult>();
13+
export type AnyOpenAPIOperationBlock = DocumentBlockOpenAPI | DocumentBlockOpenAPIOperation;
14+
15+
const weakmap = new WeakMap<AnyOpenAPIOperationBlock, Promise<ResolveOpenAPIBlockResult>>();
1316

1417
/**
1518
* Cache the result of resolving an OpenAPI block.
1619
* It is important because the resolve is called in sections and in the block itself.
1720
*/
18-
export function resolveOpenAPIBlock(args: ResolveOpenAPIBlockArgs): ResolveOpenAPIBlockResult {
21+
export function resolveOpenAPIBlock(
22+
args: ResolveOpenAPIBlockArgs
23+
): Promise<ResolveOpenAPIBlockResult> {
1924
if (weakmap.has(args.block)) {
2025
return weakmap.get(args.block)!;
2126
}
@@ -26,30 +31,38 @@ export function resolveOpenAPIBlock(args: ResolveOpenAPIBlockArgs): ResolveOpenA
2631
}
2732

2833
type ResolveOpenAPIBlockArgs = {
29-
block: DocumentBlockOpenAPI;
34+
block: AnyOpenAPIOperationBlock;
3035
context: GitBookAnyContext;
3136
};
32-
type ResolveOpenAPIBlockResult = Promise<
37+
export type ResolveOpenAPIBlockResult =
3338
| { error?: undefined; data: OpenAPIOperationData | null; specUrl: string | null }
34-
| { error: OpenAPIParseError; data?: undefined; specUrl?: undefined }
35-
>;
39+
| { error: OpenAPIParseError; data?: undefined; specUrl?: undefined };
3640
/**
3741
* Resolve OpenAPI block.
3842
*/
39-
async function baseResolveOpenAPIBlock(args: ResolveOpenAPIBlockArgs): ResolveOpenAPIBlockResult {
43+
async function baseResolveOpenAPIBlock(
44+
args: ResolveOpenAPIBlockArgs
45+
): Promise<ResolveOpenAPIBlockResult> {
4046
const { context, block } = args;
4147
if (!block.data.path || !block.data.method) {
4248
return { data: null, specUrl: null };
4349
}
4450

45-
const resolved = block.data.ref ? await resolveContentRef(block.data.ref, context) : null;
51+
const ref = block.data.ref;
52+
const resolved = ref ? await resolveContentRef(ref, context) : null;
4653

4754
if (!resolved) {
4855
return { data: null, specUrl: null };
4956
}
5057

5158
try {
52-
const filesystem = resolved.openAPIFilesystem ?? (await fetchFilesystem(resolved.href));
59+
const filesystem = await (() => {
60+
if (ref.kind === 'openapi') {
61+
assert(resolved.openAPIFilesystem);
62+
return resolved.openAPIFilesystem;
63+
}
64+
return fetchFilesystem(resolved.href);
65+
})();
5366

5467
const data = await resolveOpenAPIOperation(filesystem, {
5568
path: block.data.path,

packages/react-contentkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"classnames": "^2.5.1",
13-
"@gitbook/api": "^0.96.0",
13+
"@gitbook/api": "0.96.1",
1414
"assert-never": "^1.2.1"
1515
},
1616
"peerDependencies": {

0 commit comments

Comments
 (0)