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

🗳 Add new table-of-contents validator #1151

Merged
merged 14 commits into from Apr 30, 2024
Merged

Conversation

agoose77
Copy link
Collaborator

@agoose77 agoose77 commented Apr 23, 2024

In #1102, we identified the need for a new table of contents format.

This format must satisfy the following goals:

  1. Easy to humanly read/write
  2. Easy to programatically read/write
  3. Expressive enough to represent complex trees

This PR is a WIP in that direction. The TOC schema can be build using

npm run build:schema --workspace=packages/myst-toc

and then used with https://www.jsonschemavalidator.net/ (or the CLI equivalent) to verify a TOC JSON blob.

Here's an example "complex" TOC:

  - title: Main
    file: main.md
  - title: Overview
    children:
      - file: overview-1.md
      - file: overview-2.md
  - url: https://google.com
  - file: getting-started.md
    children:
      - file: getting-started-part-1.md
      - file: getting-started-part-2.md

There are notably two kinds of "subtrees" in MyST:

  1. Non-hyperlink categories (grouping)
  2. Categories with index pages (Sphinx-like)

This TOC supports both; a bare children entry requires a title, whereas a file/URL entry does not.

@rowanc1 also mentioned the ability to choose between drop-downs and fixed-headings. This TOC format has a class string field that we could use to support that.

We should iterate on this as necessary!

Closes #1102

@agoose77 agoose77 requested review from rowanc1 and fwkoch April 23, 2024 13:36
@rowanc1
Copy link
Member

rowanc1 commented Apr 23, 2024

Looking good, some thoughts below as I go through this.

Some of the things we can maybe choose to add:

  • id: be able to reference a part of the toc
  • numbered: (maybe numbering?) We have had a lot of requests to have full-book numbering rather than per page. This is a step in that direction, I think that numbered would be validated to numbering as well (defined in frontmatter already, important for figure: start: 5 and enumerator: 1.%s), which has the enumerator to be used on the page.
  • part? Something to indicate that this is a chapter, appendix, or dedication, etc. I think this is different than a class, which is about visual styles. This would be helpful for placing in templates.
  • hidden

Options like glob can be read at validation time, and coerced into this format. Also implicit folders, similar to the default behaviour now.

Is there some way to apply default classes? i.e. a top-level kind: book-sections? Or do we want people to write class: heading on each? Maybe this is also validation/coersion to this format? I am not sure what the ajv validation is used for in that case - if the user is never interacting with this format?

@agoose77
Copy link
Collaborator Author

agoose77 commented Apr 23, 2024

Notes from a meeting on this:

  • We don't need a root document; we can use the first document (array of entries).
  • part should be singular, we can pluralise it later if needs be
  • numbering needed, should inherit?
  • id needed for all entries
  • Normalise pattern entry type to array of file, don't use internally after normalisation.
  • hidden needed, should inherit
  • class and classes are plural vs singular
  • We want to define the numbering style in future, as a style-field?

@agoose77 agoose77 self-assigned this Apr 24, 2024
@agoose77
Copy link
Collaborator Author

@rowanc1 I've updated this PR to add id, hidden, part fields to all entries. We don't have class on all entries; right now it's limited to parents. Is that reasonable, or do we have need of it on every entry?

@agoose77
Copy link
Collaborator Author

@rowanc1 the schema generator has a dependency on NodeJS>=18 due to structuredClone. What are your thoughts here; it's only a development dependency, we probably want to try and patch it right?

@rowanc1
Copy link
Member

rowanc1 commented Apr 25, 2024

I think that we could drop node 16 actually and start testing 22. We should do that in another PR and update a few parts of the docs and the min version.

image

@rowanc1
Copy link
Member

rowanc1 commented Apr 25, 2024

I think class makes sense on every node? Thinking about that beta-feature example with a custom icon in the TOC which exists only on a child.

@agoose77 agoose77 marked this pull request as ready for review April 25, 2024 15:57
@rowanc1
Copy link
Member

rowanc1 commented Apr 25, 2024

@agoose77 #1162 is merged if you want to rebase here and get the tests passing?

@agoose77 agoose77 force-pushed the agoose77/feat-define-toc-format branch from 26ab792 to d0414f9 Compare April 26, 2024 07:51
@agoose77
Copy link
Collaborator Author

@rowanc1 is this good to go in your view? If so, feel free to merge! The plan is that another PR will hook this into the config side of MyST-MD

@rowanc1
Copy link
Member

rowanc1 commented Apr 26, 2024

I will take it for a spin this afternoon and get it in and released!

@agoose77 agoose77 force-pushed the agoose77/feat-define-toc-format branch from 1681244 to 1828e6c Compare April 29, 2024 08:03
Copy link
Member

@rowanc1 rowanc1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types look good here. I think things will change a bit when we integrate it with myst-frontmatter, but that can happen in the next iteration.

content:
- file: foo.md
hidden: "yes"
throws: 'The given contents do not form a valid TOC'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to get nicer errors here to show to users. I suspect when we integrate this into myst-frontmatter, we will use the simple-validators as that does now, which will allow us to have coercion and better validation messages that work with where we are at now.

@rowanc1 rowanc1 merged commit 50d0375 into main Apr 30, 2024
5 checks passed
@rowanc1 rowanc1 deleted the agoose77/feat-define-toc-format branch April 30, 2024 13:40
@rowanc1 rowanc1 changed the title 🗳️ Add new table-of-contents validator 🗳 Add new table-of-contents validator Apr 30, 2024
@agoose77 agoose77 mentioned this pull request May 7, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a new table of contents format
2 participants