GithubHelp home page GithubHelp logo

theisel / astro-toc Goto Github PK

View Code? Open in Web Editor NEW
23.0 23.0 1.0 196 KB

Table of Contents (ToC) generator for Astro. Use custom components for interactivity.

License: ISC License

Shell 9.66% Astro 87.35% JavaScript 2.98%
astro astro-component ui withastro

astro-toc's People

Contributors

github-actions[bot] avatar theisel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

m4k15y6666fk

astro-toc's Issues

Does not render last H3

Describe the bug

Here's an example list of headers in markdown:

## hello
## world
### a. test
### b. this

The H3 header ### b. this does not show up.

Debugging

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.

Proposed fix

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.

Doesn't render without tag of depth 1

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.