forked from LeeJim/HowToCookOnMiniprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (119 loc) · 3.19 KB
/
Copy pathindex.js
File metadata and controls
127 lines (119 loc) · 3.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
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
const path = require('path')
const fs = require('fs')
const glob = require('glob')
const {marked} = require('marked')
const MagicString = require('magic-string')
const md5 = require('md5')
const flattenToken = (token) => {
const { type, tokens, text, href } = token
switch(type) {
case 'list':
const items = token.items.map(item => flattenToken(item))
return { type, items }
case 'list_item':
if (tokens.length == 1 && tokens[0].tokens.length == 1) {
const final = tokens[0].tokens[0];
return final.type == 'text' ? text : flattenToken(final);
}
if (tokens.length == 1) {
return flattenToken(tokens[0])
}
// console.log(token);
return tokens.map(item => flattenToken(item))
case 'row':
return { type, row: token.rows }
case 'text':
case 'paragraph':
if (!tokens) return text
if (tokens.length == 1) {
return tokens[0].type == 'text' ? text : flattenToken(tokens[0])
}
return tokens.map(item => flattenToken(item))
case 'strong':
case 'codespan':
case 'em':
return { type, text }
case 'html':
const ans = /<img.+src="([\w\.\/-]+)"/.exec(text)
if (ans) {
return { type: 'image', href: ans[1], text: '图片' }
}
return ''
case 'image':
// todo upload image
// token.href
return { type, text, href }
case 'link':
return { type, text, href }
case 'space':
return ''
case 'blockquote':
return text
default:
console.log(token)
}
return token
}
glob(path.resolve(__dirname, '../HowToCook/dishes/**/*.md'), {}, (err, files) => {
if (err) {
console.log(err)
}
const dishes = []
let no = 0
for(let p of files) {
const { name, dir } = path.parse(p)
const [ ,category ] = /dishes\/([\w-]+)\//g.exec(p)
const content = fs.readFileSync(path.resolve(p), { encoding: 'utf-8'})
let menu = {
no,
id: md5(name),
name,
category,
detail: [],
desc: []
}
no++;
target = null;
marked.lexer(content, {
baseUrl: dir,
xhtml: true
}).forEach((token) => {
const { text, type, depth } = token;
if (type == 'heading') {
if (depth == 1) {
menu.title = text
}
if (depth == 2) {
const tmp = {
text,
content: []
}
menu.detail.push(tmp)
target = tmp.content
if (text.includes('原料')) {
tmp.text = '原料和工具'
}
}
if (depth > 2) {
target.push({ type, text })
}
} else if (type !== 'space') {
if (target == null) {
menu.desc.push(flattenToken(token))
} else {
const tmp = flattenToken(token)
target.push(tmp)
}
}
})
dishes.push(menu)
}
const jsonData = new MagicString('');
const s = new MagicString(JSON.stringify(dishes, null, 2))
dishes.forEach(item => {
jsonData.append(JSON.stringify(item) + '\n')
})
s.prepend('export default ')
fs.writeFileSync('./miniprogram/data.js', s.toString())
fs.writeFileSync('./miniprogram/data.json', jsonData.toString())
})