-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.js
289 lines (240 loc) · 7.6 KB
/
build.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const read = require('ecosystem-docs/read')
const sync = require('ecosystem-docs/sync')
const highlight = require('./highlight')
const map = require('map-limit')
const cheerio = require('cheerio')
const marked = require('marked')
const mkdirp = require('mkdirp')
const ghauth = require('ghauth')
const mdast = require('mdast')
const path = require('path')
const cpr = require('cpr')
const fs = require('fs')
const dataLocation = path.join(__dirname, '.data')
exports.sync = function(packageMarkdown, done) {
const repos = categoriesToRepos(parseCategories(packageMarkdown))
function syncWithToken(token){
sync(repos, {
data: dataLocation,
token, token
}, done);
}
if(process.env.ACCESS_TOKEN){
syncWithToken(process.env.ACCESS_TOKEN);
}else{
console.log("No access token passed need to authenticate...");
ghauth({
configName: 'ecosystem-docs',
userAgent: 'ecosystem-docs',
scopes: ['user']
}, function(err, auth) {
if (err) return done(err)
syncWithToken(auth.token)
})
}
}
exports.make = function(packageMarkdown, options, done) {
options = options || {}
const categories = parseCategories(packageMarkdown)
const repoList = categoriesToRepos(categories)
options.output = options.output || path.join(__dirname, 'dist')
read(repoList, {
data: dataLocation
}, function(err, repos) {
if (err) return done(err)
const contribs = indexContributors(repos)
reattachCategories(repos, categories)
map(repos, 20, function(repo, next) {
writeRepoJSON(repo, options, function(err) {
if (err) return next(err)
next(null, {
user: repo.user,
name: repo.name,
stars: repo.stars,
issues: repo.issues,
contrib: repo.contributors,
version: repo.package && repo.package.version || false,
npmName: repo.package && repo.package.name || false,
category: repo.category
})
})
}, function(err, repos) {
if (err) return done(err)
const index = {
repos: reposToCategories(repos),
contributors: contribs
}
fs.writeFile(
path.join(options.output, 'index.json'),
JSON.stringify(index),
copyAssets
)
})
})
function copyAssets(err) {
if (err) return done(err)
cpr(path.join(__dirname, 'static'), options.output, function(err) {
return done(err)
})
}
}
function writeRepoJSON(repo, options, done) {
const dest = path.join(options.output, repo.user, repo.name) + '.json'
const data = {}
mkdirp(path.dirname(dest), function(err) {
if (err) return done(err)
convertReadme(repo, function(err, readme) {
if (err) return done(err)
data.readme = readme
fs.writeFile(dest, JSON.stringify(data), done)
})
})
}
function convertReadme(repo, done) {
const ast = mdast.parse(repo.readme)
// Remove all content before the first heading
for (var i = 0; i < ast.children.length; i++) {
var child = ast.children[i]
if (child.type === 'heading') break
ast.children.splice(i--, 1)
}
// Ensure there's only one <h1> in the document,
// and that <h1/h2>'s that are code-only are at least <h3>
for (; i < ast.children.length; i++) {
var child = ast.children[i]
if (child.type !== 'heading') continue
if (child.depth === 1) child.depth = 2
if (child.depth === 2) {
if (child.children.length !== 1) continue
if (child.children[0].type !== 'inlineCode') continue
child.depth = 3
}
}
marked(mdast.stringify(ast), {
highlight: highlight
}, function(err, html) {
if (err) return done(err)
var $ = cheerio.load(html)
// Remove badges
$('img[src*="://img.shields.io"]').remove()
$('img[src*="://badges.github.io"]').remove()
$('img[src*="://nodei.co"]').remove()
$('img[src*="://david-dm.org"]').remove()
$('img[src*="://badge.fury.io"]').remove()
$('img[src*="://travis-ci.org"]').remove()
$('img[src*="://secure.travis-ci.org"]').remove()
$('h1 img').remove()
// Resolve relative URLs in READMEs for images and anchors
$('a:not([href^=http]):not([href^=#])').each(function(i,el) {
var $a = $(el)
$a.attr('href', 'http://github.com/' + repo.user + '/' + repo.name + '/blob/master/' + $a.attr('href'))
})
$('img:not([src^=http])').each(function(i,el) {
var $img = $(el)
$img.attr('src', 'https://raw.githubusercontent.com/' + repo.user + '/' + repo.name + '/master/' + $img.attr('src'))
})
// Guarantee that the first heading is an <h1>,
// and wrap it up in an <a> link to the repository.
var headings = $('h1, h2, h3, h4, h5, h6')
if (headings && headings[0] && repo.path) {
$(headings[0]).replaceWith($(
'<h1 class="title">'
+ '<a target="_blank" href="https://github.com/'+repo.path+'">'
+ $(headings[0]).text()
+ '</a>'
+ '<div class="contrib"></div>'
+ '</h1>'
))
}
if (!repo.page) return done(null, $.html())
var $iframe = $('<iframe scrolling ="no" seamless ="seamless" src ="'+repo.page+'"></iframe>')
var $img = $('img')
var replaced = false
var headCount = 0
// Try and replace the first image in the first section
// with a gh-pages iframe if present.
$('h1, h2, h3, h4, h5, h6, img').each(function(i, el) {
if (headCount++ !== 1 || el.name !== 'img') return
var $el = $(el)
if ($el.parent()[0].name === 'a') $el = $el.parent()
$el.replaceWith($iframe)
replaced = true
})
if (!replaced) {
if (headings[1]) {
// Insert before the second heading
$(headings[1]).before($iframe)
} else {
// Or insert after the first, if there's only one
$(headings[0]).after($iframe)
}
}
done(null, $.html())
})
}
function indexContributors(repos) {
const contribs = []
const avatars = []
repos.forEach(function(repo) {
repo.contributors = repo.contributors.map(function(user) {
var name = user.login
var idx = contribs.indexOf(name)
if (idx !== -1) return idx
contribs.push(name)
avatars.push(user.avatar_url)
return contribs.length - 1
})
})
return contribs.map(function(d, i) {
return { name: d, image: avatars[i] }
})
}
function parseCategories(packageMarkdown) {
const tree = mdast.parse(String(packageMarkdown))
const repos = {}
var category = null
tree.children.forEach(function(node) {
const type = node.type
if (type === 'heading') {
return category = mdast
.stringify(node)
.replace(/^\#+/g, '')
.trim()
} else
if (type !== 'list' || category === null) {
return
}
repos[category] = node.children.map(function(child) {
return mdast.stringify(child)
})
})
return repos
}
function categoriesToRepos(categories) {
return Object.keys(categories).reduce(function(memo, key) {
const repos = categories[key]
repos.forEach(function(repo) {
repo.category = key
})
return memo.concat(repos)
}, [])
}
function reposToCategories(repos) {
return repos.reduce(function(categories, repo) {
var key = repo.category
delete repo.category
categories[key] = categories[key] || []
categories[key].push(repo)
return categories
}, {})
}
function reattachCategories(repos, categories) {
const keys = Object.keys(categories)
repos.forEach(function(repo) {
keys.forEach(function(key) {
const idx = categories[key].indexOf(repo.path)
if (idx !== -1) repo.category = key
})
})
return repos
}