kaoken / markdown-it-php
PHP 版本的 markdown-it
14.1.0.0
2024-03-23 05:33 UTC
Requires
- php: >=7.4.0
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- ext-pthreads: *
- symfony/yaml: 5.0.*
This package is auto-updated.
Last update: 2024-09-23 07:00:22 UTC
README
这个包是将 Vitaly Puzrin 和 Alex Kocharin 的 markdown-it Javascript 包 转换为 PHP 的版本。目前与 markdown-it 14.1.0 同步
- 遵循 CommonMark 规范 并添加语法扩展和糖(URL 自动链接、排版)。
- 可配置的语法!您可以添加新规则甚至替换现有的规则。
- 默认安全。
目录
安装
composer:
composer require kaoken/markdown-it-php
简单
$md = new MarkdownIt(); $result = $md->render('# markdown-it rulezz!');
单行渲染,无段落换行
$md = new MarkdownIt(); $result = $md->renderInline('__markdown-it__ rulezz!');
使用预设和选项初始化
(*) 预设定义了活动规则和选项的组合。可以是 "commonmark"
、"zero"
或 "default"
(如果省略)。
// 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 ''; } ]);
插件加载
$md = new MarkdownIt() ->plugin(plugin1) ->plugin(plugin2, opts, ...) ->plugin(plugin3);
语法高亮
使用 highlight
选项将语法高亮应用到带围栏的代码块
这里的示例仅是 PHP 语言的语法高亮。
// 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 } ]);
或者使用完整的包装覆盖(如果您需要将类分配给 <pre>
)
// Actual default values $md = new MarkdownIt([ "highlight"=> function ($str, $lang) { if ( $lang ) { try { return '<pre><code class="hljs">' . highlight_string($str) . '</code></pre>'; } catch (Exception $e) {} } return '<pre><code class="hljs">' . $md->utils->escapeHtml($str) . '</code></pre>'; } ]);
链接化
linkify: true
使用 linkify-it。要配置 linkify-it,请通过 $md->linkify
访问链接化实例
$md->linkify->set(['fuzzyEmail'=>false]); // disables .py as top level domain
语法扩展
嵌入式(默认启用)
以下插件位于 kaoken\markdown-it-php\MarkdownIt\Plugins 目录中
- 下标
\MarkdownItSub
- 上标
\MarkdownItSup
- 脚注
\MarkdownItFootnote
- 定义列表
\MarkdownItDeflist
- 缩写
\MarkdownItAbbr
- 表情符号
\MarkdownItEmoji
- 自定义容器
\MarkdownItContainer
- 插入
\MarkdownItIns
- 标记
\MarkdownItMark
管理规则
默认情况下启用所有规则,但可以通过选项进行限制。在插件加载时自动启用其所有规则。
// 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, ]);
您可以在以下位置找到所有规则: ParserCore、ParserBlock、ParserInline。
参考/感谢
感谢原始实现作者在 JavaScript 中的工作,markdown-it
- Alex Kocharin github/rlidwka
- 维塔利·普兹林 github/puzrin
以及感谢 约翰·麦卡弗莱恩 对 CommonMark 规范和参考实现的工作。
相关链接
- https://github.com/jgm/CommonMark - C 和 JS 中的参考 CommonMark 实现,也包含最新的规范和在线演示。
- http://talk.commonmark.org - CommonMark 论坛,是开发者协作的良好平台。