v2exMarkdown

为v2ex而生的markdown渲染

当前为 2018-06-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name v2exMarkdown
  3. // @namespace https://github.com/hundan2020/v2exMarkdown
  4. // @version 0.1
  5. // @description 为v2ex而生的markdown渲染
  6. // @author hundan & ccsiyu
  7. // @match https://www.v2ex.com/t/*
  8. // @require https://cdn.staticfile.org/showdown/1.8.6/showdown.js
  9. // @require https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. String.prototype.replaceAll = function (search, replacement) {
  15. var target = this;
  16. return target.replace(new RegExp(search, 'g'), replacement);
  17. };
  18.  
  19. var markdownSwitch = true;
  20. $.when(true).then(function () {
  21. if (markdownSwitch) {
  22. processMarkdown();
  23. }
  24. });
  25.  
  26. function processMarkdown() {
  27. if (window.location.href.indexOf("mwap") > -1) { // for mobile site // dummy here
  28. } else { // desktop site
  29. $("div.reply_content").each(function () {
  30. var postMain = $(this)[0];
  31. var postText = postMain.innerText || postMain.textContent;
  32. var postContentLines = postText.split("\n");
  33. var converter = new showdown.Converter({
  34. simplifiedAutoLink: true
  35. });
  36. var innerHTML = "";
  37. postContentLines.forEach(function (item, index) {
  38. if (item.substring(0, 4) === "<br>")
  39. innerHTML += converter.makeHtml(item.substring(4));
  40. else if (item.charAt(0) == '<')
  41. innerHTML += item;
  42. else
  43. innerHTML += converter.makeHtml(item);
  44. });
  45. postMain.innerHTML = innerHTML;
  46. });
  47. }
  48. }
  49. })();