Using the Markdown Library

The most central part of the Markdown library is the function parse:

As can be seen above, the function returns a markdown.Document on success. This class represents the root of the Markdown document as a sequence of elements (markdown.ElementSeq). The Document itself has the following members apart from the toS member that outputs a formatted version of the document as Markdown:

Elements

The document consists of a list of elements. Each element corresponds to a piece of the document. Some elements, such as lists, may themselves contain other elements, or lists of elements. The document thus follows a tree-like structure.

As noted above, lists of elements are typically stored as the class markdown.ElementSeq internally, in order to share some of the logic related to traversing such lists. The class has a member elements that is the stored array of elements. The ElementSeq class is not an element itself, and is typically safe to ignore in most cases.

The class markdown.Element contains the following members, that all other elements need to override:

The following elements are provided by the library (and generated by the parser):

Formatted Text

The class markdown.FormattedText represents a piece of text with formatting. Most of the elements use this class to represent their contained text. A FormattedText instance is a list of markdown.TextSpan classes that each represent individual pieces of the text, each with possibly different formatting. As we shall see, some TextSpans contain FormattedText objects themselves. This makes it possible to apply more than one formatting option to a single piece of text.

The FormattedText class has the following members:

The TextSpan class is similar to the Element class, and has the following members that subclasses need to implement:

The following spans are provided by the library:

Visitors

The classes above all have a visit function to allow traversing the hierarchy conveniently. The visit functions take a Visitor as a parameter, and calls one of the member functions for each element. Each of the member functions return an instance of the same object that was received. The visitor function uses the returned object to replace the original one. In this way, it is possible to also replace elements in the hierarchy, regardless of what element they are located inside.

If it is necessary to replace one element with multiple elements, the classes markdown.PackedElements and markdown.PackedText can be used.

The Visitor class has the following members. The default implementation of all of them is to simply return the object passed as a parameter, so that they implement a no-op by default: