Skip to content

Repository files navigation

markdown-it-php

Build Statuscomposer versionlicencephp version

This gem is a port of the markdown-it Javascript package by Vitaly Puzrin and Alex Kocharin. Currently synced with markdown-it 13.0.1

Javascript Live demo

  • 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

Install

composer:

composer require kaoken/markdown-it-php

Simple

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

Single line rendering, without paragraph wrap:

$md = newMarkdownIt();
$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).

// commonmark mode$md = newMarkdownIt('commonmark');
// default mode$md = newMarkdownIt();
// enable everything$md = newMarkdownIt([
"html"=> true,
"linkify"=> true,
"typographer"=> true
]);
// full options list (defaults)$md = newMarkdownIt([
"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 = newMarkdownIt()
->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.

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

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

// Actual default values$md = newMarkdownIt([
"highlight"=> function ($str, $lang) {
if ( $lang ) {
try {
return'<pre class="hljs"><code>' .
highlight_string($str) .
'</code></pre>';
} catch (Exception$e) {}
}
return'<pre class="hljs"><code>' . $md->utils->escapeHtml($str) . '</code></pre>';
}
]);

Linkify

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

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

Syntax extensions

Embedded (enabled by default):

The following plugins are in the kaoken\markdown-it-php\MarkdownIt\Plugins directory:

Manage rules

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

// Activate/deactivate rules, with currying$md = (newMarkdownIt())
->disable([ 'link', 'image' ])
->enable([ 'link' ])
->enable('image');
// Enable everything$md = newMarkdownIt([
"html" => true,
"linkify" => true,
"typographer" => true,
]);

You can find all rules in sources: ParserCore, ParserBlock, ParserInline.

References / Thanks

Thanks to the authors of the original implementation in Javascript, markdown-it:

and to John MacFarlane for his work on the CommonMark spec and reference implementations.

Related Links:

License

MIT

About

php version markdown-it

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages