introduction to league/commonmark
TRANSCRIPT
Introduction to league/commo
nmarkColin O’Dell
Colin O’DellLead Web Developer at Unleashed TechnologiesPHP developer since 2002Author of:• league/commonmark• league/html-to-markdown• PHP 7 Migration Guide e-book
@colinodellwww.colinodell.com
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
CommonMark is…A strongly defined, highly compatible specification of Markdown.
Written by people from Github, StackOverflow, Reddit, and others.
Spec includes:• Strict rules (precedence, parsing order, handling edge cases)• Specific definitions (ex: “whitespace”, “punctuation”)• 613 examples
Why is it needed?*I love Markdown*<p><em>I love Markdown</em></p>
Why is it needed?*I *love* Markdown*
Source: http://johnmacfarlane.net/babelmark2/
30%
Why is it needed?*I *love* Markdown*<p><em>I <em>love</em> Markdown</em></p>
*I *love* Markdown*<p><em>I </em>love<em> Markdown</em></p>
*I *love* Markdown*<p><em>I *love</em> Markdown*</p>
15%
33%
Source: http://johnmacfarlane.net/babelmark2/
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement
Adding league/commonmark
$ composer require league/commonmark:^0.13
<?php$converter = new CommonMarkConverter();echo $converter->convertToHtml('Hello **Nomad PHP!**');
Integrations
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement3. Easy to customize
Customization Example<https://www.nomadphp.com>
<a href="https://www.nomadphp.com">https://www.nomadphp.com
</a>
Conversion Process
<https://www.nomadphp.com>
Markdown Parse
<document> <paragraph> <link destination="https://www.nomadphp.com"> <text>https://www.nomadphp.com</text> </link> </paragraph></document>
Conversion Process
Markdown ASTParse RenderParse
Conversion Process
<a href="https://www.nomadphp.com">
https://www.nomadphp.com</a>
Markdown AST HTMLParse RenderParse
Conversion Process
Markdown AST HTMLParse RenderParse
Add your own custom parser, processor, or renderer
Customization Example<https://www.nomadphp.com>
<a href="https://www.nomadphp.com">https://www.nomadphp.com
</a>
<@nomadphp>
<a href="https://twitter.com/nomadphp">
@nomadphp</a>
Customizing league/commonmarkclass TwitterUsernameAutolinkParser extends AbstractInlineParser { public function getCharacters() { return ['<']; }
public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); if ($match = $cursor->match('/^<@[A-Za-z0-9_]+>/')) { // Remove the starting '<@' and ending '>' that were matched $username = substr($match, 2, -1);
$profileUrl = 'https://twitter.com/' . $username; $link = new Link($profileUrl, '@'.$username); $inlineContext->getContainer()->appendChild($link);
return true; }
return false; }}
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement3. Easy to customize
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement3. Easy to customize4. Full UTF-8 compatibility
league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
1. Supports the full CommonMark standard2. Easy to implement3. Easy to customize4. Full UTF-8 compatibility5. Well-tested
Installation & Documentation: http://github.com/thephpleague/commonmark
Learn More About CommonMark: http://commonmark.org
Slides / Feedback: http://joind.in/16748 @colinodell