Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用github issue创建blog #10

Open
lotosbin opened this issue Aug 18, 2019 · 1 comment
Open

使用github issue创建blog #10

lotosbin opened this issue Aug 18, 2019 · 1 comment
Assignees

Comments

@lotosbin
Copy link
Owner

lotosbin commented Aug 18, 2019

原因

  • 使用 github issue + mardkown 可以方便的写作博客
  • 使用 github api + jekll 可以自动生成 博客

使用

  • [install]复制本仓库下 generate.js 和 package.json 中的依赖 并执行 npm install
  • [setup] 修改 generate.js 中的 仓库地址和 想要生成博客对应的label
  • [usage]执行 node generate.js (node version 10.x)

实现

  • 搭建jekll博客,参考其他教程
  • 生成博客
    • 从github获取issue列表,参考 github api
    async function getIssues() {
      return new Promise((resolve, reject) => {
        const options = {
          method: 'GET',
          headers: {
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
          }
        };
        var request = require('request');
        request('https://api.github.com/repos/lotosbin/lotosbin.github.io/issues?labels=published', options, function (error, response, body) {
          if (!error && response.statusCode == 200) {
            resolve(JSON.parse(body));
          } else {
            reject(error || `${response.statusCode}`)
          }
        })
      })
    }
    • 把issue转换成article
     async function issueToArticle(issue) {
       var moment = require('moment');
       var fileName = `${moment(issue.created_at).format('YYYY-MM-DD')}-${issue.id}-${issue.title}`
       var title = issue.title;
       var body = issue.body;
       var content = `# ${title} \r\n ${body}`
       return { fileName, content }
     }
    • 根据article 生成博客文件
    const fs = require('fs');
    casync function writeArticleToFile(article) {
      return new Promise((resolve, reject) => {
        var filePath = `_posts/${article.fileName}.markdown`;
        fs.writeFile(filePath, `${article.content}`, function (err) {
          if (err) {
            console.log(err);
            reject(err);
            return
          }
    
          console.log(`generate file ${filePath}`);
          resolve({ filePath })
        });
      })
    }
    • 合体
    var main = async () => {
      var issues = await getIssues();
      for (i in issues) {
        var issue = issues[i];
        var article = await issueToArticle(issue);
        await writeArticleToFile(article);
      }
    }
    main()
  • 其他
  • 关于使用node开发命令行工具,参考其他文章

TODO

参考

ref:
ref: zp1112/blog#3
https://www.v2ex.com/t/253854

@lotosbin lotosbin self-assigned this Aug 18, 2019
@lotosbin lotosbin added the draft label Aug 18, 2019
lotosbin added a commit that referenced this issue Aug 26, 2019
@lotosbin lotosbin reopened this Aug 26, 2019
@yaoqs
Copy link

yaoqs commented Feb 5, 2024

感谢,在您的基础上做了点改进https://github.com/yaoqs/Issues-LordYao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants