-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathReferenceItemLayout.astro
More file actions
321 lines (306 loc) · 10.7 KB
/
ReferenceItemLayout.astro
File metadata and controls
321 lines (306 loc) · 10.7 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
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
---
import CodeEmbed from "@components/CodeEmbed";
import { getCurrentLocale, getUiTranslator } from "@i18n/utils";
import Head from "@components/Head/index.astro";
import {
getRefEntryTitleConcatWithParen,
escapeCodeTagsContent,
parseReferenceExamplesAndMetadata,
normalizeReferenceRoute,
getRelatedEntriesinCollection,
generateJumpToState,
} from "@pages/_utils";
import BaseLayout from "./BaseLayout.astro";
import RelatedItems from "@components/RelatedItems/index.astro";
import MethodSignature from "@components/MethodSignature/index.astro";
import type {
ReferenceOverload,
ReferenceParam,
} from "@/types/parsers.interface";
import { setJumpToState } from "../globals/state";
import { p5Version } from "../globals/p5-version";
import flask from "@src/content/ui/images/icons/flask.svg?raw";
import warning from "@src/content/ui/images/icons/warning.svg?raw";
import OutdatedTranslationBanner from "@components/OutdatedTranslationBanner/index.astro";
import { checkTranslationBanner } from "../utils/translationBanner";
const { entry, relatedEntries } = Astro.props;
const currentLocale = getCurrentLocale(Astro.url.pathname);
const examples = parseReferenceExamplesAndMetadata(entry.data.example)
// Remove empty lines at the beginning and end of the examples
?.map((example) => ({ ...example, src: example.src.trim() }));
const description = escapeCodeTagsContent(entry.data.description);
const t = await getUiTranslator(currentLocale);
const title = getRefEntryTitleConcatWithParen(entry);
const jumpToState = await generateJumpToState(
"reference",
entry.id,
"Reference",
t,
currentLocale
);
// make related entry links
const relatedEntriesLinks = relatedEntries.map((relatedEntry: any) => {
return {
label: getRefEntryTitleConcatWithParen(relatedEntry),
url: `/reference/${normalizeReferenceRoute(relatedEntry.id)}`,
size: "small",
current: relatedEntry.id === entry.id,
};
});
// Add link for submodule itself
relatedEntriesLinks.unshift({
label: entry.data.submodule,
url: `/reference/#${entry.data.submodule}`,
});
// Put related links at top of the list
jumpToState.links?.unshift(...relatedEntriesLinks);
setJumpToState(jumpToState);
// get whatever related entries might be specified in the collection entry data
const relatedReferencesFromEntryData =
entry.data.relatedContent?.references !== undefined
? await getRelatedEntriesinCollection(
"reference",
currentLocale,
entry.data.relatedContent.references.map((r: any) => r.slug)
)
: [];
// then make a list of four, backfilled with entries from the jumpTo links
const relatedReferences = [
...relatedReferencesFromEntryData,
...relatedEntries,
].slice(0, 4);
const seenParams: Record<string, true> = {};
const descriptionParts = description.split(
/(<pre><code class="language-js example">[\s\S]+?<\/code><\/pre>)/gm
);
const { showBanner, englishUrl } = checkTranslationBanner(
'reference',
entry.id,
currentLocale,
Astro.url.pathname,
Astro.url.origin
);
// Normalizes malformed p5.* reference anchors (e.g. "#/p5.Element" or "#/p5/rectMode")
// to proper reference routes ("/reference/p5/p5.Element/" or "/reference/p5/rectMode/")
function normalizeP5ReferenceLinks(html: string | undefined): string | undefined {
if (!html) return html;
return html.replace(
/href="#\/p5([.\/][^"]+)"/g,
'href="/reference/p5$1/"'
);
}
---
<Head title={entry.data.title} locale={currentLocale} />
<BaseLayout
title={title}
titleClass={`reference_${title.toLowerCase()}`}
subtitle={null}
variant="item"
topic="reference"
className="reference-item"
>
{showBanner && <OutdatedTranslationBanner englishUrl={englishUrl} locale={currentLocale} />}
<div class="content-grid mt-0 max-w-[770px]">
<div class="col-span-9 xl:min-w-[1000px]">
{entry.data.beta && (
<div class="experimental">
<h5>
<div
class="inline-block mr-2 w-[20px] h-[20px] mb-[-2px]"
set:html={flask}
/>
{t('experimentalApi', 'title')}
</h5>
<p>{t('experimentalApi', 'description')}</p>
</div>
)}
{entry.data.deprecated && (
<div class="deprecated">
<h5>
<div
class="inline-block mr-2 w-[20px] h-[20px] mb-[-2px]"
set:html={warning}
/>
{t('calloutTitles', 'Warning')}
</h5>
<p set:html={entry.data.deprecated} />
</div>
)}
{descriptionParts.map((part) => {
if (part.startsWith('<pre')) {
const exampleCode = part
.replace(/<pre><code class="language-js example">/, '')
.replace(/<\/code><\/pre>/, '');
return (
<CodeEmbed
client:load
initialValue={exampleCode}
previewable
editable
lazyLoad={false}
allowSideBySide={true}
includeSound={entry.data.module === 'p5.sound'}
/>
)
} else {
return (
<div
set:html={part}
class="[&_a]:text-type-magenta-dark [&_a]:!decoration-type-magenta-dark mb-xl reference-item rendered-markdown"
/>
);
}
})}
{
examples && (
<div class="mb-xl">
<h2 class="text-h3">{t("Examples")}</h2>
{examples.map(({ src: exampleCode, classes }, i: number) => {
return (
<CodeEmbed
client:load
initialValue={exampleCode}
previewable={!classes.norender}
editable
lazyLoad={false}
allowSideBySide={true}
includeSound={entry.data.module === 'p5.sound'}
/>
);
})}
</div>
)
}
{
(entry.data.params || entry.data.overloads) && (
<>
<div class="mb-xl">
<h2 class="text-h3">{t("Syntax")}</h2>
<MethodSignature
params={entry.data.params}
overloads={entry.data.overloads}
name={entry.data.title}
/>
</div>
<div class="mb-xl">
<h2 class="mb-md text-h3">{t("Parameters")}</h2>
{entry.data.params &&
entry.data.params.map((param: ReferenceParam) => (
<div class="grid grid-cols-6 gap-gutter-md text-body">
<span class="col-span-1 text-body whitespace-normal break-words overflow-wrap-break-word">{param.name}</span>
<div
class="col-span-5 [&_p]:m-0 [&_p]:inline [&_a]:underline"
>
{param.type && <span>{param.type}: </span>}
<span set:html={normalizeP5ReferenceLinks(param.description)} />
</div>
</div>
))}
{entry.data.overloads &&
entry.data.overloads.map((overload: ReferenceOverload) =>
overload.params.map((param: ReferenceParam) => {
if (seenParams[param.name]) return <></>;
seenParams[param.name] = true;
return (
<div class="grid grid-cols-6 gap-gutter-md text-body">
<span class="col-span-1">{param.name}</span>
<div
class="col-span-5 [&_p]:m-0 [&_p]:inline [&_a]:underline"
>
{param.type && <span>{param.type}: </span>}
<span set:html={normalizeP5ReferenceLinks(param.description)} />
</div>
</div>
)
})
)}
</div>
</>
)
}
{
(entry.data.return) && (
<>
<div class="mb-xl">
<h2 class="mb-md text-h3">{t("Returns")}</h2>
<div class="grid grid-cols-6 gap-gutter-md text-body">
<div
class="col-span-5 [&_p]:m-0 [&_p]:inline [&_a]:underline"
>
{entry.data.return.type && <span>{entry.data.return.type}: </span>}
<span set:html={normalizeP5ReferenceLinks(entry.data.return.description)} />
</div>
</div>
</div>
</>
)
}
{
entry.data.properties && (
<div class="mb-xl">
<h2 class="mb-md text-h3">{t("Fields")}</h2>
{Object.entries(entry.data.properties).map(([key, value]) => {
const propertyValue = value as {
description: string;
path: string;
};
return (
<div class="text-body my-lg">
<a
href={`/reference/${propertyValue.path}`}
class="text-body-large underline"
>
{key}
</a>
<div class="mt-xxs [&_p]:mb-1 [&_p]:mt-0" set:html={propertyValue.description} />
</div>
);
})}
</div>
)
}
{
entry.data.methods && (
<div class="my-xl">
<h2 class="text-h3">{t("Methods")}</h2>
{Object.entries(entry.data.methods).map(([key, value]) => {
const methodValue = value as {
description: string;
path: string;
};
return (
<div class="text-body my-lg">
<a
href={`/reference/${methodValue.path}`}
class="text-body-large"
>
{key}
</a>
<div class="mt-xxs [&_p]:mb-1 [&_p]:mt-0" set:html={methodValue.description} />
</div>
);
})}
</div>
)
}
{
entry.data.file && entry.data.line &&(
<div class="my-xl">
<div class="text-body [&_a]:text-type-magenta-dark [&_a]:!decoration-type-magenta-dark my-lg">
This page is generated from the comments in
<a
href={`https://github.com/processing/p5.js/blob/v${p5Version}/${entry.data.file}#L${entry.data.line}`}
>
{entry.data.file}
</a>. Please feel free to edit it and submit a pull request!
</div>
</div>
)
}
</div>
</div>
<RelatedItems
title={t("Related References") as string}
items={relatedReferences}
/>
</BaseLayout>