-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.js
167 lines (148 loc) · 6.41 KB
/
traverse.js
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
const vscode = require("vscode");
const matter = require("gray-matter");
// Recursively build sidebar items for documentation
async function endpointHelper(data, mainUri) {
data = data.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
let children = [];
for (let item of data) {
let name = item[0];
let type = item[1];
name = name.split(".")[0];
// type 2 is directory; type 1 is file
if (type === 2) {
let markdownwithMeta;
try {
let fileUri = vscode.Uri.parse(`${mainUri}/${name}.mdx`);
let fileContent = await vscode.workspace.fs.readFile(fileUri);
let byteArray = Array.from(new Uint8Array(fileContent));
// Replace emdash with `-`; emdash is an extended ASCII character and needs to replaced
for (let i in byteArray) {
if (byteArray[i] === 226) {
byteArray[i] = 45;
}
}
// Filter for 128 characters for Uint8 array of bytes
byteArray = byteArray.filter((byte) => byte <= 127);
const bytesString = String.fromCharCode(...byteArray);
markdownwithMeta = bytesString;
} catch (e) {
vscode.window.showErrorMessage(
"Endpoints couldn't be found in directory"
);
markdownwithMeta = "";
}
try {
const { data: frontMatter, content } = matter(markdownwithMeta);
let childrenUri = vscode.Uri.parse(`${mainUri}/${name}`);
let childrenRes = await vscode.workspace.fs.readDirectory(
childrenUri
);
children.push({
name: frontMatter.sidebar_title,
visible_in_sidebar: frontMatter.visible_in_sidebar,
page_title: frontMatter.page_title,
path: name,
order: frontMatter.order,
children: await endpointHelper(
childrenRes,
`${mainUri}/${name}`
),
});
} catch (e) {
vscode.window.showErrorMessage(
"Endpoints couldn't be found in directory second try" +
`${mainUri}/${name}`
);
}
} else {
if (!children.some((el) => el.path === name)) {
let markdownwithMeta;
try {
let fileUri = vscode.Uri.parse(mainUri + `/${name}.mdx`);
let fileContent = await vscode.workspace.fs.readFile(
fileUri
);
let byteArray = Array.from(new Uint8Array(fileContent));
// Replace emdash with `-`; emdash is an extended ASCII character and needs to replaced
for (let i in byteArray) {
if (byteArray[i] === 226) {
byteArray[i] = 45;
}
}
// Filter for 128 characters for Uint8 array of bytes
byteArray = byteArray.filter((byte) => byte <= 127);
const bytesString = String.fromCharCode(...byteArray);
markdownwithMeta = bytesString;
} catch (e) {
vscode.window.showErrorMessage(
`Endpoints couldn't be found in file ${mainUri}/${name}.mdx`
);
markdownwithMeta = "";
}
const { data: frontMatter, content } = matter(markdownwithMeta);
children.push({
name: frontMatter.sidebar_title,
visible_in_sidebar: frontMatter.visible_in_sidebar,
page_title: frontMatter.page_title,
path: name,
order: frontMatter.order,
});
}
}
}
return children;
}
// Traverse through endpoints to build the sidebar JSON
export async function traverse(endpoints) {
let selectFolder = vscode.workspace.workspaceFolders[0];
for (let endpoint of endpoints["home"]) {
for (let child of endpoint.children) {
let uri = `${selectFolder.uri.scheme}://${selectFolder.uri.authority}${selectFolder.uri.path}/content/${endpoint.path}/${child.path}`;
let res = await vscode.workspace.fs.readDirectory(
vscode.Uri.parse(uri)
);
child["children"] = await endpointHelper(res, uri);
}
}
return endpoints;
}
// Build individual sidebar for preview
export async function customTraverse(path, endpoints) {
let selectFolder = vscode.workspace.workspaceFolders[0];
let bundledEndpoints = { home: [] };
let [categoryPath, productPath] = path.split("/");
let category = endpoints["home"].filter(
(endpoint) => endpoint.path === categoryPath
);
category = category[0];
let children = category.children.filter(
(child) => child.path === productPath
);
// Change uri if virtual workspace
let uri = `${selectFolder.uri.scheme}://${selectFolder.uri.authority}${selectFolder.uri.path}/content/${categoryPath}/${productPath}`;
let res = await vscode.workspace.fs.readDirectory(vscode.Uri.parse(uri));
children[0]["children"] = await endpointHelper(res, uri);
category["children"] = children;
bundledEndpoints["home"].push(category);
return bundledEndpoints;
}
// Fetch endpoints from endpoints.json file in the repo
export async function getEndpoints(isVirtualWorkspace) {
let selectFolder = vscode.workspace.workspaceFolders[0];
let endpointsUri = "";
// Change uri if virtual workspace
if (!isVirtualWorkspace) {
endpointsUri = vscode.Uri.parse(
`${selectFolder.uri.scheme}:/${selectFolder.uri.path}/content/endpoints.json`
);
} else {
endpointsUri = vscode.Uri.parse(
`${selectFolder.uri.scheme}://github${selectFolder.uri.path}/content/endpoints.json`
);
}
let endpoints = await vscode.workspace.fs.readFile(endpointsUri);
let byteArray = Array.from(new Uint8Array(endpoints));
const bytesString = String.fromCharCode(...byteArray);
let endpointsData = JSON.parse(bytesString);
return endpointsData;
}