v2exMarkdown

为v2ex而生的markdown渲染

目前为 2018-07-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name v2exMarkdown
  3. // @version 0.62
  4. // @description 为v2ex而生的markdown渲染
  5. // @author hundan
  6. // @match https://*.v2ex.com/t/*
  7. // @require https://cdn.staticfile.org/showdown/1.8.6/showdown.js
  8. // @grant none
  9. // @namespace https://github.com/hundan2020/v2exMarkdown
  10. // ==/UserScript==
  11.  
  12.  
  13. (function () {
  14. // jquery.js和highlight.js都由v2ex自身提供,不再向外部重复请求
  15. var preFix = function(rawReply){
  16. var picRe = function(reply){
  17. reply = reply.replace(/(?:!\[.*?\])?!\[.*?\]\(\s*((?:https?)?:\/\/i\.loli\.net\/\d{4}\/\d{2}\/\d{2}\/[a-z0-9]+.[a-z]+)\)|(https:\/\/i\.loli\.net\/\d{4}\/\d{2}\/\d{2}\/[a-z0-9]+.[a-z]+)/ig, '![]( ' + encodeURI('$1$2') + ' )'); // sm.ms
  18. reply = reply.replace(/(?:!\[.*?\])?!\[.*?\]\(\s*((?:https?)?:\/\/imgurl\.org\/temp\/\d{4}\/[a-z0-9]+\.[a-z0-9]+)\)|(https?:\/\/imgurl\.org\/temp\/\d{4}\/[a-z0-9]+\.[a-z0-9]+)/ig, '![]( ' + encodeURI('$1$2') + ' )'); // 小z图床
  19. reply = reply.replace(/(?<!http:|https:)\/\/([a-z0-9]+\.sinaimg.cn\/(?:[a-z]+)\/[a-z0-9]+\.(?:jpg|png|gif|bmp))/ig, '![]( ' + encodeURI('https://$1') + ' )'); // 新浪微博不规则图片链接
  20. return reply;
  21. }
  22. var xssFilter = function(reply){
  23. var sReply = reply;
  24. sReply = sReply.replace(/(!?\[.*?\]\(\s*?javascript.*?\))/igs, '`$1`');
  25. return sReply;
  26. }
  27. var fixedReply = rawReply;
  28. fixedReply = fixedReply.replace(/#(\d{1,3}\s)/ig, '&#x23;$1 '); // 避免楼层号加粗 safe
  29. debugger;
  30. fixedReply = fixedReply.replace(/(!\[(\S*?)\]\(\s+?)<a target="_blank" href="(\S+?)".*?><img src="(\S+?)" class="embedded_image".*?><\/a>\)+/ig, '![$2]($3)'); // 正常显示的图片处理 safe
  31. fixedReply = fixedReply.replace(/&lt;img src="(.+?)" \/&gt;/ig, '$1'); // 不规则图片链接处理 safe
  32. fixedReply = fixedReply.replace(/@<a href="\/member\/(\S+?)">(\S+?)<\/a>/ig, '@[$1](/member/$2)'); // 论坛内@处理,考虑到代码段中的@应当正常显示 safe
  33. fixedReply = fixedReply.replace(/<a target="_blank" href="(\/t\/\d+)">\/t\/\d+<\/a>/ig, '[$1]($1)'); // 论坛内链处理,考虑到在代码段中应当正常显示 safe
  34. fixedReply = fixedReply.replace(/<a.*? href="(\S+?)".*?>(\S+?)<\/a>/ig, '$2'); // 链接处理 safe
  35. fixedReply = fixedReply.replace(/(\n)?<br *\/?>/ig, "\n"); // 换行处理,避免多行代码无法正常工作 safe
  36. fixedReply = picRe(fixedReply);
  37. fixedReply = xssFilter(fixedReply);
  38. // 不安全的是,从原生markdown格式转变到html
  39. return fixedReply;
  40. }
  41. var endFix = function(markedReply){
  42. var fixedReply = markedReply;
  43. fixedReply = fixedReply.replace(/\n/ig, '<br />'); //safe markdown软回车转硬回车
  44. fixedReply = fixedReply.replace(/(<\/ul>|<\/li>|<\/p>|<\/table>|<\/h\d>)\s*<br\s*\/?>/ig, '$1'); // safe 表格换行删除
  45. fixedReply = fixedReply.replace(/<br\s*\/?>(<li>|<ul>|<p>|<table>|<h\d>)/ig, '$1'); // safe 表格换行删除
  46. fixedReply = fixedReply.replace(/(<\/?table>|<\/?tbody>|<\/?thead>|<\/?tr>|<\/?th>|<\/?td>)<br\s*\/?>/ig, '$1'); // safe 表格换行删除
  47. fixedReply = fixedReply.replace(/(<br\s*\/?>\s*){2,}/ig, '<br />'); // safe 多重换行转单行
  48. fixedReply = fixedReply.replace(/@\[(\S+?)\]\(\/member\/\S+\)/ig, '@$1'); // 代码段中的@ 还原
  49. fixedReply = fixedReply.replace(/\[(\/t\/\d+)\]\(\/t\/\d+\)/ig, '$1'); // 代码段中的内链还原
  50. fixedReply = fixedReply.replace(/&amp;/ig, '&'); // 对重复转义的 & 进行还原,而不必对<>进行操作,有效的避免了XSS发生
  51. return fixedReply;
  52. }
  53. var processMarkdown = function(){
  54. $("div.reply_content").each(function () {
  55. var reply = $(this)[0];
  56. var rawReply = reply.innerHTML;
  57. var converter = new showdown.Converter({
  58. omitExtraWLInCodeBlocks: true,
  59. parseImgDimensions: true,
  60. simplifiedAutoLink: true,
  61. literalMidWordUnderscores: true,
  62. strikethrough: true,
  63. tables: true,
  64. ghCodeBlocks: true,
  65. tasklists: true,
  66. smoothLivePreview: true,
  67. ghCompatibleHeaderId: true,
  68. encodeEmails: true,
  69. emoji: true
  70. });
  71. var markedReply = converter.makeHtml(preFix(rawReply));
  72. reply.innerHTML = endFix(markedReply);
  73. reply.className = 'reply_content markdown_body';
  74. // 开启代码高亮
  75. hljs.configure({useBR: true});
  76. $('div.reply_content code').each(function(i, block) {
  77. hljs.highlightBlock(block);
  78. });
  79. });
  80. }
  81. processMarkdown();
  82. console.clear();
  83. console.log("\n\n\n Thanks for using my script~\n\n\n\n");
  84. })();

QingJ © 2025

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