爱意满满的作品展示区。
muyunyun

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

  •  
  •   muyunyun ·
    MuYunyun · Sep 30, 2020 · 2971 views
    This topic created in 2099 days ago, the information mentioned may be changed or developed.

    背景: 写博客的时候每当新增 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 replies    2020-10-06 18:16:00 +08:00
    jiangshanmeta
        1
    jiangshanmeta  
       Sep 30, 2020
    题主 为了完成同样的事情,我只用了一个[node 脚本]( https://github.com/jiangshanmeta/meta/blob/master/scripts/generate.js)
    muyunyun
        2
    muyunyun  
    OP
       Sep 30, 2020
    @jiangshanmeta 神奇, 调用 restful 接口不用 cookie 就能访问么。。我这边多了一步登陆操作, 成本其实相差不大, 后续涉及到编辑的接口也能访问了。
    muyunyun
        3
    muyunyun  
    OP
       Sep 30, 2020
    asanelder
        4
    asanelder  
       Sep 30, 2020
    挺好,不错,鼓励,给个赞
    xrr2016
        5
    xrr2016  
       Oct 1, 2020 via iPhone
    标记
    muyunyun
        6
    muyunyun  
    OP
       Oct 6, 2020
    @asanelder 谢啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5307 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    ♥ Do have faith in what you're doing.