Acwing blog to markdown

用于将 Acwing 上的代码模板转换为 markdown

目前為 2022-03-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Acwing blog to markdown
// @namespace   acwing
// @match       https://www.acwing.com/blog/content/*
// @grant       GM_setClipboard
// @grant       GM_registerMenuCommand
// @grant       GM_unregisterMenuCommand
// @version     1.0
// @author      moeakwak
// @description 用于将 Acwing 上的代码模板转换为 markdown
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @require https://cdn.bootcdn.net/ajax/libs/turndown/7.1.1/turndown.min.js
// @license MIT
// ==/UserScript==

$("[data-tab='preview-tab-content']").before("<button id='html2md'>转换为markdown</button>");

$("#html2md").click(function () {
  let doc = $("[data-tab='preview-tab-content']");
  let turndownService = new TurndownService();
  turndownService.addRule('pre', {
    filter: 'pre',
    replacement: function (content, node) {
      let t = $(node).attr("class").split(/\s+/).slice(-1);
      return "```" + t +"\n" + content + "```";
    }
  });
  
  let markdown = turndownService.turndown(doc.html());
  GM_setClipboard(markdown);
  console.log(markdown);
  
  $("#html2md").text("已复制到剪贴板");
});