Typecho实现Markdown插入卡片
使用Parsedown
替换Typecho
自带的Markdown
解析库,所以在Parsedown
下扩展语法,首先在functions.php
加载Parsedown
class theme_plugin{
public static function markdown($text){
require_once 'Parsedown.php'; // Parsedown.php文件路径
require_once 'ParsedownExtra.php'; // ParsedownExtra.php文件路径
require_once 'ParsedownExtraPlugin.php'; // ParsedownExtraPlugin.php文件路径
$parsedown = new ParsedownExtraPlugin();
return $parsedown->setBreaksEnabled(true)->text($text);
}
}
在ParsedownExtraPlugin.php
中新增以下代码
<?php
class ParsedownExtraPlugin extends ParsedownExtra {
public function __construct()
{
parent::__construct();
$this->InlineTypes['{'][] = 'Card';
$this->inlineMarkerList .= '{';
}
protected function inlineCard($Excerpt)
{
if (preg_match('/^{card\((.*),(.*),(.*)\)}$/', $Excerpt['text'], $matches)) {
$Element = array(
'name' => 'a',
'attributes' => array('href' => trim($matches[3]),'title' => trim($matches[1]),'class' => 'post-card'),
'handler' => 'elements',
'text' => array(
array(
'name' => 'b',
'handler' => 'line',
'text' => trim($matches[1]),
),
array(
'name' => 'span',
'handler' => 'elements',
'text' => array(
array(
'name' => 'i',
'handler' => 'line',
'attributes' => array('class' => 'ci-link'),
'text' => '',
),
array(
'name' => 'i',
'handler' => 'line',
'text' => trim($matches[2]),
),
),
),
),
);
return array(
'extent' => strlen($matches[0]),
'element' => $Element,
);
}
}
}
{card(Aciuz, aciuz.com, https://www.aciuz.com)}
最新评论