AcWing content to markdown

将AcWing上的内容转换为markdown

  1. // ==UserScript==
  2. // @name AcWing content to markdown
  3. // @namespace acwing
  4. // @match https://www.acwing.com/*
  5. // @grant GM_setClipboard
  6. // @version 1.4
  7. // @author -
  8. // @description 将AcWing上的内容转换为markdown
  9. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
  10. // @require https://cdn.bootcdn.net/ajax/libs/turndown/7.1.1/turndown.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. let debug = false; // whether to enable on editor
  15.  
  16. let turndownService = new TurndownService();
  17.  
  18. turndownService.keep(['del']);
  19.  
  20. // code block
  21. turndownService.addRule('pre', {
  22. filter: 'pre',
  23. replacement: function (content, node) {
  24. let t = $(node).attr("class").split(/\s+/).slice(-1);
  25. if (t == "hljs") t = "";
  26. return "```" + t + "\n" + content.trim() + "\n```";
  27. }
  28. });
  29.  
  30. // remove <script> math
  31. turndownService.addRule('remove-script', {
  32. filter: function (node, options) {
  33. return node.tagName.toLowerCase() == "script" && node.type.startsWith("math/tex");
  34. },
  35. replacement: function (content, node) {
  36. return "";
  37. }
  38. });
  39.  
  40. // inline math
  41. turndownService.addRule('inline-math', {
  42. filter: function (node, options) {
  43. return node.tagName.toLowerCase() == "span" && node.className == "MathJax";
  44. },
  45. replacement: function (content, node) {
  46. return "$ " + $(node).next().text() + " $";
  47. }
  48. });
  49.  
  50. // block math
  51. turndownService.addRule('block-math', {
  52. filter: function (node, options) {
  53. return node.tagName.toLowerCase() == "div" && node.className == "MathJax_Display";
  54. },
  55. replacement: function (content, node) {
  56. return "\n$$\n" + $(node).next().text() + "\n$$\n";
  57. }
  58. });
  59.  
  60. // add buttons
  61. $("div[data-tab='preview-tab-content']").each(function() {
  62. if (debug || $(this).prev().attr('data-tab') != "editor-tab-content")
  63. $(this).before(
  64. "<div> <button class='html2md-view'>显示markdown</button> <button class='html2md-cb'>复制markdown到剪贴板</button> </div>"
  65. );
  66. });
  67.  
  68. $(".html2md-cb").click(function() {
  69. let target = $(this).parent().next().get(0);
  70. if (!target.markdown)
  71. target.markdown = turndownService.turndown($(target).html());
  72. GM_setClipboard(target.markdown);
  73. // console.log(markdown);
  74. $(this).text("已复制到剪贴板");
  75. });
  76.  
  77. $(".html2md-view").click(function() {
  78. let target = $(this).parent().next().get(0);
  79. if (target.viewmd) {
  80. target.viewmd = false;
  81. $(this).text("显示markdown");
  82. $(target).html(target.original_html);
  83. } else {
  84. target.viewmd = true;
  85. if (!target.original_html)
  86. target.original_html = $(target).html();
  87. if (!target.markdown)
  88. target.markdown = turndownService.turndown($(target).html());
  89. $(this).text("显示原始内容");
  90. $(target).html(`<textarea oninput="$(this).parent().get(0).markdown=this.value;" style="width:100%; height:400px;"> ${target.markdown} </textarea>`);
  91. }
  92. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址