introduction to league/commonmark

24
Introduction to league/commonm ark Colin O’Dell

Upload: colin-odell

Post on 15-Apr-2017

279 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Introduction to league/commonmark

Introduction to league/commo

nmarkColin O’Dell

Page 2: Introduction to league/commonmark

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

Page 3: Introduction to league/commonmark

league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.

Page 4: Introduction to league/commonmark

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

Page 5: Introduction to league/commonmark

Why is it needed?*I love Markdown*<p><em>I love Markdown</em></p>

Page 6: Introduction to league/commonmark

Why is it needed?*I *love* Markdown*

Source: http://johnmacfarlane.net/babelmark2/

Page 7: Introduction to league/commonmark

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/

Page 8: Introduction to league/commonmark

league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.

1. Supports the full CommonMark standard

Page 9: Introduction to league/commonmark

league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.

1. Supports the full CommonMark standard2. Easy to implement

Page 10: Introduction to league/commonmark

Adding league/commonmark

$ composer require league/commonmark:^0.13

<?php$converter = new CommonMarkConverter();echo $converter->convertToHtml('Hello **Nomad PHP!**');

Page 11: Introduction to league/commonmark

Integrations

Page 12: Introduction to league/commonmark

league/commonmarkA well-written, super-configurable Markdown parser for PHP based on the CommonMark spec.

1. Supports the full CommonMark standard2. Easy to implement

Page 13: Introduction to league/commonmark

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

Page 14: Introduction to league/commonmark

Customization Example<https://www.nomadphp.com>

<a href="https://www.nomadphp.com">https://www.nomadphp.com

</a>

Page 15: Introduction to league/commonmark

Conversion Process

<https://www.nomadphp.com>

Markdown Parse

Page 16: Introduction to league/commonmark

<document> <paragraph> <link destination="https://www.nomadphp.com"> <text>https://www.nomadphp.com</text> </link> </paragraph></document>

Conversion Process

Markdown ASTParse RenderParse

Page 17: Introduction to league/commonmark

Conversion Process

<a href="https://www.nomadphp.com">

https://www.nomadphp.com</a>

Markdown AST HTMLParse RenderParse

Page 18: Introduction to league/commonmark

Conversion Process

Markdown AST HTMLParse RenderParse

Add your own custom parser, processor, or renderer

Page 19: Introduction to league/commonmark

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>

Page 20: Introduction to league/commonmark

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; }}

Page 21: Introduction to league/commonmark

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

Page 22: Introduction to league/commonmark

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

Page 23: Introduction to league/commonmark

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

Page 24: Introduction to league/commonmark

Installation & Documentation: http://github.com/thephpleague/commonmark

Learn More About CommonMark: http://commonmark.org

Slides / Feedback: http://joind.in/16748 @colinodell