龗孖 龗孖
首页
  • JAVA
  • 设计模式
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 页面

    • HTML
    • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

靇孖

某微型企业非牛逼技术专家。
首页
  • JAVA
  • 设计模式
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 页面

    • HTML
    • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • JAVA

  • MQ

  • 工具

    • Arthas

    • Docker

    • Lombok

    • Redis

    • gitalk

      • hexo_gitalk_评论自动初始化
      • hexo

    • 微服务

    • 数据库

    • 其他

    • 程序设计

    • 算法

    • 服务端
    • 工具
    • gitalk
    龗孖
    2023-08-01
    目录

    hexogitalk评论自动初始化

    # 第一步 申请Personal Access Token

    从 Github 的 Personal access tokens (opens new window) 页面,点击 Generate new token (opens new window)

    # 第二步 安装项目依赖

    npm i request xml-parser blueimp-md5 moment hexo-generator-sitemap -S

    项目根目录配置文件 _config.yml 添加配置

    # ...
    # hexo sitemap网站地图
    sitemap:
      path: sitemap.xml
      # 此配置跟后面新建的文件名相同
      template: ./sitemap_template.xml
    # ...
    
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 第三步 项目根目录下新建 comment.js

    #!/usr/bin/env node
    const request = require("request");
    const fs = require("fs");
    const path = require("path");
    const url = require("url");
    const xmlParser = require("xml-parser");
    const YAML = require("yamljs");
    const cheerio = require("cheerio");
    const md5 = require("md5");
    // 根据自己的情况进行配置
    const config = {
        username: "用户名", // GitHub 用户名
        token: "Token",  // GitHub Token
        repo: "issues的git仓库",  // 存放 issues的git仓库
        // sitemap.xml的路径,commit.js放置在根目录下,无需修改,其他情况自行处理
        sitemapUrl: path.resolve(__dirname, "./public/sitemap.xml"),
        kind: "Gitalk",  // "Gitalk" or "Gitment",
        baseUrl: "http://blog.lingma.top/"
    };
    let issuesUrl = `https://api.github.com/repos/${config.username}/${config.repo}/issues?access_token=${config.token}`;
    
    let requestGetOpt = {
        url: `${issuesUrl}&page=1&per_page=1000`,
        json: true,
        headers: {
            "User-Agent": "github-user",
            "Authorization":"token 你的token"
        }
    };
    let requestPostOpt = {
        ...requestGetOpt,
        url:issuesUrl,
        method: "POST",
        form: ""
    };
    
    console.log("开始初始化评论...");
    
    (async function() {
        console.log("开始检索链接,请稍等...");
    
        try {
            let websiteConfig = YAML.parse(fs.readFileSync(path.resolve(__dirname, "./_config.yml"), "utf8"));
    
            let urls = sitemapXmlReader(config.sitemapUrl);
    
    
            console.log(`共检索到${urls.length}个链接`);
    
            console.log("开始获取已经初始化的issues:"+`${requestGetOpt.url}` );
            let issues = await send(requestGetOpt);
         //   console.log(issues)
            console.log(`已经存在${issues.length}个issues`);
    
            let notInitIssueLinks = urls.filter((link) => {
                return !issues.find((item) => {
    
                    link = removeProtocol(link);
                    console.log("link",link)
                    return  !!item.body? item.body.includes(link):false;
                });
            });
    
    
            if (notInitIssueLinks.length > 0) {
                console.log(`本次有${notInitIssueLinks.length}个链接需要初始化issue:`);
             //   console.log(notInitIssueLinks);
                console.log("开始提交初始化请求, 大约需要40秒...");
                /**
                 * 部署好网站后,直接执行start,新增文章并不会生成评论
                 * 经测试,最少需要等待40秒,才可以正确生成, 怀疑跟github的api有关系,没有找到实锤
                 */
                setTimeout(async ()=>{
                    let initRet = await notInitIssueLinks.map(async (item) => {
                        let html = await send({ ...requestGetOpt, url: item });
                        let title = cheerio.load(html)("title").text();
                        let pathLabel = md5( url.parse(item).path);
                        console.log("MD5",url,url.parse(item).path,pathLabel)
                        let body = `${item}<br><br>${websiteConfig.description}`;
                        let form = JSON.stringify({ body, labels: [config.kind, pathLabel], title });
                        return send({ ...requestPostOpt, form });
                    });
                    console.log(`已完成${initRet.length}个!`);
                    console.log("可以愉快的发表评论了!");
                },40000);
            } else {
                console.log("本次发布无新增页面,无需初始化issue!!");
            }
        } catch (e) {
            console.log(`初始化issue出错,错误如下:`);
            console.log(e);
        } finally {
    
        }
    })();
    
    function sitemapXmlReader(file) {
        let data = fs.readFileSync(file, "utf8");
        let sitemap = xmlParser(data);
        return sitemap.root.children.map(function (url) {
            let loc = url.children.filter(function (item) {
                return item.name === "loc";
            })[0];
            return loc.content;
        });
    }
    
    function removeProtocol(url) {
        return url.substr(url.indexOf(":"));
    }
    
    function send(options) {
        return new Promise(function (resolve, reject) {
            request(options, function (error, response, body) {
                if (!error) {
                    resolve(body);
                } else {
                    reject(error);
                }
            });
        });
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122

    # 第四步: 测试

    执行 node ./comment.js

    # 第五步: 自动化

    修改 package.json

    ...
    "scripts": {
        "b": "npm run c && hexo generate && node comment.js",
        "c": "hexo clean",
        "d": "npm run b && hexo d ",
        "s": "npm run b && hexo s"
      },
    ...
    
    1
    2
    3
    4
    5
    6
    7
    8

    运行 npm run d 或者 npm run s

    本文参考: https://blog.jijian.link/2020-01-10/hexo-gitalk-auto-init/

    上次更新: 2024/11/01, 16:22:32
    简单接口限流的设计与实现
    hexo多平台多博客网站同步

    ← 简单接口限流的设计与实现 hexo多平台多博客网站同步→

    最近更新
    01
    树中两个节点的最低公共祖先
    10-17
    02
    hexo多平台多博客网站同步
    09-04
    03
    最长不含重复字符的子字符串
    09-03
    更多文章>
    Theme by Vdoing | Copyright © 2015-2024 Ling ma | 996.icu | 京ICP备16011424号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式