-
Notifications
You must be signed in to change notification settings - Fork 4k
Adds the prompt block #4329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adds the prompt block #4329
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4825a2f
add prompt block
BrettJephson 5983853
add prompt to skeleton
BrettJephson 9a2317c
update api for prompt
BrettJephson 0f38d58
chore: format
BrettJephson d37852e
remove unused fallback
BrettJephson 0a8f7a7
fix translations
BrettJephson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "gitbook": patch | ||
| --- | ||
|
|
||
| Add a Prompt block |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/gitbook/src/components/DocumentView/Prompt/Prompt.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { tcls } from '@/lib/tailwind'; | ||
| import { | ||
| CustomizationPageActionType, | ||
| type DocumentBlockPrompt, | ||
| type SiteCustomizationSettings, | ||
| } from '@gitbook/api'; | ||
| import { validateIconName } from '@gitbook/icons/icons'; | ||
| import type { BlockProps } from '../Block'; | ||
| import { getPlainCodeBlock } from '../CodeBlock/highlight'; | ||
| import { PromptClient } from './PromptClient'; | ||
|
|
||
| export function Prompt(props: BlockProps<DocumentBlockPrompt>) { | ||
| const { block } = props; | ||
| const contentIcon = | ||
| block.data.icon && validateIconName(block.data.icon) ? block.data.icon : null; | ||
|
|
||
| return ( | ||
| <div | ||
| className={tcls( | ||
| 'relative flex w-full flex-col overflow-hidden', | ||
| 'border border-tint-subtle bg-tint-subtle theme-bold-tint:bg-tint-base theme-muted:bg-tint-base text-tint-strong contrast-more:border-tint contrast-more:bg-tint-base', | ||
| 'circular-corners:rounded-2xl rounded-corners:rounded-xl straight-corners:rounded-xs', | ||
| 'depth-subtle:shadow-xs' | ||
| )} | ||
| > | ||
| <PromptClient | ||
| contentIcon={contentIcon} | ||
| description={block.data.description} | ||
| prompt={getPromptText(block)} | ||
| openInAIProviders={getOpenInAIProviders(props)} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function getOpenInAIProviders(props: BlockProps<DocumentBlockPrompt>): boolean { | ||
| const { block, context } = props; | ||
| const { openInAIProviders } = block.data; | ||
|
|
||
| if (openInAIProviders !== undefined) { | ||
| return openInAIProviders; | ||
| } | ||
|
|
||
| const contentContext = context.contentContext; | ||
| if (contentContext && 'customization' in contentContext) { | ||
| const { pageActions } = contentContext.customization; | ||
| return isExternalAIPageActionEnabled(pageActions); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function isExternalAIPageActionEnabled( | ||
| pageActions: SiteCustomizationSettings['pageActions'] | ||
| ): boolean { | ||
| return pageActions.items | ||
| ? pageActions.items.includes(CustomizationPageActionType.ExternalAi) | ||
| : pageActions.externalAI; | ||
| } | ||
|
|
||
| function getPromptText(block: DocumentBlockPrompt): string { | ||
| return (block.nodes ?? []).map((node) => getPlainCodeBlock(node)).join('\n'); | ||
| } |
224 changes: 224 additions & 0 deletions
224
packages/gitbook/src/components/DocumentView/Prompt/PromptClient.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| Button, | ||
| ButtonGroup, | ||
| DropdownMenu, | ||
| DropdownMenuItem, | ||
| ToggleChevron, | ||
| } from '@/components/primitives'; | ||
| import { tString, useLanguage } from '@/intl/client'; | ||
| import { tcls } from '@/lib/tailwind'; | ||
| import { Icon, type IconName } from '@gitbook/icons'; | ||
| import React from 'react'; | ||
|
|
||
| const OPEN_IN_AI_PROVIDERS = ['claude', 'chatgpt', 'cursor'] as const; | ||
| type AIProviders = (typeof OPEN_IN_AI_PROVIDERS)[number]; | ||
|
|
||
| export function PromptClient(props: { | ||
| contentIcon: IconName | null; | ||
| description: string; | ||
| prompt: string; | ||
| openInAIProviders: boolean; | ||
| }) { | ||
| const { contentIcon, description, prompt, openInAIProviders } = props; | ||
| const language = useLanguage(); | ||
| const promptId = React.useId(); | ||
| const [open, setOpen] = React.useState(false); | ||
| const [headerHasFocus, setHeaderHasFocus] = React.useState(false); | ||
| return ( | ||
| <> | ||
| <div className="group/prompt-header relative flex min-h-9 flex-row items-center justify-between gap-4 px-3 py-2"> | ||
| <button | ||
| type="button" | ||
| aria-controls={promptId} | ||
| aria-expanded={open} | ||
| aria-label={tString(language, 'view')} | ||
| className={tcls( | ||
| 'absolute inset-0 z-10 cursor-pointer outline-hidden', | ||
| 'focus-visible:ring-2 focus-visible:ring-primary-hover' | ||
| )} | ||
| disabled={!prompt} | ||
| onBlur={() => setHeaderHasFocus(false)} | ||
| onClick={() => setOpen((prev) => !prev)} | ||
| onFocus={() => setHeaderHasFocus(true)} | ||
| /> | ||
| <div className="pointer-events-none relative z-0 flex min-w-0 flex-row items-center gap-2 text-tint-strong"> | ||
| <PromptDisclosureIcon | ||
| contentIcon={contentIcon} | ||
| headerHasFocus={headerHasFocus} | ||
| open={open} | ||
| /> | ||
| <span className="min-w-0 truncate">{description}</span> | ||
| </div> | ||
| <PromptActions prompt={prompt} openInAIProviders={openInAIProviders} /> | ||
| </div> | ||
| {open ? ( | ||
| <div id={promptId} className="border-tint-subtle border-t bg-tint-base"> | ||
| <pre className="overflow-auto p-4 text-sm text-tint-strong"> | ||
| <code className="language-markdown whitespace-pre-wrap font-mono"> | ||
| {prompt} | ||
| </code> | ||
| </pre> | ||
| </div> | ||
| ) : null} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function PromptDisclosureIcon(props: { | ||
| contentIcon: IconName | null; | ||
| headerHasFocus: boolean; | ||
| open: boolean; | ||
| }) { | ||
| const { contentIcon, headerHasFocus, open } = props; | ||
| return ( | ||
| <span className="relative flex size-4 shrink-0 items-center justify-center"> | ||
| {contentIcon ? ( | ||
| <> | ||
| <span | ||
| className={tcls( | ||
| 'flex items-center transition-opacity duration-150 group-hover/prompt-header:opacity-0', | ||
| headerHasFocus && 'opacity-0' | ||
| )} | ||
| > | ||
| <Icon icon={contentIcon} className="size-4 shrink-0" /> | ||
| </span> | ||
| <span | ||
| className={tcls( | ||
| 'absolute inset-0 flex items-center justify-center text-tint-subtle opacity-0 transition-opacity duration-150 group-hover/prompt-header:opacity-100', | ||
| headerHasFocus && 'opacity-100' | ||
| )} | ||
| > | ||
| <ToggleChevron open={open} orientation="right-to-down" className="size-3" /> | ||
| </span> | ||
| </> | ||
| ) : ( | ||
| <ToggleChevron | ||
| open={open} | ||
| orientation="right-to-down" | ||
| className="size-3 text-tint-subtle" | ||
| /> | ||
| )} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| function PromptActions(props: { prompt: string; openInAIProviders: boolean }) { | ||
| const { prompt, openInAIProviders } = props; | ||
|
|
||
| return ( | ||
| <ButtonGroup className="relative z-20 shrink-0 overflow-visible"> | ||
| <CopyPromptButton prompt={prompt} /> | ||
| {openInAIProviders ? <OpenPromptDropdown prompt={prompt} /> : null} | ||
| </ButtonGroup> | ||
| ); | ||
| } | ||
|
|
||
| // time in milliseconds to show the "Copied" message after copying a prompt | ||
| const COPIED_MESSAGE_DURATION = 1000; | ||
|
|
||
| function CopyPromptButton(props: { prompt: string }) { | ||
| const { prompt } = props; | ||
| const language = useLanguage(); | ||
| const [copied, setCopied] = React.useState(false); | ||
|
|
||
| React.useEffect(() => { | ||
| if (!copied) { | ||
| return; | ||
| } | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| setCopied(false); | ||
| }, COPIED_MESSAGE_DURATION); | ||
|
|
||
| return () => { | ||
| clearTimeout(timeout); | ||
| }; | ||
| }, [copied]); | ||
|
|
||
| return ( | ||
| <Button | ||
| variant="secondary" | ||
| size="xsmall" | ||
| icon={copied ? 'check' : 'copy'} | ||
| label={copied ? tString(language, 'code_copied') : tString(language, 'prompt_copy')} | ||
| className="bg-tint-base" | ||
| disabled={!prompt} | ||
| onClick={() => { | ||
| navigator.clipboard.writeText(prompt); | ||
| setCopied(true); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function OpenPromptDropdown(props: { prompt: string }) { | ||
| const { prompt } = props; | ||
| const language = useLanguage(); | ||
|
|
||
| return ( | ||
| <DropdownMenu | ||
| align="end" | ||
| className="!min-w-48 max-w-max" | ||
| button={ | ||
| <Button | ||
| icon={<ToggleChevron className="size-text-sm" />} | ||
| label={tString(language, 'open')} | ||
| iconOnly | ||
| size="xsmall" | ||
| variant="secondary" | ||
| className="bg-tint-base" | ||
| disabled={!prompt} | ||
| /> | ||
| } | ||
| > | ||
| {OPEN_IN_AI_PROVIDERS.map((provider) => { | ||
| const definition = getPromptOpenActionDefinition(provider, prompt); | ||
|
|
||
| return ( | ||
| <DropdownMenuItem | ||
| key={provider} | ||
| href={definition.href} | ||
| target="_blank" | ||
| leadingIcon={definition.icon} | ||
| > | ||
| {tString(language, 'open_in', definition.label)} | ||
| </DropdownMenuItem> | ||
| ); | ||
| })} | ||
| </DropdownMenu> | ||
| ); | ||
| } | ||
|
|
||
| function getPromptOpenActionDefinition( | ||
| action: AIProviders, | ||
| prompt: string | ||
| ): { href: string; icon: IconName; label: string } { | ||
| const encodedPrompt = encodeURIComponent(prompt); | ||
|
|
||
| switch (action) { | ||
| case 'cursor': | ||
| return { | ||
| href: `${CURSOR_PROMPT_URL}?text=${encodedPrompt}`, | ||
| icon: 'cursor', | ||
| label: 'Cursor', | ||
| }; | ||
| case 'claude': | ||
| return { | ||
| href: `${CLAUDE_PROMPT_URL}?q=${encodedPrompt}`, | ||
| icon: 'claude', | ||
| label: 'Claude', | ||
| }; | ||
| case 'chatgpt': | ||
| return { | ||
| href: `${CHATGPT_PROMPT_URL}?q=${encodedPrompt}`, | ||
| icon: 'chatgpt', | ||
| label: 'ChatGPT', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const CLAUDE_PROMPT_URL = 'https://claude.ai/new'; | ||
| const CHATGPT_PROMPT_URL = 'https://chat.openai.com/'; | ||
| const CURSOR_PROMPT_URL = 'https://cursor.com/link/prompt'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './Prompt'; | ||
| export type * from './types'; |
14 changes: 14 additions & 0 deletions
14
packages/gitbook/src/components/DocumentView/Prompt/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { DocumentBlockCode } from '@gitbook/api'; | ||
|
|
||
| export type PromptBlock = { | ||
| object: 'block'; | ||
| type: 'prompt'; | ||
| key?: string; | ||
| data: { | ||
| icon?: string; | ||
| description?: string; | ||
| openInAIProviders?: boolean; | ||
| }; | ||
| nodes?: DocumentBlockCode[]; | ||
| isVoid?: false; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.