dart-lang / pub-dev

The pub.dev website
https://pub.dev
BSD 3-Clause "New" or "Revised" License
802 stars 148 forks source link

Markdown parser for table of contents #8347

Open Levi-Lesches opened 1 day ago

Levi-Lesches commented 1 day ago

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:

https://github.com/dart-lang/pub-dev/blob/4acdc2bdc3dce95832ad9171f2f2fef4f04a5fad/app/lib/frontend/templates/views/shared/toc.dart#L35-L51

Can you elaborate here:

Levi-Lesches commented 1 day ago

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;
}