Closed HHongSeungWoo closed 3 years ago
function search(node) {
const headings = [];
const minDepth = 1;
const maxDepth = 4;
const current = Array(maxDepth - minDepth + 1).fill(-1);
let previousDepth = 0;
const onHeading = (child, index, parent) => {
const value = toString(child);
if (parent !== node || !value || child.depth > maxDepth || child.depth < minDepth) {
return;
}
const entry = {
value: toValue(child),
id: child.data.id,
children: [],
};
if (!headings.length || previousDepth >= child.depth) {
previousDepth = child.depth;
current.fill(-1, child.depth - minDepth + 1)
}
index = child.depth - minDepth;
let table = headings;
for(let i = 0; i < index; i++) {
table = table[current[i]].children;
}
table.push(entry);
current[index] += 1;
};
visit(node, 'heading', onHeading);
return headings;
}
이렇게 적용함
@docusaurus/mdx-loader/src/remark/rightToc/search.js