有没有解决Hexo TOC生成问题的补丁方法?

更新于
2026-09-30 20:53:24
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计415个文字,预计阅读时间需要2分钟。

有没有解决Hexo TOC生成问题的补丁方法?

javascripttoc.js: 加载 cheerio 库,处理字符串内容,设置最大深度

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);

有没有解决Hexo TOC生成问题的补丁方法?

本文共计415个文字,预计阅读时间需要2分钟。

有没有解决Hexo TOC生成问题的补丁方法?

javascripttoc.js: 加载 cheerio 库,处理字符串内容,设置最大深度

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);

有没有解决Hexo TOC生成问题的补丁方法?