-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.html
117 lines (83 loc) · 3.18 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
/* 获取参数 */
const oParam = function() {
const pathname = "/jawil/github-extension"
const parseParam = pathname.replace(/^\//, '').split('/')
const oParam = {
userName: '',
reposName: '',
branch: ''
}
oParam.userName = parseParam[0]
oParam.reposName = parseParam[1]
oParam.branch = parseParam[3] ? `trees/${parseParam[3]}` : 'trees/master'
return oParam
}()
/* 获取所有的文件名 */
const filePathName = function(callback) {
const API = 'https://api.github.com/repos/'
let parmArr = []
for (let attr in oParam) {
parmArr.push(oParam[attr])
}
parmArr.splice(2, 0, 'git')
const getPathUrl = `${API}${parmArr.join('/')}?recursive=1`
fetch('./data.json', {
method: 'get'
}).then(response => {
return response.json()
}).then(data => {
callback(data.tree, document.body)
})
}(generatePath)
let count = 0
let isHaveTree = false
/* 组合拼装生成想要的嵌套格式 */
function generatePath(files, parent) {
count++
/* 筛选出第一层的tree和blob*/
files.forEach((ele, i) => {
let oOuterLi = document.createElement('li')
const cascading = ele.path.split('/').length
if (count === cascading && ele.type === 'blob') {
oOuterLi.textContent = ele.path
parent.appendChild(oOuterLi)
}
if (ele.type === 'tree' && count === cascading) {
isHaveTree = true
oOuterLi.textContent = ele.path
parent.appendChild(oOuterLi)
let oUl = document.createElement('ul')
oOuterLi.appendChild(oUl)
const newFiles = function() {
let filterArr = []
files.forEach((item, index) => {
if (new RegExp(`^${ele.path}`).test(item.path)) {
const cascad = item.path.split('/').length
if (cascad === count + 1) {
filterArr.push({
path: item.path
})
}
}
})
generatePath(filterArr, oUl)
}()
}
if (!isHaveTree) {
return
}
}
)
}
</script>
</body>
</html>