kaoken / laravel-markdown-it
用于在 Laravel 6.x 中使用 PHP 版本 markdown-it 的工具。
2.6.0
2020-02-22 06:07 UTC
Requires
- php: >=7.3.6
- kaoken/markdown-it-php: 10.*
- laravel/framework: 6.0.*|6.2.*|6.4.*|6.8.*|6.12.*
This package is auto-updated.
Last update: 2024-09-22 15:56:34 UTC
README
此工具针对 Laravel 6.0 及更高版本,使得 PHP 版本 markdown-it 能够方便地与 Laravel 结合使用。
目录
安装
composer:
将以下内容添加到 composer.json
中:
"require": { ... "kaoken/laravel-markdown-it":"^2.0" }
然后更新!
composer update
设置
将以下内容添加到 config\app.php
中:
'providers' => [ ... Kaoken\LaravelMarkdownIt\MarkdownItServiceProvider::class, ], 'aliases' => [ ... 'MarkdownIt' => Kaoken\LaravelMarkdownIt\Facade\MarkdownIt::class, ],
Artisan 命令
php artisan vendor:publish
执行上述命令后,markdownit.php
将在 config
目录中创建。
选项和规则配置文件
默认情况下启用所有选项和规则。这相当于以下 config \ markdownit.php
中的 hoge ['options_rules_group'] ['default']
。
有关选项("options "
)和规则("enable "
、"disable "
)的说明,请参阅 hoge ['options_rules_group'] ['default']
每个参数的注释。
如果您想添加新的选项和规则,请删除以下注释中 Add options and rules
下的注释,并重写 'example'
的内容。
<?php return [ 'set_options_rules' => 'default', 'options_rules_group' =>[ 'default' => [ 'options'=> [ 'html'=> true, // Enable HTML tags in source 'xhtmlOut'=> true, // Use '/' to close single tags (<br />) 'breaks'=> true, // Convert '\n' in paragraphs into <br> 'langPrefix'=> 'language-', // CSS language prefix for fenced blocks 'linkify'=> true, // autoconvert URL-like texts to links // Enable some language-neutral replacements + quotes beautification '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. // // function (/*str, lang*/) { return ''; } // 'highlight'=> null, 'maxNesting'=> 100 // Internal protection, recursion limit ], /** * Manage rules! * `'enable'` adds the rule you want to enable. * `'disable'` adds the rule you want to disable. * default all rules are enabled. * @see https://github.com/markdown-it/markdown-it/tree/master/benchmark/samples */ 'enable' => [ /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-autolink.md */ 'autolink', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-backticks.md */ 'backticks', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-bq-flat.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-bq-nested.md */ 'blockquote', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-code.md */ 'code', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-em-flat.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-em-nested.md */ 'emphasis', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-entity.md */ 'entity', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-escape.md */ 'escape', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-fences.md */ 'fence', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-heading.md */ 'heading', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-hr.md */ 'hr', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-html.md */ 'html_block', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-html.md */ 'html_inline', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-links-flat.md */ 'image', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-lheading.md */ 'lheading', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-links-flat.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-links-nested.md */ 'link', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-list-flat.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-list-nested.md */ 'list', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/inline-newlines.md */ 'newline', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-ref-flat.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-ref-list.md * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-ref-nested.md */ 'reference', /** * @see https://github.com/markdown-it/markdown-it/blob/master/benchmark/samples/block-tables.md */ 'table' ], 'disable' => [ ] ], /** * Add options and rules. */ /* 'example' => [ 'options'=> [ 'html'=> false, 'xhtmlOut'=> false, 'breaks'=> false, 'langPrefix'=> 'language-', 'linkify'=> false, 'typographer'=> false ], 'enable' => [ 'backticks', 'blockquote', 'emphasis', 'heading', 'list', 'newline', ], 'disable' => [ 'autolink', 'code', 'entity', 'escape', 'fence', 'hr', 'html_block', 'html_inline', 'image', 'lheading', 'link', 'reference', 'table' ] ] */ ] ];
Simpl
use MarkdownIt; class hoge{ public function test(){ // Already a group of `default` options and rules have been set. $result1 = MarkdownIt::render('# markdown-it rulezz!'); // `example`options and groups of rules are set. $result2 = MarkdownIt::setOptionsRules("example") ->render('# markdown-it rulezz!'); } }
单行渲染,不换行
use MarkdownIt; class hoge{ public function test(){ // Already a group of `default` options and rules have been set. $result1 = MarkdownIt::renderInline('__markdown-it__ rulezz!'); // `example`options and groups of rules are set. $result2 = MarkdownIt::setOptionsRules("example") ->renderInline('__markdown-it__ rulezz!'); } }
链接化
linkify: true
将 linkify-it 设置为访问链接化实例。
use MarkdownIt; class hoge{ public function test(){ // disables .py as top level domain MarkdownIt::linkify()->tlds('.py', false); } }
参考 / 感谢
感谢原始实现的 JavaScript 作者,markdown-it
- Alex Kocharin github/rlidwka
- Vitaly Puzrin github/puzrin
以及 John MacFarlane 对 CommonMark 规范和参考实现的工作。
相关链接
- https://github.com/jgm/CommonMark - C & JS 中的参考 CommonMark 实现,还包含最新的规范和在线演示。
- http://talk.commonmark.org - CommonMark 论坛,是开发者协作的好地方。