tovic/parsedown-extra-plugin

可配置的 Markdown 到 HTML 转换器,使用 Parsedown Extra。

v1.3.11 2023-09-03 15:08 UTC

README

可配置的 Markdown 到 HTML 转换器,使用 Parsedown Extra。

更新 2023/09/03: 由于 Parsedown 项目至今缺乏活动,我将退役此项目。实际上有一个 Parsedown 版本 2.0 的草案,但似乎没有进展。我将在 Parsedown 版本 2.0 发布时考虑重构,但我可能不再积极维护此项目,因为我现在将重点转向 我自己的 Markdown Extra 解析器。它由一个文件组成,使用与 Parsedown 相同的方法来分隔块和内联元素,因此应该达到相似(甚至更好的)速度和效率。

Parsedown Logo

使用方法

手动

Parsedown.phpParsedownExtra.php 文件之后包含 ParsedownExtraPlugin.php

require 'Parsedown.php';
require 'ParsedownExtra.php';
require 'ParsedownExtraPlugin.php';

# Create
$Parsedown = new ParsedownExtraPlugin;

# Configure
$Parsedown->voidElementSuffix = '>'; // HTML5

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

Composer

从文件管理器界面,在项目文件夹中创建一个 composer.json 文件,然后添加以下内容

{
  "minimum-stability": "dev"
}

从命令行界面,导航到项目文件夹然后运行此命令

composer require taufik-nurrohman/parsedown-extra-plugin

从文件管理器界面,在项目文件夹中创建一个 index.php 文件然后要求自动加载文件

require 'vendor/autoload.php';

# Create
$Parsedown = new ParsedownExtraPlugin;

# Configure
$Parsedown->voidElementSuffix = '>'; // HTML5

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

功能

HTML 或 XHTML

$Parsedown->voidElementSuffix = '>'; // HTML5

预定义缩写

$Parsedown->abbreviationData = [
    'CSS' => 'Cascading Style Sheet',
    'HTML' => 'Hyper Text Markup Language',
    'JS' => 'JavaScript'
];

预定义参考链接和图像

$Parsedown->referenceData = [
    'mecha-cms' => [
        'url' => 'https://mecha-cms.com',
        'title' => 'Mecha CMS'
    ],
    'test-image' => [
        'url' => 'http://example.com/favicon.ico',
        'title' => 'Test Image'
    ]
);

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

$Parsedown->linkAttributes = function ($Text, $Attributes, &$Element, $Internal) {
    if (!$Internal) {
        return [
            'rel' => 'nofollow',
            'target' => '_blank';
        ];
    }
    return [];
};

自动添加 id 属性到标题

$Parsedown->headerAttributes = function ($Text, $Attributes, &$Element, $Level) {
    $Id = $Attributes['id'] ?? trim(preg_replace('/[^a-z\d\x{4e00}-\x{9fa5}]+/u', '-', strtolower($Text)), '-');
    return ['id' => $Id];
};

自动添加图像元素

段落中单独出现的每个图像标记都将自动转换为图像元素。

$Parsedown->figuresEnabled = true;
$Parsedown->figureAttributes = ['class' => 'image'];

$Parsedown->imageAttributesOnParent = ['class', 'id'];

要添加图像下的标题,请在段落之前添加至少一个空格但少于四个空格,将图像之后的段落序列转换为图像标题。

This is a paragraph.

![Image](/path/to/image.jpg)
 Image caption.

This is a paragraph.

![Image](/path/to/image.jpg)

 Image caption in a paragraph tag.

This is a paragraph.

![Image](/path/to/image.jpg)

    This is a code block.

This is a paragraph.

请注意,此格式也适用于常规 Markdown 文件。因此,当其他 Markdown 转换器解析时,它将优雅地降级。

自定义代码块类格式

$Parsedown->blockCodeClassFormat = 'language-%s';

自定义代码块内容

$Parsedown->codeHtml = '<span class="my-code">%s</span>';
$Parsedown->blockCodeHtml = '<span class="my-code-block">%s</span>';
// <https://github.com/scrivo/highlight.php>
function doApplyHighlighter(string $Text, array $ClassList, &$Element) {
    $Highlight = new \Highlight\Highlighter;
    $Highlight->setAutodetectLanguages($ClassList);
    $Highlighted = $Highlight->highlightAuto($Text);
    $Element['attributes']['class'] = 'hljs ' . $Highlighted->language;
    return $Highlighted->value;
}

$Parsedown->codeHtml = function ($Text, $Attributes, &$Element) {
    return doApplyHighlighter($Text, [], $Element);
};

$Parsedown->blockCodeHtml = function ($Text, $Attributes, &$Element) {
    $ClassList = array_filter(explode(' ', $Attributes['class'] ?? ""));
    return doApplyHighlighter($Text, $ClassList, $Element);
};

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

$Parsedown->codeAttributesOnParent = true;

自定义引用块类

$Parsedown->blockQuoteAttributes = ['class' => 'quote'];
$Parsedown->blockQuoteAttributes = function ($Text, $Attributes, &$Element) {
    if (strpos($Text, '**Danger:** ') === 0) {
        return ['class' => 'alert alert-danger'];
    }
    if (strpos($Text, '**Info:** ') === 0) {
        return ['class' => 'alert alert-info'];
    }
    return [];
};

自定义表格属性

$Parsedown->tableAttributes = ['border' => 1];

自定义表格对齐类

$Parsedown->tableColumnAttributes = function ($Text, $Attributes, &$Element, $Align) {
    return [
        'class' => $Align ? 'text-' . $Align : null,
        'style' => null // Remove inline styles
    ];
};

自定义脚注 ID 格式

$Parsedown->footnoteLinkAttributes = function ($Number, $Attributes, &$Element, $Name) {
    return ['href' => '#to:' . $Name];
};

$Parsedown->footnoteReferenceAttributes = function ($Number, $Attributes, &$Element, $Name, $Index) {
    return ['id' => 'from:' . $Name . '.' . $Index];
};

$Parsedown->footnoteBackLinkAttributes = function ($Number, $Attributes, &$Element, $Name, $Index) {
    return ['href' => '#from:' . $Name . '.' . $Index];
};

$Parsedown->footnoteBackReferenceAttributes = function ($Number, $Attributes, &$Element, $Name, $Total) {
    return ['id' => 'to:' . $Name];
};

自定义脚注类

$Parsedown->footnoteAttributes = ['class' => 'notes'];

自定义脚注链接文本

$Parsedown->footnoteLinkHtml = '[%s]';

自定义脚注回链文本

$Parsedown->footnoteBackLinkHtml = '<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 class="php" id="foo">

属性别名作为方法

属性别名可以作为方法使用,仅为了遵循 Parsedown 设置其配置数据的方式。它使用 PHP 的 __call() 方法自动生成类方法

// This is …
$Parsedown->setBlockCodeHtml(function () { … });

// … equal to this
$Parsedown->blockCodeHtml = function () { … };

支持

我在寻找对试用我的内容管理系统感兴趣的网络机构和网页设计师,Mecha。Mecha 是一个极简主义的内容管理系统,用于构建从网络公司简介到个人品牌网页日志的简单网站。它是一个基于文件的内容管理系统,不会占用太多网络托管空间,因此

  1. 从客户的角度来看,这可以减少他们的月度托管费用。
  2. 从网页设计师的角度来看,这将增加您的公司利润。

我更喜欢那些不太懂编程语言但了解创建网页主题基础的设计师。这样,我的 JavaScript 和 PHP 技能可以帮助您解决与您网络项目后端功能相关的问题。然后,当您在客户的项中使用面板功能时,我可以得到奖励。