From 2ff77e15b52fd292c3bc03fa1641ead42a4ff6af Mon Sep 17 00:00:00 2001 From: "Nolann B." <100787331+nolannbiron@users.noreply.github.com> Date: Fri, 4 Sep 2026 12:17:00 +0200 Subject: [PATCH] Attribute a markdown request from the page actions menu (#4581) --- .changeset/markdown-page-action-source.md | 5 +++++ bun.lock | 4 ++-- package.json | 2 +- .../gitbook/src/components/PageActions/PageActions.tsx | 10 +++++++--- packages/gitbook/src/middleware.ts | 9 +++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 .changeset/markdown-page-action-source.md diff --git a/.changeset/markdown-page-action-source.md b/.changeset/markdown-page-action-source.md new file mode 100644 index 000000000..927930487 --- /dev/null +++ b/.changeset/markdown-page-action-source.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Bump `@gitbook/api` to 0.199.0, and record a markdown request made from the page actions menu as a page action rather than an agent request. diff --git a/bun.lock b/bun.lock index d354000e9..5978e4818 100644 --- a/bun.lock +++ b/bun.lock @@ -354,7 +354,7 @@ }, "catalog": { "@base-ui/react": "^1.7.0", - "@gitbook/api": "0.198.0", + "@gitbook/api": "0.199.0", "@scalar/api-client-react": "^1.3.46", "@tsconfig/node20": "^20.1.6", "@tsconfig/strictest": "^2.0.6", @@ -726,7 +726,7 @@ "@fortawesome/fontawesome-svg-core": ["@fortawesome/fontawesome-svg-core@7.2.0", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "7.2.0" } }, "sha512-6639htZMjEkwskf3J+e6/iar+4cTNM9qhoWuRfj9F3eJD6r7iCzV1SWnQr2Mdv0QT0suuqU8BoJCZUyCtP9R4Q=="], - "@gitbook/api": ["@gitbook/api@0.198.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-ZGXqYip6YFsCM5rqlJ+n8/Esw1bMNK4whqiMoWMeQgMKEX5YXVQaBuVRxU1jDEI49kup+T56xPq3I9g6Wcayng=="], + "@gitbook/api": ["@gitbook/api@0.199.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-yLxkSTXlGk7jbtThV2vpnTfqZTq6yLgMJt6jhDGqOmP0LsYAVYYtg8NPf2tXp37aF46m3Z0D908wfSQyu374cg=="], "@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"], diff --git a/package.json b/package.json index 9747484e5..ccac0c529 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@tsconfig/strictest": "^2.0.6", "@tsconfig/node20": "^20.1.6", "@base-ui/react": "^1.7.0", - "@gitbook/api": "0.198.0", + "@gitbook/api": "0.199.0", "@scalar/api-client-react": "^1.3.46", "@types/react": "^19.0.0", "@types/react-dom": "^19.0.0", diff --git a/packages/gitbook/src/components/PageActions/PageActions.tsx b/packages/gitbook/src/components/PageActions/PageActions.tsx index 7e9ff1648..df50737f0 100644 --- a/packages/gitbook/src/components/PageActions/PageActions.tsx +++ b/packages/gitbook/src/components/PageActions/PageActions.tsx @@ -5,7 +5,7 @@ import QuickLRU from 'quick-lru'; import React from 'react'; import { createStore, useStore } from 'zustand'; -import type { GitSyncState } from '@gitbook/api'; +import { type GitSyncState, SiteInsightsMarkdownSource } from '@gitbook/api'; import { Icon, type IconName, IconStyle } from '@gitbook/icons'; import { useAIChatController, useAIChatState } from '@/components/AI'; @@ -125,6 +125,10 @@ function useCopiedStore(stateKey: string) { return useStore(getOrCreateCopiedStoreByKey(stateKey)); } +function getReaderMarkdownURL(markdownPageURL: string) { + return `${markdownPageURL}?displayAgentInstructions=false&markdownSource=${SiteInsightsMarkdownSource.PageAction}`; +} + /** * Cache for the markdown version of the page. */ @@ -149,7 +153,7 @@ export function ActionCopyMarkdown(props: { const fetchMarkdown = async () => { setLoading(true); - const humanURL = `${markdownPageURL}?displayAgentInstructions=false`; + const humanURL = getReaderMarkdownURL(markdownPageURL); const result = await fetch(humanURL).then((res) => res.text()); markdownCache.set(markdownPageURL, result); @@ -196,7 +200,7 @@ export function ActionViewAsMarkdown(props: { markdownPageURL: string; type: Pag icon="markdown" label={tString(language, 'view_page_markdown')} description={tString(language, 'view_page_plaintext')} - href={`${markdownPageURL}?displayAgentInstructions=false`} + href={getReaderMarkdownURL(markdownPageURL)} /> ); } diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 57bba5dea..d8c760d61 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -13,6 +13,7 @@ import { SiteInsightsDisplayContext, type SiteInsightsEventLocation, SiteInsightsLLMSVariant, + SiteInsightsMarkdownSource, } from '@gitbook/api'; import { @@ -533,6 +534,9 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { if (rewrittenURL.searchParams.has('displayAgentInstructions')) { rewrittenURL.searchParams.delete('displayAgentInstructions'); } + if (rewrittenURL.searchParams.has('markdownSource')) { + rewrittenURL.searchParams.delete('markdownSource'); + } const response = NextResponse.rewrite(rewrittenURL, { request: { @@ -897,6 +901,10 @@ function encodePathInSiteContent( // It is encoded as a second path segment (the route is statically rendered, so it can't // read query params at runtime — the question is path-encoded for the same reason). const goal = searchParams.get('goal'); + // Validated: this is user input going into insights. + const markdownSource = Object.values(SiteInsightsMarkdownSource).find( + (source) => source === searchParams.get('markdownSource') + ); return { pathname: typeof ask === 'string' @@ -922,6 +930,7 @@ function encodePathInSiteContent( : [ { type: 'page_markdown_request', + ...(markdownSource ? { markdownSource } : {}), location: { displayContext: SiteInsightsDisplayContext.Server, },