-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathArticleHTML.tsx
More file actions
242 lines (198 loc) · 5.59 KB
/
ArticleHTML.tsx
File metadata and controls
242 lines (198 loc) · 5.59 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { ContextModule } from "@artsy/cohesion"
import { Box, type BoxProps, Flex, THEME } from "@artsy/palette"
import { themeGet } from "@styled-system/theme-get"
import { FollowArtistButtonQueryRenderer } from "Components/FollowButton/FollowArtistButton"
import { FollowProfileButtonQueryRenderer } from "Components/FollowButton/FollowProfileButton"
import { toStyle } from "Utils/toStyle"
import { type FC, useEffect, useState } from "react"
import styled from "styled-components"
import { ArticleTooltip, isSupportedArticleTooltip } from "./ArticleTooltip"
interface ArticleHTMLProps extends BoxProps {
children: string
}
export const ArticleHTML: FC<React.PropsWithChildren<ArticleHTMLProps>> = ({
children,
...rest
}) => {
// Looks for links and if they are internal and a supported entity type,
// inserts the relevant tooltip.
const transform = (node: Element, i: number) => {
if (node.tagName !== "A") return
const { href } = node as HTMLAnchorElement
try {
const uri = new URL(href)
if (!uri.hostname.includes("artsy.net")) return
const [_, entity, id] = uri.pathname.split("/")
if (!isSupportedArticleTooltip(entity)) return
const heading = node.closest("h2")
const isArtistHeading =
entity === "artist" && isEligibleFollowHeading(heading, entity)
const isPartnerHeading =
entity === "partner" && isEligibleFollowHeading(heading, entity)
if (isArtistHeading) {
return (
<Flex alignItems="center" gap={1} key={[i, id].join("-")}>
<ArticleTooltip entity={entity} id={id} href={href}>
{node.textContent}
</ArticleTooltip>
<FollowArtistButtonQueryRenderer
id={id}
size={["large", "small"]}
contextModule={ContextModule.artistHeader}
/>
</Flex>
)
}
if (isPartnerHeading) {
return (
<Flex alignItems="center" gap={1} key={[i, id].join("-")}>
<ArticleTooltip entity={entity} id={id} href={href}>
{node.textContent}
</ArticleTooltip>
<FollowProfileButtonQueryRenderer
id={id}
size={["large", "small"]}
contextModule={ContextModule.partnerHeader}
/>
</Flex>
)
}
return (
<ArticleTooltip
key={[i, id].join("-")}
entity={entity}
id={id}
href={href}
>
{node.textContent}
</ArticleTooltip>
)
} catch {
return
}
}
const [transformed, setTransformed] = useState<string | null>(null)
// biome-ignore lint/correctness/useExhaustiveDependencies: ignored using `--suppress`
useEffect(() => {
// Relies on the DOMParser global being available in the browser.
import("@artsy/react-html-parser").then(({ default: reactHtmlParser }) => {
setTransformed(reactHtmlParser(children, { transform }))
})
}, [children])
if (transformed) {
return <Container {...rest}>{transformed}</Container>
}
return <Container dangerouslySetInnerHTML={{ __html: children }} {...rest} />
}
export const hasConflictingAdjacentEntityLinks = (
heading: Element | null,
originalEntity: string,
): boolean => {
if (!heading) return false
let sibling = heading.nextElementSibling
while (sibling && sibling.tagName === "H3") {
const entityLinks = sibling.querySelectorAll(
"a[href*='/artist/'], a[href*='/partner/']",
)
for (const link of Array.from(entityLinks)) {
const href = (link as HTMLAnchorElement).href
const isArtist = href.includes("/artist/")
const isPartner = href.includes("/partner/")
if (
(originalEntity === "artist" && isArtist) ||
(originalEntity === "partner" && isPartner)
) {
return true
}
}
sibling = sibling.nextElementSibling
}
return false
}
export const isEligibleFollowHeading = (
heading: Element | null,
entity: string,
): boolean => {
if (!heading || heading.tagName !== "H2") return false
if (hasConflictingAdjacentEntityLinks(heading, entity)) return false
const hasComma = Array.from(heading.childNodes).some(node => {
return node.nodeType === Node.TEXT_NODE && node.textContent?.includes(",")
})
if (hasComma) return false
const expectedPrefix =
entity === "artist"
? "https://www.artsy.net/artist/"
: "https://www.artsy.net/partner/"
const links = heading.querySelectorAll(`a[href^='${expectedPrefix}']`)
return links.length === 1
}
const Container = styled(Box)`
h1,
h2,
h3,
h4,
h5,
p,
blockquote,
pre,
hr {
margin: ${themeGet("space.2")} auto;
&:first-child {
margin-top: 0;
}
&:last-child {
margin-bottom: 0;
}
}
h1,
h2,
h3 {
margin: ${themeGet("space.2")} auto;
}
h1 {
${toStyle({ ...THEME.textVariants.xxl })}
}
h2 {
${toStyle({ ...THEME.textVariants.xl })}
}
h3 {
${toStyle({ ...THEME.textVariants["lg-display"] })}
}
ul {
list-style: disc;
}
ul,
ol,
li {
margin: ${themeGet("space.2")} auto;
&:first-child {
margin-top: 0;
}
&:last-child {
margin-bottom: 0;
}
}
ul,
ol {
margin-left: ${themeGet("space.2")};
}
p,
li {
${toStyle({ ...THEME.textVariants.md })}
}
a {
transition: color 250ms;
text-decoration: underline;
&:hover {
color: ${themeGet("colors.brand")};
}
}
hr {
height: 1px;
border: 0;
background-color: ${themeGet("colors.mono10")};
}
blockquote {
${toStyle({ ...THEME.textVariants.bq })}
}
`