-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathUnrenderedMarkdownContent.tsx
More file actions
86 lines (83 loc) · 2.8 KB
/
UnrenderedMarkdownContent.tsx
File metadata and controls
86 lines (83 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import ReactMarkdown from 'react-markdown'
import type { Components } from 'react-markdown'
import cx from 'classnames'
import remarkGfm from 'remark-gfm'
export type MarkdownContentPropsT = {
children: string
className?: string
openLinksInNewTab?: boolean
includeQueryParams?: boolean
eventGroupKey?: string
eventGroupId?: string
as?: keyof JSX.IntrinsicElements
tabIndex?: number
}
// For content that comes in a Markdown string
// e.g. a GPT Response
export const UnrenderedMarkdownContent = ({
children,
className,
openLinksInNewTab = true,
includeQueryParams = true,
eventGroupKey = '',
eventGroupId = '',
...restProps
}: MarkdownContentPropsT) => {
// Overrides for ReactMarkdown components
const components = {} as Components
// eslint-disable-next-line @typescript-eslint/no-unused-vars
components.a = ({ node, ...props }) => {
let href = props.href || ''
let existingAnchorParams = ''
// When we want to include specific query parameters in the URL
if (includeQueryParams) {
if (href.includes('?')) {
href = href.split('?')[0]
existingAnchorParams = href.split('?')[1]
}
// Include feature, search-overlay-ask-ai, and search-overlay-input query parameters if they exist in the current URL
const existingURLParams = new URLSearchParams(window.location.search)
const newParams = new URLSearchParams()
if (existingURLParams.get('feature')) {
newParams.set('feature', existingURLParams.get('feature') || '')
}
if (existingURLParams.get('search-overlay-ask-ai')) {
newParams.set('search-overlay-ask-ai', existingURLParams.get('search-overlay-ask-ai') || '')
}
if (existingURLParams.get('search-overlay-input')) {
newParams.set('search-overlay-input', existingURLParams.get('search-overlay-input') || '')
}
// Combine new and existing query parameters
if (newParams.toString()) {
href = `${href}?${existingAnchorParams}&${newParams.toString()}`
}
}
return (
<a
{...props}
href={href}
target={openLinksInNewTab ? '_blank' : undefined}
rel={openLinksInNewTab ? 'noopener noreferrer' : undefined}
onClick={(e) => {
// For some reason we need to override the default onClick to get these links to open in a new tab
if (openLinksInNewTab) {
e.stopPropagation()
e.preventDefault()
window.open(href, '_blank')
}
}}
data-group-key={eventGroupKey}
data-group-id={eventGroupId}
>
{props.children}
</a>
)
}
return (
<div className={cx('markdown-body', className)}>
<ReactMarkdown remarkPlugins={[remarkGfm]} {...restProps} components={components}>
{children}
</ReactMarkdown>
</div>
)
}