'use strict'; const { tocObj, escapeHTML, encodeURL } = require('hexo-util'); function tocHelper(str, options = {}) { options = Object.assign({ min_depth: 1, max_depth: 6, class: 'toc', list_number: true }, options); const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth }); if (!data.length) return ''; const className = escapeHTML(options.class); const listNumber = options.list_number; let result = `
    `; const lastNumber = [0, 0, 0, 0, 0, 0]; let firstLevel = 0; let lastLevel = 0; for (let i = 0, len = data.length; i < len; i++) { const el = data[i]; const { level, id, text } = el; const href = id ? `#${encodeURL(id)}` : null; lastNumber[level - 1]++; for (let i = level; i <= 5; i++) { lastNumber[i] = 0; } if (firstLevel) { for (let i = level; i < lastLevel; i++) { result += '
'; } if (level > lastLevel) { result += `
    `; } else { result += ''; } } else { firstLevel = level; } result += `
  1. `; if (href) { result += ``; } else { result += ``; } if (listNumber) { result += ``; for (let i = firstLevel - 1; i < level; i++) { result += `${lastNumber[i]}.`; } result += ' '; } result += `${text}`; lastLevel = level; } for (let i = firstLevel - 1; i < lastLevel; i++) { result += '
'; } return result; } module.exports = tocHelper;