Closed xuexb closed 9 years ago
// var marked = require('marked'); // var renderer = new marked.Renderer(); // var count = 0; // renderer.heading = function(text, level) { // var escapedText = text.toLowerCase()//.replace(/[^\w]+/g, ''); // escapedText = encodeURIComponent(escapedText); // if (!escapedText) { // escapedText = ++count; // } // return '<h' + level + '><a name="anchor-' + // escapedText + // '" class="anchor" href="#anchor-' + // escapedText + // '"><span class="header-link"></span></a>' + // text + '</h' + level + '>'; // } // console.log(marked('# heading+', { // renderer: renderer // })); // console.log(marked('## 前端\n\ // [fdsfdsf](#前端)+', { // renderer: renderer // })); // console.log(marked('### f15614656+', { // renderer: renderer // })); var str = '<h2><a name="anchor-前端" class="anchor" href="#anchor-前端"><span class="header-link"></span></a>heading+</h2>\ <p>fdsjlkfjsdlkfjksldfj</p>\ {{view_page}}\ <p>fjsdklfjsdkf</p>\ {{view_page}}\ <h2><a name="anchor-%E5%89%8D%E7%AB%AF" class="anchor" href="#anchor-%E5%89%8D%E7%AB%AF"><span class="header-link"></span></a>前端</h2><p><a href="#前端">fdsfdsf</a>+</p>\ <h3><a name="anchor-前端" class="anchor" href="#anchor-f15614656%2B"><span class="header-link"></span></a>f15614656+</h3>\ <h3><a name="anchor-465464646" class="anchor" href="#anchor-f15614656%2B"><span class="header-link"></span></a>f15614656+</h3>\ <p>fsdfsf</p>'; var data = str.split('{{view_page}}'); var result = []; data.forEach(function(val, index){ var temp = val.match(/<h([23])><a name\=\"anchor\-(.+?)\"/g) || []; temp.forEach(function(val2){ var temp2 = val2.match(/<h([23])><a name\=\"anchor\-(.+?)\"/); if(temp2){ if(temp2[1] === '2'){ result.push('<dt><a href="?page='+ (index + 1) +'#anchor-'+ temp2[2] +'">'+ decodeURIComponent(temp2[2]) +'</a></dt>'); } else { result.push('<dd><a href="?page='+ (index + 1) +'#anchor-'+ temp2[2] +'">'+ decodeURIComponent(temp2[2]) + '</a></dd>'); } } temp2 = null; }); }); console.log(result);