Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction to
league/commonmark
Colin O’Dell
Colin O’Dell
Lead Web Developer at Unleashed Technologies
PHP developer since 2002
Author of:
• league/commonmark
• league...
league/commonmark
A 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, StackOverfl...
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<...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
Adding league/commonmark
$ composer require league/commonmark:^0.13
<?php
$converter = new CommonMarkConverter();
echo $co...
Integrations
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
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>
</parag...
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>
<@nomad...
Customizing league/commonmark
class TwitterUsernameAutolinkParser extends AbstractInlineParser {
public function getCharac...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
league/commonmark
A well-written, super-configurable Markdown
parser for PHP based on the CommonMark spec.
1. Supports the...
Installation & Documentation:
http://github.com/thephpleague/commonmark
Learn More About CommonMark:
http://commonmark.org...
Upcoming SlideShare
Loading in …5
×

Introduction to league/commonmark

615 views

Published on

league/commonmark is a well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. In this lightning talk, we'll introduce the CommonMark spec, discuss why it's important, and demonstrate how the league/commonmark project can be used and extended for your own PHP projects

Published in: Software
  • Be the first to comment

  • Be the first to like this

Introduction to league/commonmark

  1. 1. Introduction to league/commonmark Colin O’Dell
  2. 2. Colin O’Dell Lead Web Developer at Unleashed Technologies PHP developer since 2002 Author of: • league/commonmark • league/html-to-markdown • PHP 7 Migration Guide e-book @colinodell www.colinodell.com
  3. 3. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.
  4. 4. 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
  5. 5. Why is it needed? *I love Markdown* <p><em>I love Markdown</em></p>
  6. 6. Why is it needed? *I *love* Markdown* Source: http://johnmacfarlane.net/babelmark2/
  7. 7. 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/
  8. 8. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard
  9. 9. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement
  10. 10. Adding league/commonmark $ composer require league/commonmark:^0.13 <?php $converter = new CommonMarkConverter(); echo $converter->convertToHtml('Hello **Nomad PHP!**');
  11. 11. Integrations
  12. 12. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement
  13. 13. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement 3. Easy to customize
  14. 14. Customization Example <https://www.nomadphp.com> <a href="https://www.nomadphp.com"> https://www.nomadphp.com </a>
  15. 15. Conversion Process <https://www.nomadphp.com> Markdown Parse
  16. 16. <document> <paragraph> <link destination="https://www.nomadphp.com"> <text>https://www.nomadphp.com</text> </link> </paragraph> </document> Conversion Process Markdown ASTParse RenderParse
  17. 17. Conversion Process <a href="https://www.nomadphp.com"> https://www.nomadphp.com </a> Markdown AST HTMLParse RenderParse
  18. 18. Conversion Process Markdown AST HTMLParse RenderParse Add your own custom parser, processor, or renderer
  19. 19. 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>
  20. 20. Customizing league/commonmark class 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; } }
  21. 21. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement 3. Easy to customize
  22. 22. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement 3. Easy to customize 4. Full UTF-8 compatibility
  23. 23. league/commonmark A well-written, super-configurable Markdown parser for PHP based on the CommonMark spec. 1. Supports the full CommonMark standard 2. Easy to implement 3. Easy to customize 4. Full UTF-8 compatibility 5. Well-tested
  24. 24. Installation & Documentation: http://github.com/thephpleague/commonmark Learn More About CommonMark: http://commonmark.org Slides / Feedback: http://joind.in/16748 @colinodell

×