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

Markdown parser for table of contents #8347

Open
Levi-Lesches opened this issue Dec 2, 2024 · 1 comment
Open

Markdown parser for table of contents #8347

Levi-Lesches opened this issue Dec 2, 2024 · 1 comment

Comments

@Levi-Lesches
Copy link

Levi-Lesches commented Dec 2, 2024

Continuing the conversation from #4371, the goal is to create a parser that accepts markdown as input as spits out a list of headings that can be easily formatted into a table of contents or similar.

@isoos From the other thread:

The TocNode is an incomplete API for this use case: it doesn't hold the content, it was created for a different reason, and while it could be simple to reuse it, it may be better to just have a clean start. As we really want to use this for changelog parsing too, it could be a better starting point to collect different kind of requirements for that and how it would look in a parsed form.

For reference, here is the TocNode class:

/// Describes a tree node in the table of contents hierarchy.
class TocNode {
/// The text label to be display.
final String label;
/// The link href to use, if omitted, only a regular text is displayed.
final String? href;
/// Children nodes of this node.
List<TocNode>? children;
TocNode(
this.label, {
this.href,
this.children,
});
}

Can you elaborate here:

  • Why is the content needed? For either the changelog or details page, isn't it enough to just have the title be a clickable link? Right now it is a string, I can definitely change it to be a markdown Node, but I don't see where we'd want the content to be
  • What exactly do we want for the Changelog page? A similar ToC of all releases? Should we nest for major/minor/patches? That would be tricky if the author did not use nested headers. I went through some older issues and PRs and couldn't really find a clear description of exactly what's wanted here
  • Should I make my new API be the new TocNode and refactor the admin page to use that?
@Levi-Lesches
Copy link
Author

Levi-Lesches commented Dec 2, 2024

I opened #8348 with my first pass at the parser, marked as draft because I know you still have some points to address. Here's the new API:

/// A node of the Table of Contents
class TocNode {
  /// What level heading this node is.
  ///
  /// This is not defined by what tag it is using, or how many `#` it has, but rather
  /// how many levels of nesting have occurred in the document so far. That is to say,
  ///
  /// ```md
  /// # Level 1
  /// ### Level 2
  /// ##### Level 3
  /// ```
  int level;

  /// The list of [TocNode] that are nested under this heading.
  List<TocNode> children;

  /// The title of the node, as a string.
  final String title;

  /// The parent heading for this node.
  TocNode? parent;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants