theisel / astro-toc Goto Github PK
View Code? Open in Web Editor NEWTable of Contents (ToC) generator for Astro. Use custom components for interactivity.
License: ISC License
Table of Contents (ToC) generator for Astro. Use custom components for interactivity.
License: ISC License
Here's an example list of headers in markdown:
## hello
## world
### a. test
### b. this
The H3 header ### b. this
does not show up.
The culprit are lines 39-40 of TOC.astro
:
const nextHeading = headings[idx + 1]
const subHeadings = toc.slice((toc as any).indexOf(it) + 1, (toc as any).indexOf(nextHeading));
If we're iterating through all the headings with depth=2
, then when we're at the last element ## world
, there is no more heading after it. This causes nextHeading
to be undefined.
So when calculating subHeadings
, the second argument passed into slice is toc.indexOf(undefined)
which is -1
. This means that the slice will only return the next item, and not retrieve the last item.
const nextHeading = headings[idx + 1];
let subHeadings = toc.slice((toc as any).indexOf(it) + 1, (toc as any).indexOf(nextHeading));
if (!nextHeading) subHeadings = toc.slice((toc as any).indexOf(it) + 1);
If nextHeading
is undefined, then subHeadings
will just grab the remainder of toc
.
I am using <TOC />
but I noticed that it does not render when the toc
only includes h2 and h3 headings. Currently, I am quickfixing it by manually pushing an h1 heading (depth: 1
) into my toc:
toc.unshift({
title: 'Glossary',
depth: 1,
url: '',
});
Would it be possible to render a TOC even when there is no h1?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.