🐓🥚一键同步简书的文章到-GitHub-Pages

简书的文章写完后,想同步到github的博客,这样你的博客有可能送到北极也说不定呢🥳🥳。
首先你要有一个github Page,现在有ruby和node.js版的,我用的这个模版,jekyll,免费快速搭建博客模版,你也可以选择其它的。
1.创建github Page
2.利用脚本下载简书文章
下载userName.github.io仓库代码,在项目根目录中建立如下文件夹

新建 setUp.js,这里只展示主要代码,详细信息可查看github仓库
2.1下载简书文章
简书有一键下载功能,

接口:https://www.jianshu.com/backup/download
我写了一个脚本,可以直接下载到本地
1 2 3 4 5 6 7 8 9 10
| (async () => { await download( config.downloadUrl, // 下载连接 https://www.jianshu.com/backup/download config.downloadPath, // 下载后的文件路径 { headers: { Cookie: config.cookie, // 你的简书cookie,很好找的 }, }); })()
|
2.2 解压下载文章压缩包
删除上次解压的文件夹,找到最新的压缩文件,加压到output文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| unCompress() { this.deleteUnrarDir(); const inputRarPath = this.newestRar(); exec( `unar ${inputRarPath} -o ${this.unRarPath}`, (err, stdout, stderr) => { if (err) { console.log(err); return; } // 简书文章分为不同累不,解压到不同文件夹 let childDirs = fs.readdirSync(this.unCompressPath()); childDirs.forEach((item) => { file.addTags(this.unCompressPath() + "/" + item + "/"); }); if (stderr) console.error(`stderr: ${stderr}`); } ); }
|
2.3 为文章打Tag
jekyll要根据文件头的Tag,生成文章信息,标题,日期,文章分类。格式如下
1 2 3 4 5 6 7 8
| --- layout: post tilte: "2018-12-08-🦕🦕---React-如何阻止事件冒泡?.md" date: 2018-12-08- tags: - 开发日常 ---
|
output里的文章打Tag后,复制到_post(默认文件夹)里,文章头如果没有日期,自动加上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| addTags(unarTagPath) {
fs.readdir(unarTagPath, "utf8", (err, fileList) => { if (err) throw err; fileList.forEach((excludeDatePrexFile) => { const filePath = unarTagPath + excludeDatePrexFile; const postContent = this.readFile( filePath, excludeDatePrexFile, this.prex ); this.addTagsOnHeader(filePath, postContent);
this.renameFileByAddDatePrexInUnarTagPath( unarTagPath, this.prex, excludeDatePrexFile ); }); }); }
|
3.提交到github
github会自动构建你发布的文章
遇到的问题,集中在:
1.了解http协议 (request,response)
2.如何处理不同场景下的响应体 (response, body)
3.响应头中的 content-type 与响应数据对应的关系
4.简书markdown,解析和jekyll不太一样,可能会样式错乱