修复抽取页内目录含单双引号、圆括号时无法正确解析的问题
This commit is contained in:
parent
2d9dc0d5a4
commit
5c2db8836d
|
|
@ -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));
|
||||
//首次打开页面滚动位置修正
|
||||
|
|
|
|||
|
|
@ -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, ''') //转义单引号
|
||||
.replace(/"/g, '"') //转义双引号,由于双引号无法正确传给html属性,当作为hash时删除
|
||||
.replace(/\(/g, '(') //转义左圆括号
|
||||
.replace(/\)/g, ')') //转义右圆括号
|
||||
.replace(/\[/g, '[') //转义左中括号
|
||||
.replace(/\]/g, ']'); //转义右中括号
|
||||
};
|
||||
//生成列表
|
||||
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(/"/g, '') + ' "' + lineStr + '")\n';
|
||||
} else {
|
||||
contents += '1.  \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(/"/g, '') + ' "' + lineStr + '")\n';
|
||||
} else {
|
||||
contents += '\t1.  \n';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue