当前位置 : 主页 > 网络编程 > JavaScript >

Hexo toc patch

来源:互联网 收集:自由互联 发布时间:2021-06-28
toc.js // content of node_modules/hexo/lib/plugins/helper/toc.js'use strict';var cheerio;function tocHelper(str, options) { options = options || {}; if (!cheerio) cheerio = require('cheerio'); var $ = cheerio.load(str); var headingsMaxDepth
toc.js
// content of node_modules/hexo/lib/plugins/helper/toc.js
'use strict';

var cheerio;

function tocHelper(str, options) {
    options = options || {};

    if (!cheerio) cheerio = require('cheerio');

    var $ = cheerio.load(str);
    var headingsMaxDepth = options.hasOwnProperty('max_depth') ? options.max_depth : 6;
    var headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
    var headings = $(headingsSelector);

    if (!headings.length) return '';

    var className = options.class || 'toc';
    var listNumber = options.hasOwnProperty('list_number') ? options.list_number : true;
    var result = '
 
    '; var lastNumber = [0, 0, 0, 0, 0, 0]; var firstLevel = 0; var lastLevel = 0; var i = 0; headings.each(function () { var level = +this.name[1]; var id = $(this).attr('id'); var text = $(this).html(); lastNumber[level - 1]++; for (i = level; i <= 5; i++) { lastNumber[i] = 0; } if (firstLevel) { for (i = level; i < lastLevel; i++) { result += '
'; } if (level > lastLevel) { result += '
    '; } else { result += ''; } } else { firstLevel = level; } result += '
  1. '; result += ''; if (listNumber) { result += ''; for (i = firstLevel - 1; i < level; i++) { result += lastNumber[i] + '.'; } result += ' '; } result += '' + text + ''; lastLevel = level; }); for (i = firstLevel - 1; i < lastLevel; i++) { result += '
'; } return result; } module.exports = tocHelper;
tocPatch.js
// content of /script/tocPatch.js
'use strict';

var stripHTML = require('hexo-util').stripHTML;

var cheerio;

function tocHelper(str, options) {
    options = options || {};

    if (!cheerio) cheerio = require('cheerio');

    var $ = cheerio.load(str);
    var headingsMaxDepth = options.hasOwnProperty('max_depth') ? options.max_depth : 6;
    var headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
    var headings = $(headingsSelector);

    if (!headings.length) return '';

    var className = options.class || 'toc';
    var listNumber = options.hasOwnProperty('list_number') ? options.list_number : true;
    var result = '
 
    '; var lastNumber = [0, 0, 0, 0, 0, 0]; var firstLevel = 0; var lastLevel = 0; var i = 0; headings.each(function () { var level = +this.name[1]; var id = $(this).attr('id'); var text = $(this).html(); lastNumber[level - 1]++; for (i = level; i <= 5; i++) { lastNumber[i] = 0; } if (firstLevel) { for (i = level; i < lastLevel; i++) { result += '
'; } if (level > lastLevel) { result += '
    '; } else { result += ''; } } else { firstLevel = level; } result += '
  1. '; result += ''; if (listNumber) { result += ''; for (i = firstLevel - 1; i < level; i++) { result += lastNumber[i] + '.'; } result += ' '; } result += '' + stripHTML(text) + ''; lastLevel = level; }); for (i = firstLevel - 1; i < lastLevel; i++) { result += '
'; } return result; } hexo.extend.helper.register('toc', tocHelper);
网友评论