Safe Haskell | None |
---|---|
Language | Haskell2010 |
This library lets you parse Markdown into a hierarchical structure (delimited by headings). For instance, let's say your document looks like this:
This is the preface. First chapter ======================================== This chapter doesn't have sections. Second chapter ======================================== First section -------------------- Here's some text. Second section -------------------- And more text.
It can be represented as a tree:
preface
: "This is the preface."sections
: *heading
: "First chapter"content
: "This chapter doesn't have sections."sections
: [] *heading
: "Second chapter"sections
: *heading
: "First section"content
: "Here's some text."sections
: [] *heading
: "Second section"content
: "And more text."sections
: []
That's what this library does. Moreover, it lets you access the Markdown source of every node of the tree.
In most cases the only thing you need to do is something like this:
nodesToDocument
.commonmarkToNodesWithSource
[optSafe
,optNormalize
]
You can preprocess parsed Markdown after doing commonmarkToNodesWithSource
as long as you don't add or remove any top-level nodes.
- commonmarkToNodesWithSource :: [CMarkOption] -> Text -> WithSource [Node]
- nodesToDocument :: WithSource [Node] -> Document () ()
- data WithSource a = WithSource Text a
- getSource :: WithSource a -> Text
- stripSource :: WithSource a -> a
- data Section a b = Section {
- level :: Int
- heading :: WithSource [Node]
- headingAnn :: a
- content :: WithSource [Node]
- contentAnn :: b
- data Document a b = Document {
- preface :: WithSource [Node]
- prefaceAnn :: b
- sections :: Forest (Section a b)
- flattenDocument :: Document a b -> WithSource [Node]
- flattenSection :: Section a b -> WithSource [Node]
- flattenTree :: Tree (Section a b) -> WithSource [Node]
- flattenForest :: Forest (Section a b) -> WithSource [Node]
Parse Markdown to trees
commonmarkToNodesWithSource :: [CMarkOption] -> Text -> WithSource [Node] Source #
commonmarkToNodesWithSource
parses Markdown with the given options and
extracts nodes from the initial DOCUMENT
node.
nodesToDocument :: WithSource [Node] -> Document () () Source #
Turn a list of Markdown nodes into a tree.
data WithSource a Source #
A data type for annotating things with their source. In this library we
only use WithSource [Node]
, which stands for “some Markdown nodes + source”.
Functor WithSource Source # | |
Foldable WithSource Source # | |
Traversable WithSource Source # | |
Eq a => Eq (WithSource a) Source # | |
Data a => Data (WithSource a) Source # | |
Show a => Show (WithSource a) Source # | |
Generic (WithSource a) Source # | |
Monoid a => Monoid (WithSource a) Source # | |
type Rep (WithSource a) Source # | |
getSource :: WithSource a -> Text Source #
Extract source from WithSource
(it's stored there in a field).
stripSource :: WithSource a -> a Source #
Extract data from WithSource
.
A section in the Markdown tree.
Sections do not contain subsections; i.e. Section
isn't recursive and the
tree structure is provided by Data.Tree.
In a Section a b
, the heading is coupled with a value of type a
, and
content – with a value of type b
. This is occasionally useful.
Section | |
|
The whole parsed Markdown tree. In a Document a b
, headings are
annotated with a
and content blocks – with b
.
Document | |
|
Work with parsed trees
Note that you can use (<>
) to combine WithSource
nodes together. It will
concatenate sources and parsed Markdown.
I'm not sure how valid this operation is for Markdown, but probably more-or-less valid (when you exclude corner cases like missing newlines at the end and duplicate links). Maybe cmark doesn't even allow duplicate links, I don't know.
flattenDocument :: Document a b -> WithSource [Node] Source #
Turn the whole parsed-and-broken-down Document
into a list of nodes.
flattenSection :: Section a b -> WithSource [Node] Source #
Turn a section into a list of nodes.
flattenTree :: Tree (Section a b) -> WithSource [Node] Source #
flattenForest :: Forest (Section a b) -> WithSource [Node] Source #