Last active
December 11, 2020 03:47
-
-
Save XiongLiding/fc70b644c54d0c2bc1a6ed788469a93a to your computer and use it in GitHub Desktop.
将valine导出的评论转换成CSV格式,方便导入自建数据库(如 waline 的 wl_comment 表)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* trans | |
* 将valine导出的评论转换成CSV格式 | |
* @param {String} input - 从 valine 导出的 JSON 文本 | |
* @return {String} CSV 文本 | |
*/ | |
const trans = input => { | |
let output = []; | |
const field = [ | |
"nick", | |
"updatedAt", | |
"mail", | |
"ua", | |
"insertedAt", | |
"createdAt", | |
"comment", | |
"link", | |
"url" | |
]; | |
output.push(field.join(",")); | |
const [_head, ...body] = input.results; | |
body.forEach(row => { | |
let data = field.map(key => { | |
if (key == "insertedAt") { | |
return row[key].iso; | |
} | |
return row[key]?.replaceAll('"', '""'); | |
}); | |
output.push('"' + data.join('","') + '"'); | |
}); | |
return output.join("\n"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment