
Ghost ブログ Export ファイルを変換するスクリプトを書いた
ブログシステムを Ghost へ乗り換えてみたが、 Publish 日付とブログの日付が合わない、といった事象が発生していた。(ブログ再構築の際に順番をいしきしていなかった為、 publish 日付でソートされると、あまり宜しくない結果になった。) 仕方ない、、、と放置していたが、この度簡単なスクリプトを書いて対応した。 node のインストールに若干手こずったが、まぁ、Javascript、簡単! 概要 Ghost 管理メニューより Export した Jsonファイルを用意する。 今回は、ブログタイトルに、記事日付があったので、それを利用した。 title より取り出した 日付で、 publish_at 項目を更新。 本来の日付でソートできた!! vim convertMyGhostExport.js var srcPath = "/home/ghost/Desktop/your-domain.ghost.2015-10-07.json"; var dstPath = "/home/ghost/Desktop/your-domain.ghost.2015-10-07_dst.json"; var json = require(srcPath); json.db[0].data.posts.every(function(post,index,ar){ var dateStr = post.title.substr(0, 8); if (!dateStr.match(/\d{8}/)) { // title の頭8文字が数値出ない場合は対象外 return true; } if (post.status != "published") { // 公開していない場合は対象外 return true; } var yyyy = dateStr.substr(0,4); var mm = dateStr.substr(4,2) - 1; var dd = dateStr.substr(6,2); console.log(dateStr, yyyy, mm, dd); var published = new Date(post.published_at); var newPublished = new Date(post.published_at); // title から作成した日付を設定 newPublished.setFullYear(yyyy, mm, dd); console.log(" old: ==> "+published + "," + published.getTime()); console.log(" new: ==> "+newPublished + "," + newPublished.getTime()); post.published_at = newPublished.getTime(); return true; }); // 変換したJsonを出力 var fs = require('fs'); fs.writeFile(dstPath, JSON.stringify(json, null, ' ')); console.log("Done!"); 実行は、 node 開発環境で、以下を実行 ...