docs: correctly render table of contents [ci skip]

This commit is contained in:
Eric Liu 2022-08-06 13:37:49 -07:00
commit f1caf83103

View file

@ -163,71 +163,30 @@ module.exports = {
{ {
markup({ content, filename }) { markup({ content, filename }) {
if (/node_modules/.test(filename)) return null; if (/node_modules/.test(filename)) return null;
if (filename.endsWith(".svx")) { if (!filename.match(/pages\/(components)/)) return null;
const toc = [];
walk(parse(content), { const toc = [];
enter(node) {
if (node.type === "Element") { walk(parse(content), {
if (node.name === "h2") { enter(node) {
const id = node.attributes.find( if (node.type === "Element") {
(attribute) => attribute.name === "id" if (node.name === "h3") {
); const id = node.attributes.find(
toc.push({ (attribute) => attribute.name === "id"
id: id.value[0].raw, );
text: node.children[0].raw, toc.push({
}); id: id.value[0].raw,
} text: node.children[0].raw,
});
} }
}, }
}); },
});
return { return {
code: content.replace( code: content.replace(
"</Layout_MDSVEX_DEFAULT>", "</Layout_MDSVEX_DEFAULT>",
`<div slot="aside"> `<div slot="aside">
<ul class="bx--list--unordered">
${toc
.map((item) => {
return `
<li class="bx--list__item">
<a class="bx--link" href="\#${item.id}">${item.text}</a>
</li>`;
})
.join("")}
</ul>
</div>
</Layout_MDSVEX_DEFAULT>`
),
};
}
if (
filename.endsWith(".svx") &&
filename.match(/pages\/(components)/)
) {
const toc = [];
walk(parse(content), {
enter(node) {
if (node.type === "Element") {
if (node.name === "h3") {
const id = node.attributes.find(
(attribute) => attribute.name === "id"
);
toc.push({
id: id.value[0].raw,
text: node.children[0].raw,
});
}
}
},
});
return {
code: content.replace(
"</Layout_MDSVEX_DEFAULT>",
`<div slot="aside">
<ul class="bx--list--unordered"> <ul class="bx--list--unordered">
<li class="bx--list__item"> <li class="bx--list__item">
<a class="bx--link" href="#usage">Usage</a> <a class="bx--link" href="#usage">Usage</a>
@ -268,9 +227,8 @@ module.exports = {
</ul> </ul>
</div> </div>
</Layout_MDSVEX_DEFAULT>` </Layout_MDSVEX_DEFAULT>`
), ),
}; };
}
}, },
}, },
], ],