MMark (read “em-mark”) is a strict markdown processor for writers. “Strict”
means that not every input is considered valid markdown document and parse
errors are possible and even desirable, because they allow to spot markup
issues without searching for them in rendered document. If a markdown
document passes MMark parser, then it'll likely produce HTML without quirks.
This feature makes it a good choice for writers and bloggers.
MMark in its current state features:
A parser that produces high-quality error messages and does not choke on
first parse error. It is capable of reporting many parse errors where
An extension system allowing to create extensions that alter parsed
markdown document in some way. Some of them are available in the
There is also a blog post announcing the project:
Quick start: MMark vs GitHub-flavored markdown
It's easy to start using MMark if you're used to GitHub-flavored markdown.
There are three main differences:
URIs are not automatically recognized, you must to enclose them in
Block quotes require only one
> and they continue as long as long the
inner content is indented.
This is OK:
> Here goes my block quote.
And this is the second line of the quote.
This produces two block quotes:
> Here goes my block quote.
> And this is another block quote!
See differences in inline parsing.
MMark and Common Mark
MMark mostly tries to follow the Common Mark specification as given here:
However, due to the fact that we do not allow inputs that do not make sense,
and also try to guard against common mistakes (like writing
and having it rendered as a paragraph starting with hashes) MMark obviously
can't follow the specification precisely. In particular, parsing of inlines
differs considerably from Common Mark (see below).
Another difference between Common Mark and MMark is that the latter supports
more (pun alert) common markdown extensions out-of-the-box. In particular,
- parsing of an optional YAML block
- strikeout using
- superscript using
- subscript using
- automatic assignment of ids to headers
- pipe tables (as on GitHub)
One do not need to enable or tweak anything for these to work, they are
Differences in inline parsing
Emphasis and strong emphasis is an especially hairy topic in the Common Mark
specification. There are 17 ad-hoc rules defining interaction between
_ -based emphasis and more than an half of all Common Mark examples
(that's about 300) test just this tricky logic.
Not only it is hard to implement, it's hard to understand for humans too.
For example, this input:
results in the following HTML:
(Note the nested emphasis.)
Could it produce something like this instead?
Well, why not? Without remembering those 17 ad-hoc rules, there going to be
a lot of tricky cases when a user won't be able to tell how markdown will be
I decided to make parsing of emphasis, strong emphasis, and similar
constructs like strikethrough, subscript, and superscript more symmetric and
less ad-hoc. This is a work in progress and I'm not fully satisfied with the
current approach as it does not allow to express some combinations of
characters and markup, but in 99% of practical cases it is identical to
Common Mark, and normal markdown intuitions will work OK for the users.
Let's start by dividing all characters into three groups:
Markup characters, including the following:
]. These are used for markup and whenever they appear in a
document, they must form valid markup constructions. To be used as
ordinary punctuation characters they must be backslash escaped.
Space characters, including space, tab, newline and carriage return.
Other characters, which include all characters not falling into the
two groups described above.
Markup characters can be “converted” to other characters via
backslash escaping. We'll see how this is useful in a few moments.
We'll call markdown characters placed between space characters and
other characters left-flanking delimiter run. These markup characters
sort of hang on the left hand side of a word.
Similarly we'll call markdown characters placed between other
characters and space characters right-flanking delimiter run. These
hang on the right hand side of a word.
Emphasis markup (and other similar things like strikethrough, which we won't
mention explicitly anymore for brevity) can start only as left-flanking
delimiter run and end only as right-flanking delimiter run.
This produces a parse error:
*Something * is not right.
Something __is __ not right.
And this too:
This means that inter-word emphasis is not supported by this approach. (This
is a pity, maybe I should adjust something to allow it.)
There is one more tricky thing. In some cases we want to end emphasis and
have full stop or other punctuation right after it:
Here it *goes*.
You can see that the closing
* is not in right-flanking position here, and
so it's a parse error. To avoid this, some punctuation characters that
normally appear outside of markup were made “transparent” and thus they are
regarded as white space, so the example above parses correctly and works as
expected. To put a transparent character inside emphasis, backslash escaping
We *\(can\)* have it.
) are transparent punctuation characters, just like
they must be turned into other characters to go inside the emphasis.
This is a corner case and should not be common in practice.
So far the main limitation of this approach is the pains with inter-word
markup, as in this example:
**We started to work on the *issue*.**
Should we escape
. here? On one hand we should, to close
**. But if we
do, the closing
* won't be in right-flanking position anymore. God dammit.
- If a line starts with hash signs it is expected to be a valid non-empty
header (level 1–6 inclusive). If you want to start a paragraph with
hashes, just escape the first hash with backslash and that will be enough.
- Setext headings are not supported for the sake of simplicity.
- Fenced code blocks must be explicitly closed by a closing fence. They are
not closed by the end of document or by start of another block.
- Lists and block quotes are defined by column at which their content
starts. Content belonging to a particular list or block quote should start
at the same column (or greater column, up to the column where indented
code blocks start). As a consequence of this, block quotes do not feature
- Block quotes are started by a single
> character, it's not necessary to
> character at beginning of every line belonging to a quote (in
fact, this would make every line a separate block quote).
- Paragraphs can be interrupted by unordered and ordered lists with any
valid starting index.
- HTML blocks are not supported because the syntax conflicts with autolinks
and the feature is a hack to compensate for the lack of extensibility and
customization in the original markdown.
- MMark does not support hard line breaks represented as double space before
newline. Nevertheless, hard line breaks in the form of backslash before
newline are supported (these are more explicit too).
- All URI references (in links, images, autolinks, etc.) are parsed as per
RFC 3986, no support for escaping or support for entity and numeric
character references is provided. In addition to that, when a URI
reference in not enclosed with
>, then closing parenthesis
) is not considered part of URI (use
<uri> syntax if you
want a closing parenthesis as part of a URI). Since the empty string is a
valid URI and it may be confusing in some cases, we also force the user to
<> to represent the empty URI.
- Putting links in text of another link is not allowed, i.e. no nested links
- Putting images in description of other images is not allowed (similarly to
the situation with links).
- HTML inlines are not supported for the same reason why HTML blocks are not
About MMark-specific extensions
- YAML block must start with three hyphens
--- and end with three hyphens
---. It can only be placed at the beginning of a markdown document.
Trailing white space after the
--- sequences is allowed.
I have compared speed and memory consumption of various Haskell markdown
libraries by running them on an identical, big-enough markdown document and
by rendering it as HTML:
||Custom C code
||Custom Haskell code
Results are ordered from fastest to slowest.
markdown library is sloppy and parses markdown incorrectly. For
example, it parses the following
*My * text as an inline containing
emphasis, while in reality both asterisks must form flanking delimiter runs
to create emphasis, like so
*My* text. This allowed
markdown to get away
with a far simpler approach to parsing at the price that it's not really a
valid markdown implementation.
Issues, bugs, and questions may be reported in the GitHub issue tracker for
Pull requests are also welcome and will be reviewed quickly.
Copyright © 2017 Mark Karpov
Distributed under BSD 3 clause license.