twikoo-func 1.7.24 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/import.js DELETED
@@ -1,290 +0,0 @@
1
- const { getRelativeUrl, normalizeMail } = require('.')
2
- const { getMarked, getDomPurify, getMd5 } = require('./lib')
3
- const marked = getMarked()
4
- const md5 = getMd5()
5
-
6
- const ARRAY_FIELDS = ['like', 'ups', 'downs']
7
-
8
- const parseJsonArrayField = (value) => {
9
- if (Array.isArray(value)) return value
10
- if (typeof value !== 'string') return value
11
- const trimmed = value.trim()
12
- if (!trimmed.startsWith('[')) return value
13
- try {
14
- const parsed = JSON.parse(trimmed)
15
- return Array.isArray(parsed) ? parsed : value
16
- } catch (e) {
17
- return value
18
- }
19
- }
20
-
21
- const normalizeTwikooComment = (comment) => {
22
- const parsed = { ...comment }
23
- if (comment._id && comment._id.$oid) {
24
- // 解决 id 历史数据问题
25
- parsed._id = comment._id.$oid
26
- }
27
- if (comment.pid === null) {
28
- delete parsed.pid
29
- }
30
- if (comment.rid === null) {
31
- delete parsed.rid
32
- }
33
- for (const field of ARRAY_FIELDS) {
34
- if (field in parsed) {
35
- parsed[field] = parseJsonArrayField(parsed[field])
36
- }
37
- }
38
- return parsed
39
- }
40
-
41
- const fn = {
42
- // 兼容 Leancloud 两种 JSON 导出格式
43
- jsonParse (content) {
44
- try {
45
- return JSON.parse(content)
46
- } catch (e1) {
47
- const results = []
48
- const lines = content.split('\n')
49
- for (const line of lines) {
50
- // 逐行 JSON.parse
51
- try {
52
- results.push(JSON.parse(line))
53
- } catch (e2) {}
54
- }
55
- return { results }
56
- }
57
- },
58
- // Valine 导入
59
- async commentImportValine (valineDb, log) {
60
- let arr
61
- if (valineDb instanceof Array) {
62
- arr = valineDb
63
- } else if (valineDb && valineDb.results) {
64
- arr = valineDb.results
65
- }
66
- if (!arr) {
67
- log('Valine 评论文件格式有误')
68
- return
69
- }
70
- const comments = []
71
- log(`共 ${arr.length} 条评论`)
72
- for (const comment of arr) {
73
- try {
74
- const parsed = {
75
- _id: comment.objectId,
76
- nick: comment.nick,
77
- ip: comment.ip,
78
- mail: comment.mail,
79
- mailMd5: comment.mailMd5,
80
- isSpam: comment.isSpam,
81
- ua: comment.ua || '',
82
- link: comment.link,
83
- pid: comment.pid,
84
- rid: comment.rid,
85
- master: false,
86
- comment: comment.comment,
87
- url: comment.url,
88
- created: new Date(comment.createdAt).getTime(),
89
- updated: new Date(comment.updatedAt).getTime()
90
- }
91
- comments.push(parsed)
92
- log(`${comment.objectId} 解析成功`)
93
- } catch (e) {
94
- log(`${comment.objectId} 解析失败:${e.message}`)
95
- }
96
- }
97
- log(`解析成功 ${comments.length} 条评论`)
98
- return comments
99
- },
100
- // Disqus 导入
101
- async commentImportDisqus (disqusDb, log) {
102
- if (!disqusDb || !disqusDb.disqus || !disqusDb.disqus.thread || !disqusDb.disqus.post) {
103
- log('Disqus 评论文件格式有误')
104
- return
105
- }
106
- const comments = []
107
- const getParent = (post) => {
108
- return post.parent ? disqusDb.disqus.post.find((item) => item.$['dsq:id'] === post.parent[0].$['dsq:id']) : null
109
- }
110
- let threads = []
111
- try {
112
- threads = disqusDb.disqus.thread.map((thread) => {
113
- return {
114
- id: thread.$['dsq:id'],
115
- title: thread.title[0],
116
- url: thread.id[0],
117
- href: thread.link[0]
118
- }
119
- })
120
- } catch (e) {
121
- log(`无法读取 thread:${e.message}`)
122
- return
123
- }
124
- log(`共 ${disqusDb.disqus.post.length} 条评论`)
125
- for (const post of disqusDb.disqus.post) {
126
- try {
127
- const threadId = post.thread[0].$['dsq:id']
128
- const thread = threads.find((item) => item.id === threadId)
129
- const parent = getParent(post)
130
- let root
131
- if (parent) {
132
- let grandParent = parent
133
- while (true) {
134
- if (grandParent) root = grandParent
135
- else break
136
- grandParent = getParent(grandParent)
137
- }
138
- }
139
- comments.push({
140
- _id: post.$['dsq:id'],
141
- nick: post.author[0].name[0],
142
- mail: '',
143
- link: '',
144
- url: thread.url
145
- ? thread.url.indexOf('http') === 0
146
- ? getRelativeUrl(thread.url)
147
- : thread.url
148
- : getRelativeUrl(thread.href),
149
- href: thread.href,
150
- comment: post.message[0],
151
- ua: '',
152
- ip: '',
153
- isSpam: post.isSpam[0] === 'true' || post.isDeleted[0] === 'true',
154
- master: false,
155
- pid: parent ? parent.$['dsq:id'] : undefined,
156
- rid: root ? root.$['dsq:id'] : undefined,
157
- created: new Date(post.createdAt[0]).getTime(),
158
- updated: Date.now()
159
- })
160
- log(`${post.$['dsq:id']} 解析成功`)
161
- } catch (e) {
162
- log(`${post.$['dsq:id']} 解析失败:${e.message}`)
163
- }
164
- }
165
- log(`解析成功 ${comments.length} 条评论`)
166
- return comments
167
- },
168
- // Artalk 导入
169
- async commentImportArtalk (artalkDb, log) {
170
- const comments = []
171
- if (!artalkDb || !artalkDb.length) {
172
- log('Artalk 评论文件格式有误')
173
- return
174
- }
175
- const DOMPurify = getDomPurify()
176
- marked.setOptions({
177
- renderer: new marked.Renderer(),
178
- gfm: true,
179
- tables: true,
180
- breaks: true,
181
- pedantic: false,
182
- smartLists: true,
183
- smartypants: true
184
- })
185
- log(`共 ${artalkDb.length} 条评论`)
186
- for (const comment of artalkDb) {
187
- try {
188
- const parsed = {
189
- _id: `artalk${comment.id}`,
190
- nick: comment.nick,
191
- ip: comment.ip,
192
- mail: comment.email,
193
- mailMd5: md5(normalizeMail(comment.email)),
194
- isSpam: false,
195
- ua: comment.ua || '',
196
- link: comment.link,
197
- pid: comment.rid && comment.rid !== '0' ? `artalk${comment.rid}` : undefined,
198
- rid: comment.rid && comment.rid !== '0' ? `artalk${comment.rid}` : undefined,
199
- master: false,
200
- comment: DOMPurify.sanitize(marked.marked(comment.content)),
201
- url: getRelativeUrl(comment.page_key),
202
- href: comment.page_key,
203
- created: new Date(comment.date).getTime(),
204
- updated: Date.now()
205
- }
206
- comments.push(parsed)
207
- log(`${comment.id} 解析成功`)
208
- } catch (e) {
209
- log(`${comment.id} 解析失败:${e.message}`)
210
- }
211
- }
212
- log(`解析成功 ${comments.length} 条评论`)
213
- return comments
214
- },
215
- // Artalk v2 导入
216
- async commentImportArtalk2 (artalkDb, log) {
217
- const comments = []
218
- if (!artalkDb || !artalkDb.length) {
219
- log('Artalk v2 评论文件格式有误')
220
- return
221
- }
222
- const DOMPurify = getDomPurify()
223
- marked.setOptions({
224
- renderer: new marked.Renderer(),
225
- gfm: true,
226
- tables: true,
227
- breaks: true,
228
- pedantic: false,
229
- smartLists: true,
230
- smartypants: true
231
- })
232
- log(`共 ${artalkDb.length} 条评论`)
233
- for (const comment of artalkDb) {
234
- try {
235
- const parsed = {
236
- _id: `artalk${comment.id}`,
237
- nick: comment.nick,
238
- ip: comment.ip,
239
- mail: comment.email,
240
- mailMd5: md5(normalizeMail(comment.email)),
241
- isSpam: comment.is_pending === 'true',
242
- ua: comment.ua || '',
243
- link: comment.link,
244
- pid: comment.rid && comment.rid !== '0' ? `artalk${comment.rid}` : undefined,
245
- rid: comment.rid && comment.rid !== '0' ? `artalk${comment.rid}` : undefined,
246
- master: false,
247
- comment: DOMPurify.sanitize(marked.marked(comment.content)),
248
- url: getRelativeUrl(comment.page_key),
249
- href: comment.page_key,
250
- created: new Date(comment.created_at).getTime(),
251
- updated: Date.now()
252
- }
253
- comments.push(parsed)
254
- log(`${comment.id} 解析成功`)
255
- } catch (e) {
256
- log(`${comment.id} 解析失败:${e.message}`)
257
- }
258
- }
259
- log(`解析成功 ${comments.length} 条评论`)
260
- return comments
261
- },
262
- // Twikoo 导入
263
- async commentImportTwikoo (twikooDb, log) {
264
- let arr
265
- if (twikooDb instanceof Array) {
266
- arr = twikooDb
267
- } else if (twikooDb && twikooDb.results) {
268
- arr = twikooDb.results
269
- }
270
- if (!arr) {
271
- log('Twikoo 评论文件格式有误')
272
- return
273
- }
274
- const comments = []
275
- log(`共 ${arr.length} 条评论`)
276
- for (const comment of arr) {
277
- try {
278
- const parsed = normalizeTwikooComment(comment)
279
- comments.push(parsed)
280
- log(`${comment._id} 解析成功`)
281
- } catch (e) {
282
- log(`${comment._id} 解析失败:${e.message}`)
283
- }
284
- }
285
- log(`解析成功 ${comments.length} 条评论`)
286
- return comments
287
- }
288
- }
289
-
290
- module.exports = fn