You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
super-gear-directus/src/Helpers/MarkdownHelper.php

48 lines
1.2 KiB

<?php
namespace SuperGear\Directus\Helpers;
use Parsedown;
/**
* Helper to extend Parsedown
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/directus GitHub Repository
*/
class MarkdownHelper extends Parsedown
{
/**
*
* @var string
*/
const EXTERNAL_LINK = "/^(http|https):\/\//";
const INNER_BRACKETS = "/\){(.*?)\}/";
const TARGET_BLANK = "_blank";
const DIVIDER_METHOD = ':';
const DIVIDER_SIZES = 'x';
/**
* extend default function, if a link has http|https in url,
* then handle this link as external and set target to _blank
*
* @param array $excerpt
* @return array
*/
protected function inlineLink($excerpt)
{
$result = parent::inlineLink($excerpt);
if (is_array($result)) {
if (isset($result['element']['attributes'])) {
if (preg_match(self::EXTERNAL_LINK, $result['element']['attributes']['href'])) {
$result['element']['attributes']['target'] = self::TARGET_BLANK;
}
}
return $result;
}
}
}