Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide tree of headings in @island/headings #226

Open
dhruvkb opened this issue Jan 30, 2023 · 3 comments
Open

Provide tree of headings in @island/headings #226

dhruvkb opened this issue Jan 30, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@dhruvkb
Copy link
Contributor

dhruvkb commented Jan 30, 2023

Is your feature request related to a problem? Please describe.
Since the @islands/headings packages provides headings as a list, instead of a tree, I am unable to render a proper tree in the table of contents.

Describe the solution you'd like
I would prefer that during parsing of the AST, headings be compiled into a tree-structure instead of (or in addition to) a list so that the relationships between headings is also available in the post.meta field.

Describe alternatives you've considered
This could be accomplished by a separate plugin but a lot of the code would be duplicated between the @islands/heading module and the separate one.

Additional context
Having the headings as a tree would make it possible to index the headings and provide meaningful numbering to them as well. For example, something like this would be possible if the headings were a tree.

§1 Chapter
  §1.1 Section
  §1.2 Section
    §1.2.1 Subsection
§2 Chapter
...

I'm happy to make a PR if this change seems useful to the general user base.

@ElMassimo
Copy link
Owner

You can already create a tree structure from the list of headings.

Would you describe what structure you would expect to see when using "tree mode"?

@ElMassimo ElMassimo added the enhancement New feature or request label Jan 30, 2023
@dhruvkb
Copy link
Contributor Author

dhruvkb commented Jan 30, 2023

I extended PageMeta to inherit from the interface NestedHeading.

export interface NestedHeading {
  subheadings: NestedHeading[] // contains headings that are children of this one
  indices: number[]            // useful for building numbered chapters, sections etc.
}

This way I can recursively iterate over subheadings field and build the tree of headings when parsing the AST. Each node also has a data-indices attr injected on it which is indices.join('.'), and I am using CSS to prefix the §x.y.z before it.

@ElMassimo
Copy link
Owner

I see your point about creating a separate module:

a lot of the code would be duplicated

Given that this feature seems reasonably common, it might make sense to include this in @islands/headings out of the box, under a nested: true opt-in setting (or similar).

Feel free to make a PR, and we can discuss what the API would look like. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants