-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathLongFormPage.tsx
429 lines (415 loc) · 22.9 KB
/
LongFormPage.tsx
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import { Fragment } from "react"
import { Head } from "./Head.js"
import { CitationMeta } from "./CitationMeta.js"
import { SiteHeader } from "./SiteHeader.js"
import { SiteFooter } from "./SiteFooter.js"
import { addContentFeatures, formatUrls } from "../site/formatting.js"
import { SiteSubnavigation } from "./SiteSubnavigation.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { faBook, faSync } from "@fortawesome/free-solid-svg-icons"
import { faCreativeCommons } from "@fortawesome/free-brands-svg-icons"
import { TableOfContents } from "../site/TableOfContents.js"
import { FormattedPost, TocHeading, omit } from "@ourworldindata/utils"
import { FormattingOptions } from "@ourworldindata/types"
import { BreadcrumbsFromSubnav } from "./Breadcrumb/Breadcrumb.js"
import { Byline } from "./Byline.js"
import { PageInfo } from "./PageInfo.js"
import { BackToTopic } from "./BackToTopic.js"
import StickyNav from "./blocks/StickyNav.js"
import { CodeSnippet } from "@ourworldindata/components"
import { formatAuthors } from "./clientFormatting.js"
import { Html } from "./Html.js"
export interface PageOverrides {
pageTitle?: string
pageDesc?: string
canonicalUrl?: string
citationTitle?: string
citationSlug?: string
citationCanonicalUrl?: string
citationAuthors?: string[]
citationPublicationDate?: Date
}
export const LongFormPage = (props: {
withCitation: boolean
post: FormattedPost
overrides?: PageOverrides
formattingOptions: FormattingOptions
baseUrl: string
}) => {
const { withCitation, post, overrides, formattingOptions, baseUrl } = props
const isPost = post.type === "post"
const pageTitle = overrides?.pageTitle ?? post.title
const pageTitleSEO = `${pageTitle}${
post.supertitle ? ` - ${post.supertitle}` : ""
}`
const pageDesc = overrides?.pageDesc ?? post.pageDesc
const canonicalUrl = overrides?.canonicalUrl ?? `${baseUrl}/${post.slug}`
const citationTitle = overrides?.citationTitle ?? pageTitle
const citationSlug = overrides?.citationSlug ?? post.slug
const citationCanonicalUrl = overrides?.citationCanonicalUrl ?? canonicalUrl
const citationPublicationDate =
overrides?.citationPublicationDate ?? post.date
const citationPublishedYear = citationPublicationDate.getFullYear()
const citationAuthors = overrides?.citationAuthors ?? post.authors
let hasSidebar = false
const endNotes = { text: "Endnotes", slug: "endnotes" }
const tocHeadings: TocHeading[] = [...post.tocHeadings]
if (tocHeadings.some((tocHeading) => !tocHeading.isSubheading)) {
hasSidebar = true
if (post.footnotes.length) {
tocHeadings.push({
...endNotes,
isSubheading: false,
})
}
if (withCitation) {
tocHeadings.push(
{
text: "Licence",
slug: "licence",
isSubheading: false,
},
{
text: "Citation",
slug: "citation",
isSubheading: false,
}
)
}
}
const tocHeadingsNoHtml: Array<Omit<TocHeading, "html">> = tocHeadings.map(
(tocHeading) => omit(tocHeading, "html")
)
const bodyClasses = []
if (formattingOptions.bodyClassName) {
bodyClasses.push(formattingOptions.bodyClassName)
}
const citationText = `${formatAuthors({
authors: citationAuthors,
})} (${citationPublishedYear}) - "${citationTitle}". Published online at OurWorldinData.org. Retrieved from: '${citationCanonicalUrl}' [Online Resource]`
const bibtex = `@article{owid${citationSlug.replace(/-/g, "")},
author = {${formatAuthors({
authors: citationAuthors,
forBibtex: true,
})}},
title = {${citationTitle}},
journal = {Our World in Data},
year = {${citationPublishedYear}},
note = {${citationCanonicalUrl}}
}`
return (
<Html>
<Head
pageTitle={pageTitleSEO}
pageDesc={pageDesc}
canonicalUrl={canonicalUrl}
imageUrl={
// uriEncoding is taken care of inside the Head component
post.imageUrl ? formatUrls(post.imageUrl) : undefined
}
baseUrl={baseUrl}
>
{withCitation && (
<CitationMeta
title={citationTitle}
authors={citationAuthors}
date={citationPublicationDate}
canonicalUrl={citationCanonicalUrl}
/>
)}
{post.style && <style>{post.style}</style>}
</Head>
<body className={bodyClasses.join(" ")}>
<SiteHeader baseUrl={baseUrl} />
<main>
<article
className={`page${
hasSidebar ? " with-sidebar" : " no-sidebar"
}${isPost ? " thin-banner" : " large-banner"}`}
>
<div className="offset-header">
<header className="article-header">
{isPost && formattingOptions.subnavId && (
<BackToTopic
subnavId={formattingOptions.subnavId}
/>
)}
<div className="article-titles">
{post.supertitle && (
<div className="supertitle">
{post.supertitle}
</div>
)}
<h1 className="entry-title">{pageTitle}</h1>
{!isPost && formattingOptions.subnavId && (
<BreadcrumbsFromSubnav
subnavId={
formattingOptions.subnavId
}
subnavCurrentId={
formattingOptions.subnavCurrentId
}
/>
)}
</div>
{!isPost && (
<>
{!formattingOptions.hideAuthors && (
<Byline
authors={post.authors}
override={post.byline}
/>
)}
{post.info && (
<PageInfo info={post.info} />
)}
{(withCitation || post.lastUpdated) && (
<div className="tools">
{post.lastUpdated && (
<div className="last-updated">
<FontAwesomeIcon
icon={faSync}
/>
<span
dangerouslySetInnerHTML={{
__html: post.lastUpdated,
}}
/>
</div>
)}
{withCitation && (
<>
<a href="#licence">
<FontAwesomeIcon
icon={
faCreativeCommons
}
/>
Reuse our work
freely
</a>
<a href="#citation">
<FontAwesomeIcon
icon={faBook}
/>
Cite this research
</a>
</>
)}
</div>
)}
</>
)}
</header>
</div>
{post.stickyNavLinks?.length ? (
<nav className="sticky-nav sticky-nav--dark">
<StickyNav links={post.stickyNavLinks} />
</nav>
) : null}
{!isPost && formattingOptions.subnavId && (
<SiteSubnavigation
subnavId={formattingOptions.subnavId}
subnavCurrentId={
formattingOptions.subnavCurrentId
}
/>
)}
<div className="content-wrapper">
{hasSidebar && (
<TableOfContents
headings={tocHeadingsNoHtml}
pageTitle={pageTitle}
// hideSubheadings={true}
/>
)}
<div className="offset-content">
<div className="content-and-footnotes">
<div
className="article-content"
dangerouslySetInnerHTML={{
__html: addContentFeatures({
post,
formattingOptions,
}),
}}
/>
<footer className="article-footer">
<div className="wp-block-columns">
<div className="wp-block-column">
{isPost && post.info && (
<PageInfo
info={post.info}
/>
)}
{post.footnotes.length ? (
<Fragment>
<h3
id={endNotes.slug}
className="h3-bold"
>
{endNotes.text}
</h3>
<ol className="endnotes">
{post.footnotes.map(
(
footnote,
i
) => (
<li
key={i}
id={`note-${
i +
1
}`}
>
<p
dangerouslySetInnerHTML={{
__html: footnote,
}}
/>
</li>
)
)}
</ol>
</Fragment>
) : undefined}
{withCitation && (
<>
<h3
id="citation"
className="h3-bold"
>
Cite this work
</h3>
<p>
Our articles and
data visualizations
rely on work from
many different
people and
organizations. When
citing this topic
page, please also
cite the underlying
data sources. This
topic page can be
cited as:
</p>
<div>
<CodeSnippet
code={
citationText
}
/>
</div>
<p>BibTeX citation</p>
<div>
<CodeSnippet
code={bibtex}
/>
</div>
<hr
style={{
paddingBottom: 0,
}}
/>
</>
)}
{(isPost || withCitation) && (
<>
<h3
id="licence"
className="h3-bold"
>
Reuse this work
freely
</h3>
<p>
All visualizations,
data, and code
produced by Our
World in Data are
completely open
access under the{" "}
<a
href="https://creativecommons.org/licenses/by/4.0/"
target="_blank"
rel="noopener"
>
Creative Commons
BY license
</a>
. You have the
permission to use,
distribute, and
reproduce these in
any medium, provided
the source and
authors are
credited.
</p>
<p>
The data produced by
third parties and
made available by
Our World in Data is
subject to the
license terms from
the original
third-party authors.
We will always
indicate the
original source of
the data in our
documentation, so
you should always
check the license of
any such third-party
data before use and
redistribution.
</p>
<p>
All of{" "}
<a href="/faqs#how-can-i-embed-one-of-your-interactive-charts-in-my-website">
our charts can
be embedded
</a>{" "}
in any site.
</p>
</>
)}
</div>
<div className="wp-block-column"></div>
</div>
</footer>
</div>
</div>
</div>
</article>
</main>
<SiteFooter
hideDonate={formattingOptions.hideDonateFooter}
baseUrl={baseUrl}
/>
<script
type="module"
dangerouslySetInnerHTML={{
__html: `
runTableOfContents(${JSON.stringify({
// headings might contain </script> tags in their
// html property (which break the page JS, see
// #1256). This comes from {{DataValue}}
// (transformed into AnnotatingDataValue) tags used
// within <h3>s.
headings: tocHeadingsNoHtml,
pageTitle,
// hideSubheadings: true
})})
`,
}}
/>
</body>
</Html>
)
}