2017 © Pedro Peláez
 

library markdown-it-php

PHP version makdown-it

image

kaoken/markdown-it-php

PHP version makdown-it

  • Monday, July 23, 2018
  • by kaoken
  • Repository
  • 3 Watchers
  • 9 Stars
  • 785 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 8 Versions
  • 22 % Grown

The README.md

markdown-it-php

Build Status composer version licence php version, (*1)

This gem is a port of the markdown-it Javascript package by Vitaly Puzrin and Alex Kocharin. Currently synced with markdown-it 14.1.0, (*2)

Javascript Live demo, (*3)

  • Follows the CommonMark spec + adds syntax extensions & sugar (URL autolinking, typographer).
  • Configurable syntax! You can add new rules and even replace existing ones.
  • Safe by default.

Table of content, (*4)

Install

composer:, (*5)

composer require kaoken/markdown-it-php

Simple

$md = new MarkdownIt();
$result = $md->render('# markdown-it rulezz!');

Single line rendering, without paragraph wrap:, (*6)

$md = new MarkdownIt();
$result = $md->renderInline('__markdown-it__ rulezz!');

Init with presets and options

(*) presets define combinations of active rules and options. Can be "commonmark", "zero" or "default" (if skipped)., (*7)

// commonmark mode
$md = new MarkdownIt('commonmark');

// default mode
$md = new MarkdownIt();

// enable everything
$md = new MarkdownIt([
  "html"=>        true,
  "linkify"=>     true,
  "typographer"=> true
]);

// full options list (defaults)
$md = new MarkdownIt([
  "html"=>         false,        // Enable HTML tags in source
  "xhtmlOut"=>     false,        // Use '/' to close single tags (<br />).
                                 // This is only for full CommonMark compatibility.
  "breaks"=>       false,        // Convert '\n' in paragraphs into <br>
  "langPrefix"=>   'language-',  // CSS language prefix for fenced blocks. Can be
                                 // useful for external highlighters.
  "linkify"=>      false,        // Autoconvert URL-like text to links

  // Enable some language-neutral replacement + quotes beautification
  // For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js
  "typographer"=>  false,

  // Double + single quotes replacement pairs, when typographer enabled,
  // and smartquotes on. Could be either a String or an Array.
  //
  // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  "quotes"=> '“”‘’',

  // Highlighter function. Should return escaped HTML,
  // or '' if the source string is not changed and should be escaped externaly.
  // If $result starts with 

<

pre... internal wrapper is skipped.
  "highlight"=> function (/*str, lang*/) { return ''; }
]);

Plugins load

$md = new MarkdownIt()
            ->plugin(plugin1)
            ->plugin(plugin2, opts, ...)
            ->plugin(plugin3);

Syntax highlighting

Apply syntax highlighting to fenced code blocks with the highlight option:
The sample here is only the highlight of the PHP language., (*8)


// Actual default values $md = new MarkdownIt([ "highlight"=> function ($str, $lang) { if ( $lang ) { try { return highlight_string($str); } catch (Exception $e) {} } return ''; // use external default escaping } ]);

Or with full wrapper override (if you need assign class to <pre>):, (*9)

// Actual default values
$md = new MarkdownIt([
  "highlight"=> function ($str, $lang) {
    if ( $lang ) {
      try {
        return '

' .
               highlight_string($str) .
               '
'; } catch (Exception $e) {} } return '<pre><code class="hljs">' . $md->utils->escapeHtml($str) . '</code></pre>'; } ]);

Linkify

linkify: true uses linkify-it. To configure linkify-it, access the linkify instance through $md->linkify:, (*10)

$md->linkify->set(['fuzzyEmail'=>false]);  // disables .py as top level domain

Syntax extensions

Embedded (enabled by default):, (*11)

The following plugins are in the kaoken\markdown-it-php\MarkdownIt\Plugins directory:, (*12)

Manage rules

By default all rules are enabled, but can be restricted by options. On plugin load all its rules are enabled automatically., (*13)

// Activate/deactivate rules, with currying
$md = (new MarkdownIt())
            ->disable([ 'link', 'image' ])
            ->enable([ 'link' ])
            ->enable('image');

// Enable everything
$md = new MarkdownIt([
  "html"        => true,
  "linkify"     => true,
  "typographer" => true,
]);

You can find all rules in sources: ParserCore, ParserBlock, ParserInline., (*14)

References / Thanks

Thanks to the authors of the original implementation in Javascript, markdown-it:, (*15)

and to John MacFarlane for his work on the CommonMark spec and reference implementations., (*16)

Related Links:, (*17)

  • https://github.com/jgm/CommonMark - reference CommonMark implementations in C & JS, also contains latest spec & online demo.
  • http://talk.commonmark.org - CommonMark forum, good place to collaborate developers' efforts.

License

MIT, (*18)

The Versions

23/07 2018

dev-master

9999999-dev

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

23/07 2018

8.4.200

8.4.200.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

17/02 2018

8.4.100

8.4.100.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

25/08 2017

8.4.000

8.4.000.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

04/08 2017

8.3.201

8.3.201.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

07/03 2017

8.3.101

8.3.101.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

02/03 2017

0.0.3

0.0.3.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it

23/12 2016

0.0.1

0.0.1.0

PHP version makdown-it

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

markdown markdown-it