Skip to content

Feature request: Generate a table of contents #1456

Open
@Levi-Lesches

Description

@Levi-Lesches

Something like this:

/// A section 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.
  String title;

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

  /// Renders this content to markdown
  List<Node> renderToMarkdown();
}

class Document { 
  /// Generates a Table of Contents for this document. 
  List<TocNode> generateToc();  // or generateTableOfContents()
}

I may be misunderstanding Document here -- its API does not include a List<Node>, so given the typical approach of:

final doc = Document()
final nodes = doc.parse(markdown);

would I need to make a TocNode generataeToc(List<Node> nodes) instead?

Code for parsing can be found at dart-lang/pub-dev#8348. See the discussion there for context, but the motivation is to eventually generate a ToC on the side of Pub package pages.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions