forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-indices.js
84 lines (69 loc) · 1.98 KB
/
gen-indices.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
const fs = require('fs')
const path = require('path')
const algoliasearch = require('algoliasearch')
const key = require('./algolia-key')
const client = algoliasearch('4GDUUWIAWB', key)
const index = client.initIndex('mand')
index.clearIndex(err => {
if (err) {
return
}
generateIndices('../../docs')
fs.readdir(path.resolve(__dirname, '../../../components'), (err, dirs) => {
if (err) {
console.log(err)
return
}
dirs.forEach(dir => {
if (dir.indexOf('.') < 0) {
generateIndices(`../../../components/${ dir }`, dir)
}
})
})
})
function generateIndices(filePath, component) {
fs.readdir(path.resolve(__dirname, filePath), (err, files) => {
if (err) {
console.log(err)
return
}
let indices = []
files.forEach(file => {
if (file.indexOf('.md') < 0) {
return
}
const content = fs.readFileSync(path.resolve(__dirname, `${ filePath }/${ file }`), 'utf8')
if (content.indexOf('##') < 0) {
return
}
const matches = content
.replace(/:::[\s\S]*?:::/g, '')
.replace(/```[\s\S]*?```/g, '')
.match(/#{2,5}[^#]*/g)
.map(match => match.replace(/\n+/g, '\n').split('\n').filter(part => !!part))
.map(match => {
const length = match.length
if (length > 2) {
const desc = match.slice(1, length).join('')
return [match[0], desc]
}
return match
})
indices = indices.concat(matches.map(match => {
const isComponent = match[0].indexOf('###') < 0
const title = match[0].replace(/#{2,5}/, '').trim()
let index
index = {
component: component || file.replace('.md', ''),
title,
}
index.ranking = isComponent ? 2 : 1
index.content = (match[1] || title).replace(/<[^>]+>/g, '')
return index
}))
})
index.addObjects(indices, (err, res) => {
console.log(err, res)
})
})
}