|
|
@ -29,16 +29,25 @@ module.exports = async (job) => {
|
|
|
|
|
|
|
|
|
|
|
|
$('h1,h2,h3,h4,h5,h6').each((idx, el) => {
|
|
|
|
$('h1,h2,h3,h4,h5,h6').each((idx, el) => {
|
|
|
|
const depth = _.toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2)
|
|
|
|
const depth = _.toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2)
|
|
|
|
|
|
|
|
let leafPathError = false
|
|
|
|
|
|
|
|
|
|
|
|
const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => {
|
|
|
|
const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => {
|
|
|
|
if (_.has(toc, curPath)) {
|
|
|
|
if (_.has(toc, curPath)) {
|
|
|
|
const lastLeafIdx = _.get(toc, curPath).length - 1
|
|
|
|
const lastLeafIdx = _.get(toc, curPath).length - 1
|
|
|
|
curPath = `${curPath}[${lastLeafIdx}].children`
|
|
|
|
if (lastLeafIdx >= 0) {
|
|
|
|
|
|
|
|
curPath = `${curPath}[${lastLeafIdx}].children`
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
leafPathError = true
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return curPath
|
|
|
|
return curPath
|
|
|
|
}, 'root')
|
|
|
|
}, 'root')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (leafPathError) { return }
|
|
|
|
|
|
|
|
|
|
|
|
const leafSlug = $('.toc-anchor', el).first().attr('href')
|
|
|
|
const leafSlug = $('.toc-anchor', el).first().attr('href')
|
|
|
|
$('.toc-anchor', el).remove()
|
|
|
|
$('.toc-anchor', el).remove()
|
|
|
|
|
|
|
|
|
|
|
|
_.get(toc, leafPath).push({
|
|
|
|
_.get(toc, leafPath).push({
|
|
|
|
title: _.trim($(el).text()),
|
|
|
|
title: _.trim($(el).text()),
|
|
|
|
anchor: leafSlug,
|
|
|
|
anchor: leafSlug,
|
|
|
|