forked from solidjs/solid-docs-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
122 lines (106 loc) · 3.54 KB
/
index.ts
File metadata and controls
122 lines (106 loc) · 3.54 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
import supported from "../build/out/supported.json"
import {DocFile, LessonFile, LessonLookup, StringKeyed, ResourceMetadata} from "./types";
export { supported, ResourceMetadata, DocFile, LessonFile, LessonLookup }
function traversePath(resourcePath: string[]): StringKeyed | false | string[]{
let cursor = supported;
for (const part of resourcePath) {
// @ts-ignore
cursor = cursor[part];
if (!cursor) {
return false;
}
}
return cursor;
}
export async function getGuides(lang: string, defaultToEnglish = false): Promise<ResourceMetadata[] | undefined> {
const supported = getSupported("guides", lang);
if (Array.isArray(supported) && supported.length) {
const metadata = (await import(`../build/out/docs/${lang}/guides/_metadata.json`)).default as {
[resource: string]: {
sort: number,
title: string,
description: string }
};
if (metadata) {
return Object.entries(metadata)
.filter( ([resource, metadata]) => metadata.title)
.sort((a, b) => (a[1].sort - b[1].sort))
.map( ([resource, {description, title}]) => ({
resource: "guides/" + resource,
description, title
}))
}
}
return defaultToEnglish ? getGuides("en") : []
}
export function getSupported(resourcePath: string, lang?: string) {
const cursor = traversePath(resourcePath.split('/'));
if (!cursor) {
return false;
}
if (Array.isArray(cursor)) {
if (lang) {
return cursor.includes(lang);
}
return cursor;
}
if (!lang) { return false; }
const keys = Object.keys(cursor);
if (!keys.length) return false;
if (keys.length) {
return keys.filter(key => {
if (!Array.isArray(cursor[key])) return false;
return (cursor[key] as string[]).includes(lang);
});
}
}
export function getAllResourcePaths(lang: string) {
const paths: string[] = [];
const traverse = (resourcePath: string[], cursor: StringKeyed | string[]) => {
if (Array.isArray(cursor)) {
if (cursor.includes(lang)) {
paths.push(resourcePath.join('/'));
}
return;
}
for (const [key, value] of Object.entries(cursor)) {
traverse(resourcePath.concat(key), value);
}
};
traverse([], supported);
return paths;
}
export async function getDoc(lang: string, resource: string): Promise<DocFile | false> {
const resourcePath = resource.split('/');
const cursor = traversePath(resourcePath);
if (!Array.isArray(cursor) || !cursor.includes(lang)) {
return false;
}
// We have to have each depth explicitly for rollup dynamic imports to work
if (resourcePath.length == 1) {
return (await import(`../build/out/docs/${lang}/${resourcePath[0]}.json`)).default as DocFile;
}
if (resourcePath.length == 2) {
return (await import(`../build/out/docs/${lang}/${resourcePath[0]}/${resourcePath[1]}.json`)).default as DocFile;
}
return false;
}
export async function getTutorial(lang: string, lesson: string): Promise<LessonFile | false> {
const cursor = traversePath(['tutorials', lesson]);
if (!Array.isArray(cursor) || !cursor.includes(lang)) {
return false;
}
const lessonFile = (await import(`../build/out/tutorials/${lang}/${lesson}.json`)).default as LessonFile;
return lessonFile;
return false;
}
export async function getTutorialDirectory(lang: string): Promise<LessonLookup[] | false> {
const directory = (await import(`../build/out/tutorials/${lang}/directory.json`));
if (directory?.default) {
return directory.default;
}
if (directory) {
return directory;
}
return false;
}