|
| 1 | +import React from 'react' |
1 | 2 | import ReactMarkdown from 'react-markdown' |
2 | 3 | import type { Components } from 'react-markdown' |
3 | | -import cx from 'classnames' |
4 | 4 | import remarkGfm from 'remark-gfm' |
| 5 | +import cx from 'classnames' |
| 6 | +import { IconButton } from '@primer/react' |
| 7 | +import { CopyIcon, CheckIcon } from '@primer/octicons-react' |
| 8 | +import { announce } from '@primer/live-region-element' |
| 9 | + |
| 10 | +import { useTranslation } from '@/languages/components/useTranslation' |
| 11 | +import useCopyClipboard from '@/rest/components/useClipboard' |
| 12 | +import { EventType } from '@/events/types' |
| 13 | +import { sendEvent } from '@/events/components/events' |
5 | 14 |
|
6 | 15 | export type MarkdownContentPropsT = { |
7 | 16 | children: string |
8 | 17 | className?: string |
9 | 18 | openLinksInNewTab?: boolean |
10 | 19 | includeQueryParams?: boolean |
| 20 | + codeBlocksCopyable?: boolean |
11 | 21 | eventGroupKey?: string |
12 | 22 | eventGroupId?: string |
13 | 23 | as?: keyof JSX.IntrinsicElements |
14 | 24 | tabIndex?: number |
15 | 25 | } |
16 | 26 |
|
17 | | -// For content that comes in a Markdown string |
18 | | -// e.g. a GPT Response |
19 | | - |
20 | 27 | export const UnrenderedMarkdownContent = ({ |
21 | 28 | children, |
22 | 29 | className, |
23 | 30 | openLinksInNewTab = true, |
24 | 31 | includeQueryParams = true, |
| 32 | + codeBlocksCopyable = true, |
25 | 33 | eventGroupKey = '', |
26 | 34 | eventGroupId = '', |
27 | 35 | ...restProps |
28 | 36 | }: MarkdownContentPropsT) => { |
| 37 | + const { t } = useTranslation('search') |
29 | 38 | // Overrides for ReactMarkdown components |
30 | 39 | const components = {} as Components |
31 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
32 | | - components.a = ({ node, ...props }) => { |
| 40 | + if (codeBlocksCopyable) { |
| 41 | + // Override the default code block to make multiline code blocks copyable |
| 42 | + components.code = ({ ...props }) => { |
| 43 | + // get the literal text of the code block |
| 44 | + let text = String(props.children) |
| 45 | + // If the codeblock is not multiline, return inline code block without copy functionality |
| 46 | + if (!text.includes('\n')) { |
| 47 | + return <code {...props}>{props.children}</code> |
| 48 | + } else { |
| 49 | + // Otherwise it's multiline and we want to make it copyable |
| 50 | + text = text.replace(/\n$/, '') |
| 51 | + } |
| 52 | + |
| 53 | + const [isCopied, copyToClipboard] = useCopyClipboard(text, { |
| 54 | + successDuration: 2000, |
| 55 | + }) |
| 56 | + |
| 57 | + return ( |
| 58 | + <div> |
| 59 | + <IconButton |
| 60 | + size="small" |
| 61 | + icon={isCopied ? CheckIcon : CopyIcon} |
| 62 | + className="btn-octicon" |
| 63 | + aria-label={t('search.ai.response.copy_code')} |
| 64 | + onClick={async () => { |
| 65 | + await copyToClipboard() |
| 66 | + announce(t('search.ai.response.copied_code')) |
| 67 | + sendEvent({ |
| 68 | + type: EventType.clipboard, |
| 69 | + clipboard_operation: 'copy', |
| 70 | + eventGroupKey: eventGroupKey, |
| 71 | + eventGroupId: eventGroupId, |
| 72 | + }) |
| 73 | + }} |
| 74 | + sx={{ |
| 75 | + position: 'absolute', |
| 76 | + right: '1.3rem', |
| 77 | + marginTop: '-.7rem', |
| 78 | + zIndex: 1, |
| 79 | + }} |
| 80 | + ></IconButton> |
| 81 | + <code {...props}>{props.children}</code> |
| 82 | + </div> |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Override the default anchor tag to open links in a new tab and include specific query parameters |
| 88 | + components.a = ({ ...props }) => { |
33 | 89 | let href = props.href || '' |
34 | 90 | let existingAnchorParams = '' |
35 | 91 | // When we want to include specific query parameters in the URL |
|
0 commit comments