-
Notifications
You must be signed in to change notification settings - Fork 220
/
+page.ts
74 lines (64 loc) · 2.38 KB
/
+page.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
import { base } from '$app/paths';
import type { Tutorial } from '$markdoc/layouts/Tutorial.svelte';
const framework_order = ['React', 'Vue', 'SvelteKit', 'Stripe', 'Refine'];
const category_order = [
'Web',
'Mobile and native',
'Server',
'Auth',
'Databases',
'Storage',
'Functions'
];
export async function load() {
const tutorialsGlob = import.meta.glob('./**/step-1/+page.markdoc', {
eager: true
});
const allTutorials = Object.entries(tutorialsGlob)
.map(([filepath, tutorialList]: [string, unknown]) => {
const { frontmatter } = tutorialList as {
frontmatter: Tutorial;
};
const slug = filepath
.replace('./', '')
.replace('/+page.markdoc', '')
.replace('/step-1', '');
const tutorialName = slug.slice(slug.lastIndexOf('/') + 1);
return {
title: frontmatter.title,
framework: frontmatter.framework,
draft: frontmatter.draft || false,
category: frontmatter.category,
href: `${base}/docs/tutorials/${tutorialName}`
};
})
.filter((tutorial) => !tutorial.draft)
.sort((a, b) => {
// Sort by framework order
const frameworkIndexA = framework_order.indexOf(a.framework as string);
const frameworkIndexB = framework_order.indexOf(b.framework as string);
if (frameworkIndexA > frameworkIndexB) {
return 1;
}
if (frameworkIndexA < frameworkIndexB) {
return -1;
}
// Else, sort by title
return a.title.toLowerCase().localeCompare(b.title.toLowerCase());
});
const tutorials = Object.entries(
allTutorials.reduce((acc: { [key: string]: any[] }, item) => {
// If the category does not exist in the accumulator, initialize it
if (!acc[item.category]) {
acc[item.category] = [];
}
// Push the current item into the appropriate category
acc[item.category].push(item);
return acc;
}, {})
).map(([title, tutorials]) => ({ title, tutorials }));
tutorials.sort((a, b) => category_order.indexOf(a.title) - category_order.indexOf(b.title));
return {
tutorials
};
}