hhvm/fbmarkdown

Name: fbmarkdown

Owner: HipHop Virtual Machine

Description: Pure-Hack implementation of GitHub Flavored Markdown, based on the specification at https://github.github.com/gfm/

Created: 2017-12-20 04:43:39.0

Updated: 2018-05-10 17:35:36.0

Pushed: 2018-05-10 17:35:34.0

Homepage: null

Size: 264

Language: Hack

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

FBMarkdown

FBMarkdown is an extensible parser and renderer for GitHub Flavored Markdown, written in Hack.

It is used to render the Hack and HHVM documentation.

Why another Markdown library?

For docs.hhvm.com, we wanted:

Originally, the Ruby GFM pipeline was the best fit; over time, we started to want to:

FBMarkdown exists to address all of these goals.

Requirements
Installing FBMarkdown
hhvm composer.phar require facebook/fbmarkdown
Using FBMarkdown
namespace Facebook\Markdown;

tion render(string $markdown): string {
st = Markdown\parse(new Markdown\ParserContext(), $markdown);

tml = (new Markdown\HTMLRenderer(
new Markdown\RenderContext()
->render($ast);

turn $html;

For complete compatibility with GitHub Flavored Markdown, support for embedded HTML must be enabled; it is disabled by default as a security precaution.

 = (new Markdown\ParserContext())->enableHTML_UNSAFE();
 = Markdown\parse($ctx, $markdown);

If you are re-using contexts to render multiple independent snippets, you will need to call ->resetFileData() on the context.

How FBMarkdown works
Parsing
  1. The classes in the Facebook\Markdown\UnparsedBlocks namespace convert markdown text to a tree of nodes representing the block structure of the document, however the content of the blocks is unparsed.
  2. The contents of the blocks ('inlines') are parsed using the classes in the Facebook\Markdown\Inlines namespace.
  3. Finally, the classes of the Facebook\Markdown\Blocks namespace are used to represent the fully parsed AST - blocks and Inlines.
Rendering

The AST is recursively walked, emitting output for each note - e.g. the HTML renderer produces strings.

Extending FBMarkdown

There are 2 main ways to extend FBMarkdown: extending the parser, and transforming the AST.

Extending The Parser
Inlines

Extend Facebook\Markdown\Inlines\Inline or a subclass, and pass your classname to $render_ctx->getInlineContext()->prependInlineTypes(...).

There are then several approaches to rendering:

Blocks

You will need to implement the Facebook\Markdown\UnparsedBlocks\BlockProducer interface, and pass your classname to $render_ctx->getBlockContext()->prependBlockTypes(...).

There are then several approaches to rendering:

Transforming The AST

Extend Facebook\Markdown\RenderFilter, and pass it to $render_ctx->appendFilters(...).

Examples

The Hack and HHVM documentation uses most of these approaches; see:

License

FBMarkdown is MIT-licensed.

FBMarkdown may contain third-party software; see third_party_notices.txt for details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.