erusev / parsedown

Better Markdown Parser in PHP

Home Page:https://parsedown.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsedown extension to open in new window

lipupini opened this issue · comments

I'm not sure if this is posted somewhere already:

<?php

class ParsedownWithTargetBlank extends \Parsedown {
	private function addTargetBlank(string $method, array $Excerpt): array|null {
		$return = parent::$method($Excerpt);
		if (!$return) {
			return null;
		}
		$return['element']['attributes']['target'] = '_blank';
		$return['element']['attributes']['rel'] = 'noopener noreferrer';
		return $return;
	}

	protected function inlineUrl($Excerpt) {
		return $this->addTargetBlank('inlineUrl', $Excerpt);
	}

	protected function inlineLink($Excerpt) {
		return $this->addTargetBlank('inlineLink', $Excerpt);
	}

	protected function inlineUrlTag($Excerpt) {
		return $this->addTargetBlank('inlineUrlTag', $Excerpt);
	}
}

Thank you! works like a charm!