-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy path_utils.ts
434 lines (398 loc) · 14.4 KB
/
_utils.ts
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
430
431
432
433
434
import {
getCollection,
type CollectionEntry,
type AnyEntryMap,
type ContentEntryMap,
} from "astro:content";
import { defaultLocale, supportedLocales } from "@i18n/const";
import { removeLocalePrefix, startsWithSupportedLocale } from "@i18n/utils";
import type { ReferenceDocContentItem } from "../content/types";
import { load } from "cheerio";
import he from "he";
import { JSDOM } from "jsdom";
import type { JumpToLink, JumpToState } from "../globals/state";
import { categories as referenceCategories } from "../content/reference/config";
import memoize from "lodash/memoize";
import { removeNestedReferencePaths } from "./_utils-node";
interface EntryWithId {
id: string;
}
/**
* Retreives all the entries in the given collection, filtered to only include
* those in the default locale (language).
*
* @param collectionName
* @returns
*/
export const getCollectionInDefaultLocale = async <C extends keyof AnyEntryMap>(
collectionName: C,
): Promise<CollectionEntry<C>[]> =>
await getCollection(collectionName, (entry: unknown) => {
const { id } = entry as EntryWithId;
return id.startsWith(`${defaultLocale}/`);
});
/**
* Retreives all the entries in the given collection for a given locale, and
* includes entries in the default locale for entries that aren't localized
*
* @param collectionName
* @param locale
* @returns
*/
export const getCollectionInLocaleWithFallbacks = memoize(async <
C extends keyof AnyEntryMap,
>(
collectionName: C,
locale: string,
): Promise<CollectionEntry<C>[]> => {
const localizedEntries = await getCollectionInLocale(collectionName, locale);
const defaultLocaleCollection =
await getCollectionInDefaultLocale(collectionName);
const filteredDefaultEntries = defaultLocaleCollection.filter(
(defaultEntry) => {
const { id: defaultLocaleId } = defaultEntry as EntryWithId;
return !localizedEntries.some((localeEntry: unknown) => {
const { id: localeId } = localeEntry as EntryWithId;
return (
removeLocalePrefix(localeId) === removeLocalePrefix(defaultLocaleId)
);
});
},
);
// Merge the locale entries with the filtered default entries
return [...localizedEntries, ...filteredDefaultEntries];
}, (...args) => args.join("_"));
/**
* Retrieves all the entries in the given collection, filtered to only include
* those in *non-default* locales (languages).
*
* @param collectionName
* @returns
*/
export const getCollectionInNonDefaultLocales = async <
C extends keyof AnyEntryMap,
>(
collectionName: C,
): Promise<CollectionEntry<C>[]> =>
await getCollection(collectionName, (entry: unknown) => {
const { id } = entry as EntryWithId;
return startsWithSupportedLocale(id);
});
/**
* Retrieves all the entries in the given collection, filtered to only include
* those in a the given *non-default* locale (language).
*
* @param collectionName
* @param locale
* @returns
*/
export const getCollectionInLocale = async <C extends keyof AnyEntryMap>(
collectionName: C,
locale: string,
): Promise<CollectionEntry<C>[]> =>
await getCollection(collectionName, (entry: unknown) => {
const { id } = entry as EntryWithId;
return id.startsWith(`${locale}/`);
});
/**
* Gets related entries from a collection utilizing our locale fallback logic.
* Astro doesn't do this for us when it constructs the entries at the route level,
* so we need to backfill this information in the page itself.
*
* @param collectionName
* @param locale
* @param relatedSlugs
* @returns
*/
export const getRelatedEntriesinCollection = async <
C extends keyof ContentEntryMap,
>(
collectionName: C,
locale: string,
relatedSlugs: string[],
): Promise<CollectionEntry<C>[]> => {
const collection = await getCollectionInLocaleWithFallbacks(
collectionName,
locale,
);
const foundEntries = relatedSlugs.map((relatedSlug) =>
collection.find(
(collectionItem) =>
removeLocaleAndExtension(collectionItem.slug) ===
removeLocaleAndExtension(relatedSlug),
),
);
// silly typescript isn't understanding filter
return foundEntries.filter((el) => el !== undefined) as CollectionEntry<C>[];
};
/**
* Astro automatically uses the directory structure for slug information
* Historically the p5 website has used a different structure for example file vs. webpage routing
* This function transforms the Astro slug to the appropriate webpage route to avoid breaking
* Any inbound legacy links
*/
export const exampleContentSlugToLegacyWebsiteSlug = (slug: string): string =>
slug
// First transformation: Remove any locale prefix.
.replace(/^[\w-]+?\//, "") // Remove locale prefix
// Second transformation: Convert slugs built from local dev path to the legacy format.
// For example, "123_topicA/456_topicB/description" becomes "topicA-topicB.html".
.replace(/\d+_(.*?)\/\d+_(.*?)\/description$/, "$1-$2")
// Third transformation: Replace all remaining underscores in the slug with hyphens.
.replace(/_/g, "-");
export const getExampleCategory = (slug: string): string =>
slug.split("/")[1].split("_").splice(1).join(" ");
export const normalizeReferenceRoute = (route: string): string =>
removeNestedReferencePaths(removeLocaleAndExtension(route));
export const removeLocaleAndExtension = (id: string): string =>
removeContentFileExt(removeLeadingSlash(removeLocalePrefix(id)));
export const removeLeadingSlash = (path: string): string =>
path.replace(/^\//, "");
/**
* We cannot use Astro's default slug because it removes characters like '.'
* We use the id instead and remove the file extension
*/
export const removeContentFileExt = (id: string): string =>
id.replace(/\.(mdx?|ya?ml)$/, "");
/* We have to modify the Astro.js slug to match existing routing */
/* This is done dynamically here instead of relying on example authors */
/* to update their slugs in the MDX Content Entry */
export const transformExampleSlugs = <C extends keyof ContentEntryMap>(
exampleCollection: CollectionEntry<C>[],
): CollectionEntry<C>[] => {
const transformedEntries = exampleCollection.map((entry) => ({
...entry,
slug: exampleContentSlugToLegacyWebsiteSlug(entry.slug),
}));
return transformedEntries;
};
/**
* Returns the correct URL to link to for a library entry
* @param library
* @returns
*/
export const getLibraryLink = (library: CollectionEntry<"libraries">) =>
library.data.websiteUrl ?? library.data.sourceUrl;
/**
* Some reference examples have multiple examples in one string separated by <div></div>
* This function separates the examples into individual strings
* @param examples Reference example strings from MDX
* @returns The examples separated into individual strings
*/
// separateReferenceExamples
export const parseReferenceExamplesAndMetadata = (examples: string[]): { src: string, classes: Record<string, any> }[] =>
examples
?.flatMap((example: string) => example.split("</div>"))
.map((src: string) => {
const matches = [...src.matchAll(/<div class=['"]([^"']*)['"]>/g)]
const classes: Record<string, boolean> = {}
for (const match of matches) {
const tokens = match[1].split(/\s+/g)
for (const token of tokens) {
classes[token] = true
}
}
return { classes, src }
})
.map(({ src, classes }) => ({ classes, src: src.replace(/<\/?div[^>]*>|<\/?code>/g, "") }))
.filter(({ src }) => src);
/**
* Returns the title concatenated with parentheses if the reference entry is a constructor or method
* This could be handled in the reference parsing and authoring process instead
* @param referenceEntry Reference entry
* @returns The title concatenated with parentheses if the reference entry is a constructor or method
*/
export const getRefEntryTitleConcatWithParen = (
referenceEntry: ReferenceDocContentItem,
) =>
`${referenceEntry.data.title}${referenceEntry.data.itemtype === "method" ? "()" : ""}`;
/* Function to escape HTML content within <code> tags
* @param htmlString String with HTML content
* @returns String with HTML content where the content inside <code> tags is escaped
*/
export const escapeCodeTagsContent = (htmlString: string): string => {
// Load the HTML string into Cheerio
const $ = load(htmlString);
// Loop through all <code> tags
$("code").each(function () {
// Don't escape code in multiline blocks, as these will already
// be escaped
if ($(this).parent().prop('tagName') === 'PRE') return;
// Get the current text and HTML inside the <code> tag
const currentHtml = $(this).html() ?? "";
// Use he to escape HTML entities
const escapedHtml = he.escape(currentHtml);
// Update the <code> tag content with the escaped HTML
$(this).html(escapedHtml);
});
// Return the modified HTML as a string
return $.html();
};
export const getPaginationMax = (numPerPage: number, numItems: number) =>
Math.ceil(numItems / numPerPage);
export type PageTopic =
| "community"
| "reference"
| "contribute"
| "about"
| "examples"
| "tutorials";
export const getTopicInfo = (topic?: PageTopic) => {
switch (topic) {
case "community":
return { name: "Community", url: "/community" };
case "reference":
return { name: "Reference", url: "/reference" };
case "contribute":
return { name: "Contribute", url: "/contribute" };
case "about":
return { name: "About", url: "/about" };
case "examples":
return { name: "Examples", url: "/examples" };
case "tutorials":
return { name: "Tutorials", url: "/tutorials" };
default:
return { name: "p5.js", url: "/" };
}
};
/**
* Capitalize the first letter of a string
* (really only makes sense for English strings)
*
* @param str
* @returns
*/
export const capitalize = (str: string): string =>
str ? str[0].toUpperCase() + str.slice(1) : "";
// Function to decode HTML content and strip HTML tags
export const decodeHtml = (html: string) => {
// Create a new JSDOM instance with the provided HTML
const dom = new JSDOM(html);
const document = dom.window.document;
// Extract text content from the parsed HTML
const textContent = document.body.textContent || "";
return textContent.trim(); // remove blank space at the beginning
};
/**
* Generate jumpToLinks for an entire category of collection entries
* Highlight the currently viewed entry
* @param collectionType The type of collection
* @param currentEntrySlug The id of the currently viewed entry
* @param jumpToHeading The heading for the jumpToLinks
* @param t Pass in the result from getUiTranslator()
* @param currentLocale The current locale to translate the header with
* @returns JumpToState object
*/
export const generateJumpToState = async (
collectionType: keyof ContentEntryMap,
currentEntrySlug: string,
jumpToHeading: string,
t: (...args: string[]) => string | Record<string, any>,
currentLocale: (typeof supportedLocales)[number],
): Promise<JumpToState> => {
// Get all entries in the collection in the default locale
// We can use the default locale because the links are automatically
// prefixed and there is fallback to the default locale
const localeEntries = await getCollectionInLocaleWithFallbacks(
collectionType,
currentLocale,
);
// Get categories for the collection
let categories: Set<string> | undefined;
// Get the categories based on the collection type
switch (collectionType) {
case "reference":
categories = new Set(referenceCategories);
break;
case "tutorials":
// @ts-expect-error - We know that the category exists because of the collection type
categories = new Set(localeEntries.map((entry) => entry.data.category));
break;
case "examples":
categories = new Set(
localeEntries.map((entry) => getExampleCategory(entry.slug)),
);
break;
default:
break;
}
const jumpToLinks = [] as JumpToLink[];
// Function to get the label for a category, these are different for each collection type
const getCategoryLabel = (category: string) => {
switch (collectionType) {
case "reference":
return t("referenceCategories", "modules", category) as string;
case "tutorials":
return t("tutorialCategories", category) as string;
case "examples":
return t("exampleCategories", category) as string;
default:
return "";
}
};
// Loop through each category and add entries to the jumpToLinks
for (const category of categories ?? []) {
const categoryLinks = [] as JumpToLink[];
categoryLinks.push({
label: getCategoryLabel(category),
url: `/${collectionType}#${category}`,
current: false,
});
// Examples are a special case where subentries are only shown if they are in the current category
if (
collectionType !== "examples" ||
category === getExampleCategory(currentEntrySlug)
) {
// Get all entries in the current category
const currentCategoryEntries = localeEntries.filter(
(entry) =>
category ===
(collectionType === "examples"
? getExampleCategory(entry.slug)
: // @ts-expect-error - We know that the category exists because of the collection type
entry.data.category ?? ""),
);
// Add the entries in the category to the jumpToLinks
categoryLinks.push(
...currentCategoryEntries.map(
(entry) =>
({
label: entry.data.title,
url: getUrl(entry, collectionType),
size: "small",
current:
removeLocalePrefix(entry.slug) ===
removeLocalePrefix(currentEntrySlug),
}) as JumpToLink,
),
);
}
const hasCurrent = categoryLinks.some((link) => link.current);
// If the current entry is in this category, move this category to the top
if (hasCurrent) {
jumpToLinks.unshift(...categoryLinks);
} else {
jumpToLinks.push(...categoryLinks);
}
}
// Return the JumpToState object
return {
heading: t(jumpToHeading) as string,
links: jumpToLinks,
};
};
const getUrl = (
entry: CollectionEntry<keyof ContentEntryMap>,
collectionType: keyof ContentEntryMap,
) => {
switch (collectionType) {
case "reference":
return `/reference/${entry.slug}`;
case "tutorials":
return `/tutorials/${removeLocalePrefix(entry.slug)}`;
case "examples":
return `/examples${exampleContentSlugToLegacyWebsiteSlug(removeLocalePrefix(entry.slug))}`;
default:
return "";
}
};