修复抽取页内目录含单双引号、圆括号时无法正确解析的问题

This commit is contained in:
TevinLi 2016-11-29 15:41:03 +08:00
parent 2d9dc0d5a4
commit 5c2db8836d
2 changed files with 28 additions and 13 deletions

View File

@ -77,7 +77,7 @@
$titles = that.$e.view.find('h1,h2,h3');
$titles.each(function (index, element) {
var $this = $(element);
var text = $.trim($this.text());
var text = $.trim($this.text()).replace('"', '');
//设置描记
$this.prepend(anchorHtml.replace(/\{title\}/g, text));
//首次打开页面滚动位置修正

View File

@ -29,30 +29,45 @@ module.exports = {
var lineStr = '';
var hashStr = '';
var h1 = '';
var text;
//修剪标题文本
var trimStr = function (str) {
return str.replace(/^\s+|\s+$/g, '') //去除首尾空格
.replace(/\[(.*?)]\(.*?\)/g, '$1') //去除链接
.replace(/<.*?>/g, '') //去除html标签
.replace(/'/g, '&#39;') //转义单引号
.replace(/"/g, '&#34;') //转义双引号由于双引号无法正确传给html属性当作为hash时删除
.replace(/\(/g, '&#40;') //转义左圆括号
.replace(/\)/g, '&#41;') //转义右圆括号
.replace(/\[/g, '&#91;') //转义左中括号
.replace(/\]/g, '&#93;'); //转义右中括号
};
//生成列表
for (var i = 0; i < lines.length; i++) {
lines[i] = lines[i].replace(/^\s*/, '');
lines[i] = lines[i].replace(/^\s*/, ''); //去除行首空格
switch (lines[i].split(/\s/)[0]) {
case '#':
var text = lines[i].split('# ')[1];
text = lines[i].split('# ')[1];
if (text != undefined) {
lineStr = text.replace(/^\s+|\s+$/g, ''); //去除首位空格
h1 = h1 == '' ? lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1') : h1;
h1 = trimStr(text);
}
break;
case '##':
var text = lines[i].split('## ')[1];
text = lines[i].split('## ')[1];
if (text != undefined) {
lineStr = text.replace(/^\s+|\s+$/g, '');
lineStr = lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1'); //去除链接
contents += '1. [' + lineStr + '](#' + lineStr + ' "' + lineStr + '")\n';
lineStr = trimStr(text);
contents += '1. [' + lineStr + '](#' + lineStr.replace(/&#34;/g, '') + ' "' + lineStr + '")\n';
} else {
contents += '1. &#12288;\n';
}
break;
case '###':
var text = lines[i].split('### ')[1];
text = lines[i].split('### ')[1];
if (text != undefined) {
lineStr = text.replace(/^\s+|\s+$/g, '');
lineStr = lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1');
contents += '\t1. [' + lineStr + '](#' + lineStr + ' "' + lineStr + '")\n';
lineStr = trimStr(text);
contents += '\t1. [' + lineStr + '](#' + lineStr.replace(/&#34;/g, '') + ' "' + lineStr + '")\n';
} else {
contents += '\t1. &#12288;\n';
}
break;
}