Skip to content

Commit 7a4f797

Browse files
committed
refactor(toc_helper): relpace cheerio with htmlparser2
1 parent 6fa7ada commit 7a4f797

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

lib/plugins/helper/toc.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
'use strict';
22

3-
let cheerio;
43
const { escapeHTML } = require('hexo-util');
4+
const { DomHandler, DomUtils, Parser } = require('htmlparser2');
5+
6+
const parseHtml = (html) => {
7+
const handler = new DomHandler(null, {});
8+
new Parser(handler, {}).end(html);
9+
return handler.dom;
10+
};
511

612
function tocHelper(str, options = {}) {
7-
if (!cheerio) cheerio = require('cheerio');
13+
const dom = parseHtml(str);
814

9-
const $ = cheerio.load(str);
1015
const headingsMaxDepth = Object.prototype.hasOwnProperty.call(options, 'max_depth') ? options.max_depth : 6;
1116
const headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
12-
const headings = $(headingsSelector);
17+
18+
const headings = DomUtils.find(el => headingsSelector.includes(el.tagName), dom, true);
1319

1420
if (!headings.length) return '';
1521

@@ -21,15 +27,15 @@ function tocHelper(str, options = {}) {
2127
let lastLevel = 0;
2228

2329
function getId(ele) {
24-
const id = $(ele).attr('id');
25-
const $parent = $(ele).parent();
26-
return id || ($parent.length < 1 ? null : getId($parent));
30+
const { id } = ele.attribs;
31+
const { parent } = ele;
32+
return id || (parent.length < 1 ? null : getId(parent));
2733
}
2834

29-
headings.each(function() {
30-
const level = +this.name[1];
31-
const id = getId(this);
32-
const text = escapeHTML($(this).text());
35+
for (const el of headings) {
36+
const level = +el.name[1];
37+
const id = getId(el);
38+
const text = escapeHTML(DomUtils.getText(el));
3339

3440
lastNumber[level - 1]++;
3541

@@ -67,7 +73,7 @@ function tocHelper(str, options = {}) {
6773
result += `<span class="${className}-text">${text}</span></a>`;
6874

6975
lastLevel = level;
70-
});
76+
}
7177

7278
for (let i = firstLevel - 1; i < lastLevel; i++) {
7379
result += '</li></ol>';

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
"archy": "^1.0.0",
4040
"bluebird": "^3.5.2",
4141
"chalk": "^2.4.1",
42-
"cheerio": "0.22.0",
4342
"hexo-cli": "^3.0.0",
4443
"hexo-front-matter": "^1.0.0",
4544
"hexo-fs": "^2.0.0",
4645
"hexo-i18n": "^1.0.0",
4746
"hexo-log": "^1.0.0",
4847
"hexo-util": "^1.5.0",
48+
"htmlparser2": "^4.0.0",
4949
"js-yaml": "^3.12.0",
5050
"lodash": "^4.17.11",
5151
"micromatch": "^4.0.2",
@@ -67,6 +67,7 @@
6767
"@easyops/git-exec-and-restage": "^1.0.4",
6868
"chai": "^4.1.2",
6969
"chai-as-promised": "^7.1.1",
70+
"cheerio": "0.22.0",
7071
"eslint": "^6.0.1",
7172
"eslint-config-hexo": "^3.0.0",
7273
"hexo-renderer-marked": "^2.0.0",

0 commit comments

Comments
 (0)