marked-heading-link-test.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <title>Marked heading link Test</title>
  5. <meta charset="UTF-8">
  6. <meta name="description" content="" />
  7. <meta name="keywords" content="" />
  8. <link rel="stylesheet" href="../examples/css/style.css" />
  9. </head>
  10. <body>
  11. <script src="../examples/js/jquery.min.js"></script>
  12. <script src="../lib/marked.min.js"></script>
  13. <script type="text/javascript">
  14. var linkReg = /\s*\<a\s*href\=\"(.*)\"\s*([^\>]*)\>(.*)\<\/a\>\s*/;
  15. var linkTextReg = /\s*\<a\s*([^\>]+)\>([^\>]*)\<\/a\>\s*/g;
  16. var testLink = "<a href=\"http://daringfireball.net/projects/markdown/basics\" title=\"Markdown Basics\">Markdown Basics</a> ";
  17. var testLink2 = "<a href=\"http://daringfireball.net/projects/markdown/basics\" title=\"Xdfsdf\">Xdfsdf</a> ";
  18. var testLink3 = "<a href=\"\" title=\"\"></a> ";
  19. var texts = (testLink + testLink2 + testLink3).split(/\<a\s*([^\>]+)\>([^\>]*)\<\/a\>/);
  20. console.log(texts);
  21. for (var i = 0, len = texts.length; i < len; i++)
  22. {
  23. if((i+1) % 3 == 0) console.log(texts[i]);
  24. }
  25. (testLink + testLink2).replace(linkTextReg, function(){
  26. //console.log($1, $2);
  27. //console.log("linkText =>", $3);
  28. console.log(arguments);
  29. });
  30. testLink.replace(linkTextReg, function($1, $2, $3){
  31. console.log($1, $2, $3);
  32. });
  33. console.log(linkReg.test(" <a name=\"dddd\" class=\"link\" style=\"color:red;\">Markdown Basics</a> "));
  34. console.log(linkReg.test("<a name=\"dddd\" href=\"http://daringfireball.com/projects/markdown/basics\" class=\"link\" style=\"color:red;\">Markdown Basics</a> "));
  35. console.log(linkReg.test("<a href=\"http://daringfireball.net/projects/markdown/basics?xx=ddd&fddf&temp=265656565665656526\" title=\"dddd\" class=\"link\" style=\"color:red;\">Markdown Basics</a> "));
  36. console.log(linkReg.test("<a href=\"http://daringfireball.net/projects/markdown/basics\" title=\"dddd\"><span>Markdown Basics</span></a> "));
  37. console.log(linkReg.test("<a href=\"http://daringfireball.net/projects/markdown/basics?xx=ddd&fddf&temp=265656565665656526\" title=\"dddd\"><span class=\"active\">Markdown Basics</span></a> "));
  38. console.log(linkReg.test("<a href=\"http://www.fdasfasdfsdaf.com/projects/markdown/basics\" title=\"dddd\"><a href=\"#ddd\" class=\"active\">Markdown Basics</a></a> "));
  39. console.log(linkReg.test("afdsfasfsadf<a href=\"http://daringfireball.net/projects/markdown/basics?xx=ddd&fddf&temp=265656565665656526\" title=\"dddd\"><a href=\"#ddd\" class=\"active\">Markdown Basics</a></a> fdasfasdfadsfsfd <a href=\"http://daringfireball.net/projects/markdown/basics\" title=\"dddd\"><span>Markdown Basics</span></a>"));
  40. $(function() {
  41. var markedRenderer = new marked.Renderer();
  42. var markdownToC = markdownToC || [];
  43. markedRenderer.heading = function(text, level, raw) {
  44. var linkText = text;
  45. var hasLinkReg = /\s*\<a\s*href\=\"(.*)\"\s*([^\>]*)\>(.*)\<\/a\>\s*/;
  46. var getLinkTextReg = /\s*\<a\s*([^\>]+)\>([^\>]*)\<\/a\>\s*/g;
  47. if (hasLinkReg) {
  48. var tempText = [];
  49. text = text.split(/\<a\s*([^\>]+)\>([^\>]*)\<\/a\>/);
  50. console.log(text);
  51. for (var i = 0, len = text.length; i < len; i++)
  52. {
  53. if((i+1) % 3 == 0) tempText.push(text[i]);
  54. }
  55. text = tempText.join(" ");
  56. }
  57. var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-");
  58. var toc = {
  59. text : text,
  60. level : level,
  61. slug : escapedText
  62. };
  63. console.log("text =>", text);
  64. var isChinese = /^[\u4e00-\u9fa5]+$/.test(text);
  65. var id = (isChinese) ? escape(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-");
  66. markdownToC.push(toc);
  67. console.log("toc =>", toc);
  68. var headingHTML = "<h" + level + " id=\"h"+ level + "-" + this.options.headerPrefix + id +"\">";
  69. /*return "<h" + level + " id=\"h"+ level + "-" + this.options.headerPrefix + id +"\">" +
  70. "<a href=\"#" + text + "\" name=\"" + text + "\" class=\"anchor\"></a>" +
  71. "<span class=\"header-link\"></span>" + text + "</h" + level + ">";*/
  72. var anchor = "<a name=\"" + text + "\" class=\"anchor\"></a>";
  73. headingHTML += anchor;
  74. headingHTML += "<span class=\"header-link\"></span>";
  75. headingHTML += (hasLinkReg) ? linkText : text;
  76. headingHTML += "</h" + level + ">";
  77. return headingHTML;
  78. };
  79. marked.setOptions({
  80. renderer : markedRenderer,
  81. gfm : true,
  82. tables : true,
  83. breaks : true,
  84. pedantic : false,
  85. smartLists : true,
  86. smartypants : true
  87. });
  88. var md = marked("## [Markdown Basics](http://daringfireball.net/projects/markdown/basics \"Markdown Basics\")");
  89. console.log(md);
  90. var md2 = marked("## fdasfsd [Markdown Basics](http://daringfireball.net/projects/markdown/basics \"Markdown Basics\") xxx [Editor.md](http://daringfireball.net/projects/markdown/basics \"Editor.md\") fadsfasdfasdf");
  91. console.log(md2);
  92. var md3 = marked("### fdasfsd中文链接 [中文链接](http://daringfireball.net/projects/markdown/basics \"中文链接\") xxx 中文链接 [Editor.md 在线Markdown编辑器](http://daringfireball.net/projects/markdown/basics \"Editor.md 在线Markdown编辑器\") fadsfasdfasdf中文链接");
  93. console.log(md3);
  94. var md4 = marked("#### fdasfsd [Markdown Basics](http://daringfireball.net/projects/markdown/basics?xx=ddd&fddf&temp=265656565665656526 \"Markdown Basics\") xxx [Editor.md](http://www.ipandao.com/#xxxxx \"Editor.md\") fadsfasdfasdf");
  95. console.log(md4);
  96. $("body").append(md).append(md2).append(md3).append(md4);
  97. });
  98. </script>
  99. </body>
  100. </html>