-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathregenerate.js
More file actions
55 lines (47 loc) · 1.19 KB
/
Copy pathregenerate.js
File metadata and controls
55 lines (47 loc) · 1.19 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
var tree = require('markdown-tree')
var cheerio = require('cheerio')
var marked = require('marked')
var unhtml = require('unhtml')
var path = require('path')
var fs = require('fs')
console.error()
console.error('extracting examples')
var examples = tree(
fs.readFileSync(path.join(__dirname, 'examples.md'), 'utf8')
).children[0]
.children
.map(function(example, i) {
var tokens = example.tokens.slice()
tokens.links = {}
var thumb = 'default.jpg'
var $ = cheerio.load(
marked.Parser.parse(tokens)
)
$('img').each(function(i, img) {
var $img = $(img)
thumb = $img.attr('src')
$img.parent().remove()
})
var html = $.html()
var head = cheerio.load(marked(example.text))
var link = head('a')
var feat = !!head('em').length
var name = link.text()
console.error('*', name)
return {
name: name
, link: link.attr('href')
, desc: feat && html.trim() || ''
, thumb: thumb
, featured: feat
, i: i
}
})
.sort(function(a, b) {
if (a.featured) return -1
if (b.featured) return +1
return a.i - b.i
})
fs.writeFileSync(__dirname + '/../build/examples.json'
, JSON.stringify(examples, null, 2)
)