youyiio/beyong-markdown

PHP markdown 解析,基于 parsedown。

v1.0.3 2023-05-05 07:10 UTC

This package is auto-updated.

Last update: 2024-09-05 10:02:48 UTC


README

带有 Parsedown Extra 的可配置 Markdown 到 HTML 转换器。

目录

安装

composer require youyiio/beyong-markdown:^1.0

用法

use beyong\markdown\ParsedownExtraPlugin;
use beyong\markdown\ParsedownExtra;

$parser = new ParsedownExtraPlugin();

// settings ...
$parser->code_class = 'lang-%s';

echo $parser->text('# Header {.sth}');

功能

HTML 或 XHTML

$parser->element_suffix = '>'; // HTML5

预定义缩写

$parser->abbreviations = array(
    'CSS' => 'Cascading Style Sheet',
    'HTML' => 'Hyper Text Markup Language',
    'JS' => 'JavaScript'
);

预定义链接

$parser->links = array(
    'mecha-cms' => array(
        'url' => 'http://mecha-cms.com',
        'title' => 'Mecha CMS'
    ),
    'test-image' => array(
        'url' => 'http://example.com/favicon.ico',
        'title' => 'Test Image'
    )
);

外部链接自动添加 rel="nofollow" 属性

// custom link attributes
$parser->links_attr = array();

// custom external link attributes
$parser->links_external_attr = array(
    'rel' => 'nofollow',
    'target' => '_blank'
);

// custom image attributes
$parser->images_attr = array(
    'alt' => ""
);

// custom external image attributes
$parser->images_external_attr = array();

自定义代码类格式

$parser->code_class = 'language-%s';
$parser->code_class = function($text) {
    return trim(str_replace('.', ' ', $text));
};

自定义代码文本格式

$parser->code_text = '<span class="my-code">%s</span>';
$parser->code_block_text = '<span class="my-code-block">%s</span>';
$parser->code_text = function($text) {
    return do_syntax_highlighter($text);
};

$parser->code_block_text = function($text) {
    return do_syntax_highlighter($text);
};

<pre> 元素上添加 <code> 属性

$parser->code_block_attr_on_parent = true;

自定义表格类

$parser->table_class = 'table-bordered';

自定义表格对齐类

$parser->table_align_class = 'text-%s';

自定义脚注 ID 格式

$parser->footnote_link_id = 'cite_note:%s';

自定义脚注回跳 ID 格式

$parser->footnote_back_link_id = 'cite_ref:%s-%s';

自定义脚注类

$parser->footnote_class = 'footnotes';

自定义脚注链接类

$parser->footnote_link_class = 'footnote-ref';

自定义脚注回跳链接类

$parser->footnote_back_link_class = 'footnote-backref';

自定义脚注链接文本

$parser->footnote_link_text = '[%s]';
$parser->footnote_link_text = function($text) {
    return '[' . $text . ']';
};

自定义脚注回跳链接文本

$parser->footnote_back_link_text = '<i class="icon icon-back"></i>';

高级属性解析器

  • {#foo}<tag id="foo">
  • {#foo#bar}<tag id="bar">
  • {.foo}<tag class="foo">
  • {.foo.bar}<tag class="foo bar">
  • {#foo.bar.baz}<tag id="foo" class="bar baz">
  • {#foo .bar .baz}<tag id="foo" class="bar baz"> (在 #. 前面的空白现在变为可选,在我的扩展中)
  • {foo="bar"}<tag foo="bar">
  • {foo="bar baz"}<tag foo="bar baz">
  • {foo='bar'}<tag foo="bar">
  • {foo='bar baz'}<tag foo="bar baz">
  • {foo=bar}<tag foo="bar">
  • {foo=}<tag foo="">
  • {foo}<tag foo="foo">
  • {foo=bar baz}<tag foo="bar" baz="baz">
  • {#a#b.c.d e="f" g="h i" j='k' l='m n' o=p q= r s t="u#v.w.x y=z"}<tag id="b" class="c d" e="f" g="h i" j="k" l="m n" o="p" q="" r="r" s="s" t="u#v.w.x y=z">

不带 language- 前缀的代码块类

类名中的点前缀现在是可选的,自定义属性语法也接受

  • php<pre><code class="language-php">
  • php html<pre><code class="language-php language-html">
  • .php<pre><code class="php">
  • .php.html<pre><code class="php html">
  • .php html<pre><code class="php language-html">
  • {.php #foo}<pre><code id="foo" class="php">