让 shortlink 插件支持 Markdown 格式
要让 shortlink
插件支持将文章里 []()
(Markdown 格式链接)和 ![]()
(Markdown 格式图片)中的外链转换为内链,需要对插件代码进行修改,主要涉及到正则匹配和链接转换的逻辑调整。以下是具体的修改步骤和代码示例:
1. 修改 Plugin.php
文件
在 Plugin.php
文件中,需要修改 replace
方法和 autoLink
方法,以支持 Markdown 格式的链接和图片。
// ... 原有的代码 ...
/**
* 外链转内链
*
* @access public
* @param string $text
* @param mixed $widget
* @param mixed $lastResult
* @return array|string|string[] $content
* @throws \Typecho\Plugin\Exception
*/
public static function replace(string $text, $widget, $lastResult)
{
$text = empty($lastResult) ? $text : $lastResult;
$pluginOption = self::options('ShortLinks'); // 插件选项
$target = ($pluginOption->target) ? ' target="_blank" ' : ''; // 新窗口打开
if ($pluginOption->convert == 1) {
if ($widget->fields) {
$fields = unserialize($widget->fields);
if (is_array($fields) && array_key_exists("noshort", $fields)) {
// 部分文章不转换
return $text;
}
}
// 文章内容和评论内容处理
// 处理 <a> 标签
@preg_match_all('/<a(.*?)href="(?!#)(.*?)"(.*?)>/', $text, $matches);
if ($matches) {
foreach ($matches[2] as $link) {
$text = str_replace("href=\"$link\"", "href=\"" . self::convertLink($link) . "\"" . $target, $text);
}
}
// 处理 Markdown 格式链接 []()
@preg_match_all('/\[([^\]]+)\]\((?!#)(.*?)\)/', $text, $markdownMatches);
if ($markdownMatches) {
for ($i = 0; $i < count($markdownMatches[0]); $i++) {
$linkText = $markdownMatches[1][$i];
$linkUrl = $markdownMatches[2][$i];
$convertedLink = self::convertLink($linkUrl);
$text = str_replace($markdownMatches[0][$i], "[$linkText]($convertedLink)", $text);
}
}
// 处理 Markdown 格式图片 ![]()
@preg_match_all('/!\[([^\]]+)\]\((?!#)(.*?)\)/', $text, $markdownImageMatches);
if ($markdownImageMatches) {
for ($i = 0; $i < count($markdownImageMatches[0]); $i++) {
$imageAlt = $markdownImageMatches[1][$i];
$imageUrl = $markdownImageMatches[2][$i];
// 图片不处理
if (!preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $imageUrl)) {
$convertedLink = self::convertLink($imageUrl);
$text = str_replace($markdownImageMatches[0][$i], "", $text);
}
}
}
}
return $text;
}
// ... 原有的代码 ...
/**
* 文本链接转A标签
*
* @param string $content
* @return string
* @throws \Typecho\Plugin\Exception
*/
public static function autoLink($content)
{
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$target = (self::options()->target) ? ' target="_blank" ' : ''; // 新窗口打开
return preg_replace_callback($url, function ($matches) use ($target) {
if (preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i', $matches[0])) {
return $matches[0];
}
if (strpos($matches[0], '://') !== false && strpos($matches[0], rtrim(self::options()->siteUrl, '/')) !== false) {
return '<a href="' . self::convertLink($matches[0]) . '" title="' . $matches[0] . '"' . $target . '>' . $matches[0] . '</a>';
}
return $matches[0];
}, $content);
}
// ... 原有的代码 ...
2. 代码解释
- **`replace` 方法**:
- 新增了对 Markdown 格式链接 `[]()` 和图片 `![]()` 的处理逻辑。
- 使用正则表达式 `\[([^\]]+)\]\((?!#)(.*?)\)` 匹配 Markdown 格式链接,将链接部分替换为转换后的内链。
- 使用正则表达式 `!\[([^\]]+)\]\((?!#)(.*?)\)` 匹配 Markdown 格式图片,对于非图片链接进行转换。
- **`autoLink` 方法**:保持不变,主要处理普通文本中的链接。
3. 注意事项
- 修改代码前,请备份原有的插件文件,以防出现问题。
- 由于正则表达式的局限性,可能无法处理所有复杂的 Markdown 格式链接和图片,需要根据实际情况进行调整。
通过以上修改,插件应该可以支持将文章里 []()
和 ![]()
中的外链转换为内链。
评论区(暂无评论)