-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghDirList.ts
More file actions
107 lines (102 loc) · 3.38 KB
/
ghDirList.ts
File metadata and controls
107 lines (102 loc) · 3.38 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
"use strict";
const converter = new showdown.Converter();
declare var showdown: any;
showdown.setFlavor("github");
const fetchHeaders = {
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "a185f5058a1b89eede21d810611ff54860c5f9c3"
}
};
async function findGithubRepoContents(username: string, repo: string) {
const url = "https://api.github.com";
const repos = `/repos/${username}/${repo}/commits`;
let repoFetch = await fetch(`${url}${repos}`, fetchHeaders);
let repoJSON = repoFetch.json();
let repoTreeRecursive = repoJSON.then(tree => {
return `${tree[0].commit.tree.url}?recursive=1`;
});
let repoTreeUrl = await repoTreeRecursive.then(r => {
return r;
});
let fullCommitTree = await fetch(repoTreeUrl, fetchHeaders).then(res =>
res.json()
);
return fullCommitTree.tree;
}
const dirFilter = (response: string[], type: string) => {
return response.filter((responseItem: any) => {
if (responseItem.type === type) {
return `${responseItem.path}`;
}
});
};
const createListElement = (linkArray: any) => {
let listEl = document.createElement("ul");
linkArray.map((links: any) => {
if (links[1].length == 0) {
return (listEl.innerHTML += `<li>
<input type="button" id="${links[0]}">
<label for="${links[0]}">
<a href='/${links[0]}'>${links[0]}</a>
</label>
</li>`);
} else {
let innerLinks: [], templateCollapse;
Promise.all(
links[1].map((url: string, i: number) =>
fetch(url + "/README.md", fetchHeaders).then(resp => resp.text())
)
)
.then((texts: any) => {
return links[1].map((link: string, i: number) => {
return `<li><a href="${link}">${converter.makeHtml(
texts[i]
)}</a></li>`;
});
})
.then((thing: any) => {
templateCollapse = `<li><input type="checkbox" id="${links[0]}">
<label for="${links[0]}">${links[0]}</label>
<ul class="${links[0]}">
${thing.join(" ")}
</ul></li>`;
return (listEl.innerHTML += templateCollapse);
});
}
});
let domDoc = document;
let wrapper = <Element>domDoc.querySelector(".wrapper");
wrapper.innerHTML = "";
wrapper.append(listEl);
};
findGithubRepoContents("atomize", "atomize.github.io")
.then(arr => dirFilter(arr, "tree"))
.then(paths => {
let fetchedPaths = {
rootPaths: <any>{}
};
paths.map((path: any) => {
if (/^\./.test(path.path)) {
return;
}
if (!path.path.includes("/")) {
/* fetchedPaths.rootPaths; */
fetchedPaths.rootPaths[path.path] = [];
} else {
let rootDir = path.path.split("/")[0];
console.log(fetchedPaths.rootPaths[rootDir]);
!fetchedPaths.rootPaths[rootDir]
? (fetchedPaths.rootPaths[rootDir] =
[] &&
fetchedPaths.rootPaths[rootDir].push(
document.location.origin + "/" + path.path
))
: fetchedPaths.rootPaths[rootDir].push(
document.location.origin + "/" + path.path
);
}
}, []);
return Object.entries(fetchedPaths.rootPaths);
})
.then(createListElement);