Skip to content

Instantly share code, notes, and snippets.

@XiongLiding
Last active December 11, 2020 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XiongLiding/fc70b644c54d0c2bc1a6ed788469a93a to your computer and use it in GitHub Desktop.
Save XiongLiding/fc70b644c54d0c2bc1a6ed788469a93a to your computer and use it in GitHub Desktop.
将valine导出的评论转换成CSV格式,方便导入自建数据库(如 waline 的 wl_comment 表)
/**
* 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