Modules · Docs · 0.23

Markdown::Simple

Markdown to HTML in C, GitHub flavoured, with anchors and highlighting.

GitHub Flavored Markdown by default, strict CommonMark with one option, and each extension on or off by itself. Headings take GitHub's anchors, render_with_toc returns the list to link them from, and fenced code is highlighted by Eshu. The posts and docs on this site are rendered by it.

use Markdown::Simple;

my $md = Markdown::Simple->new({ highlight => 1 });

my ($html, $toc) = $md->render_with_toc(<<'MD');
# Getting started

## Install it

```perl
use Markdown::Simple;
```

## The **first** page
MD

# every heading has an anchor, and the list links to them
print qq{<a href="#$_->{id}">$_->{text}</a>\n} for @$toc;
print $html;

What you get

  • GitHub flavoured, or CommonMark

    Tables, strikethrough, task lists, autolinks and escaped raw HTML by default. gfm => 0 gives strict CommonMark, and any extension or construct can be toggled by itself.

  • Anchors and a contents list

    heading_ids gives every heading GitHub's anchor, unique in the document, and render_with_toc returns the headings with them, ready to link.

  • Highlighted code

    With highlight => 1, a fenced block with a language is highlighted by Eshu, into spans that stay HTML-safe.

  • Safe by default

    javascript:, vbscript: and data: URLs are dropped unless you pass unsafe, and strict_utf8 refuses malformed input instead of guessing.

  • A warm session

    An object keeps its parser arena between renders, so a loop over many documents does not touch the allocator. strip_markdown gives the text for a search index.

In use

use Markdown::Simple qw(markdown_to_html strip_markdown);

my $doc = <<'MD';
| task       | done |
|------------|------|
| ~~write~~  | yes  |

- [x] tables
- [ ] <script>alert(1)</script>
MD

print markdown_to_html($doc);                 # GitHub flavoured: raw HTML is escaped
print strip_markdown($doc);                   # the text, for a search index

For C

mds_abi.h, resolved at run time through Markdown::Simple::_abi_ptr: five entries to decode options, render with or without a warm session, collect the headings and strip to text. Nothing in it croaks. Punk's markdown mounts render this site's posts and docs through it.

Get it

cpanm Markdown::Simple