Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(toc_helper): utilize hexo-util #3850

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions lib/plugins/helper/toc.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
'use strict';

let cheerio;
const { escapeHTML } = require('hexo-util');
const { tocObj } = require('hexo-util');

function tocHelper(str, options = {}) {
if (!cheerio) cheerio = require('cheerio');
options = Object.assign({
max_depth: 6,
class: 'toc',
list_number: true
}, options);
Copy link
Contributor

@curbengh curbengh Dec 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to add min_depth option in this PR? Or in a separate PR (due to addition of unit test)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@curbengh No. The current unit test for toc helper is completely a mess. I might refactor the unit test when bringing up the min_depth.


const $ = cheerio.load(str);
const headingsMaxDepth = Object.prototype.hasOwnProperty.call(options, 'max_depth') ? options.max_depth : 6;
const headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
const headings = $(headingsSelector);
const data = tocObj(str, { max_depth: options.max_depth });

if (!headings.length) return '';
if (!data.length) return '';

const className = options.class;
const listNumber = options.list_number;

const className = options.class || 'toc';
const listNumber = Object.prototype.hasOwnProperty.call(options, 'list_number') ? options.list_number : true;
let result = `<ol class="${className}">`;
const lastNumber = [0, 0, 0, 0, 0, 0];
let firstLevel = 0;
let lastLevel = 0;

function getId(ele) {
const id = $(ele).attr('id');
const $parent = $(ele).parent();
return id || ($parent.length < 1 ? null : getId($parent));
}

headings.each(function() {
const level = +this.name[1];
const id = getId(this);
const text = escapeHTML($(this).text());
for (let i = 0, len = data.length; i < len; i++) {
const el = data[i];
const { level, id, text } = el;

lastNumber[level - 1]++;

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

lastLevel = level;
});
}

for (let i = firstLevel - 1; i < lastLevel; i++) {
result += '</li></ol>';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"archy": "^1.0.0",
"bluebird": "^3.5.2",
"chalk": "^3.0.0",
"cheerio": "0.22.0",
"hexo-cli": "^3.0.0",
"hexo-front-matter": "^1.0.0",
"hexo-fs": "^2.0.0",
Expand Down Expand Up @@ -71,6 +70,7 @@
"@easyops/git-exec-and-restage": "^1.0.4",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cheerio": "0.22.0",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.0.0",
"hexo-renderer-marked": "^2.0.0",
Expand Down