blog/themes/embark/scripts/toc.js

58 lines
1.4 KiB
JavaScript

const path = require('path');
const nunjucks = require('nunjucks');
const cheerio = require('cheerio');
hexo.extend.helper.register('toc', function (str, options = {}) {
const $ = cheerio.load(str);
const headingsMaxDepth = options.hasOwnProperty('max_depth') ? options.max_depth : 6;
const headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
const headings = $(headingsSelector);
if (!headings.length) return '';
const className = 'o-list-bare';
let result = `<ol class="${className}">`;
const lastNumber = [0, 0, 0, 0, 0, 0];
let firstLevel = 0;
let lastLevel = 0;
let i = 0;
headings.each(function() {
const level = +this.name[1];
const id = $(this).attr('id');
const text = $(this).text();
lastNumber[level - 1]++;
for (i = level; i <= 5; i++) {
lastNumber[i] = 0;
}
if (firstLevel) {
for (i = level; i < lastLevel; i++) {
result += '</li></ol>';
}
if (level > lastLevel) {
result += `<ol class="${className}__child">`;
} else {
result += '</li>';
}
} else {
firstLevel = level;
}
result += `<li class="${className}__item ${className}-level-${level} o-ellipsis">`;
result += `<a href="#${id}" class="u-link-uniform">${text}</a>`;
lastLevel = level;
});
for (i = firstLevel - 1; i < lastLevel; i++) {
result += '</li></ol>';
}
return result;
})