V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
muyunyun
V2EX  ›  分享创造

分享一款将 LeetCode 中 AC 的题目转化为 MarkDown 表格的插件

  •  
  •   muyunyun ·
    MuYunyun · 2020-09-30 13:17:18 +08:00 · 1980 次点击
    这是一个创建于 1275 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景: 写博客的时候每当新增 LeetCode 题解时都需要在 https://github.com/MuYunyun/blog/blob/master/LeetCode/README.md 手动更新表格, 非常费劲。因此构思了该插件实现自动化同步更新 leetcode ac 题解为 markdown table 。

    crd-leetcode-cli

    crd-leetcode-cli 提供将 leetcode 中已 AC 的题目转化为 markdown 表格的能力。

    Install

    执行 yarn add crd-leetcode-cli -g, 国内用户可以执行 cnpm install crd-leetcode-cli -g

    Usage

    leetcode download       // 增量拉取 AC 题目(若无登录, 则会先执行登录逻辑)
    leetcode download -a    // 全量拉取 AC 题目
    leetcode login          // 登录
    leetcode logout         // 登出
    

    接入项目示例

    Render Markdown Table Customly

    插件提供了自定义渲染 markdown table 的能力。

    const transform_markdown_table = (dataArr) => {
      const beforeDescription = `The markdown table is generated by [crd-leetcode-cli]( https://github.com/MuYunyun/create-react-doc/tree/master/packages/leetcode-cli)`;
      let result = beforeDescription + '\n' +
        '| # | Title | Explanation | Difficulty | Type |' +
        '\n' +
        '|:---:|:---:|:---:|:---:|:---:|';
    
      for (let i = 0; i < dataArr.length; i++) {
        result += `\n| ${dataArr[i].questionId} | [${dataArr[i].title
          }]( https://leetcode.com/problems/${dataArr[i].titleSlug
          }/) | [Analyze]( https://github.com/MuYunyun/blog/blob/master/LeetCode/${dataArr[i].questionId
          }.${dataArr[i].title.split(' ').join('_')}.md) | ${dataArr[i].difficulty
          } | ${dataArr[i].topicTags} |`;
      }
      return result;
    };
    
    module.exports = { transform_markdown_table }
    

    通过自定义 transform_markdown_table 函数, 便可得到如下 markdown table:

    Technology Details

    6 条回复    2020-10-06 18:16:00 +08:00
    jiangshanmeta
        1
    jiangshanmeta  
       2020-09-30 15:58:21 +08:00
    题主 为了完成同样的事情,我只用了一个[node 脚本]( https://github.com/jiangshanmeta/meta/blob/master/scripts/generate.js)
    muyunyun
        2
    muyunyun  
    OP
       2020-09-30 18:31:26 +08:00
    @jiangshanmeta 神奇, 调用 restful 接口不用 cookie 就能访问么。。我这边多了一步登陆操作, 成本其实相差不大, 后续涉及到编辑的接口也能访问了。
    muyunyun
        3
    muyunyun  
    OP
       2020-09-30 18:34:15 +08:00
    asanelder
        4
    asanelder  
       2020-09-30 19:22:11 +08:00
    挺好,不错,鼓励,给个赞
    xrr2016
        5
    xrr2016  
       2020-10-01 07:33:57 +08:00 via iPhone
    标记
    muyunyun
        6
    muyunyun  
    OP
       2020-10-06 18:16:00 +08:00
    @asanelder 谢啦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3111 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:04 · PVG 08:04 · LAX 17:04 · JFK 20:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.