Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
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.

49 lines
1.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Helpers;
  3. use Parsedown;
  4. /**
  5. * Helper to extend Parsedown
  6. *
  7. * @author Björn Hase, Tentakelfabrik
  8. * @license http://opensource.org/licenses/MIT The MIT License
  9. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
  10. *
  11. */
  12. class MarkdownHelper extends Parsedown
  13. {
  14. /**
  15. *
  16. * @var string
  17. */
  18. const EXTERNAL_LINK = "/^(http|https):\/\//";
  19. const INNER_BRACKETS = "/\){(.*?)\}/";
  20. const TARGET_BLANK = "_blank";
  21. const DIVIDER_METHOD = ':';
  22. const DIVIDER_SIZES = 'x';
  23. /**
  24. * extend default function, if a link has http|https in url,
  25. * then handle this link as external and set target to _blank
  26. *
  27. * @param array $excerpt
  28. * @return array
  29. */
  30. protected function inlineLink($excerpt)
  31. {
  32. $result = parent::inlineLink($excerpt);
  33. if (is_array($result)) {
  34. if (isset($result['element']['attributes'])) {
  35. if (preg_match(self::EXTERNAL_LINK, $result['element']['attributes']['href'])) {
  36. $result['element']['attributes']['target'] = self::TARGET_BLANK;
  37. }
  38. }
  39. return $result;
  40. }
  41. }
  42. }