title: Add target _blank to external link - Parsedown PHP
body: <p>I'm using <a href="http://parsedown.org/" rel="noreferrer">Parsedown</a> to parse HTML from the database to my site. With Parsedown, you can't really add <code>target="_blank"</code> to the links.</p>
  
  <p>So what I'm trying to do is to add <code>target="_blank"</code> to <strong>external</strong> links. I've found this function in Parsedown.php:</p>
  
  <pre><code>protected function inlineLink($Excerpt)
  {
      $Element = array(
          'name' =&gt; 'a',
          'handler' =&gt; 'line',
          'text' =&gt; null,
          'attributes' =&gt; array(
              'href' =&gt; null,
              'title' =&gt; null,
          ),
      );
  
      $extent = 0;
  
      $remainder = $Excerpt['text'];
  
      if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
      {
          $Element['text'] = $matches[1];
  
          $extent += strlen($matches[0]);
  
          $remainder = substr($remainder, $extent);
      }
      else
      {
          return;
      }
  
      if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches))
      {
          $Element['attributes']['href'] = $matches[1];
  
          if (isset($matches[2]))
          {
              $Element['attributes']['title'] = substr($matches[2], 1, - 1);
          }
  
          $extent += strlen($matches[0]);
      }
      else
      {
          if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
          {
              $definition = strlen($matches[1]) ? $matches[1] : $Element['text'];
              $definition = strtolower($definition);
  
              $extent += strlen($matches[0]);
          }
          else
          {
              $definition = strtolower($Element['text']);
          }
  
          if ( ! isset($this-&gt;DefinitionData['Reference'][$definition]))
          {
              return;
          }
  
          $Definition = $this-&gt;DefinitionData['Reference'][$definition];
  
          $Element['attributes']['href'] = $Definition['url'];
          $Element['attributes']['title'] = $Definition['title'];
      }
  
      $Element['attributes']['href'] = str_replace(array('&amp;', '&lt;'), array('&amp;amp;', '&amp;lt;'), $Element['attributes']['href']);
  
      return array(
          'extent' =&gt; $extent,
          'element' =&gt; $Element,
      );
  }
  </code></pre>
  
  <p>Now, what I've tried is this (added a comment of what I changed):</p>
  
  <pre><code>protected function inlineLink($Excerpt)
  {
      $Element = array(
          'name' =&gt; 'a',
          'handler' =&gt; 'line',
          'text' =&gt; null,
          'attributes' =&gt; array(
              'href' =&gt; null,
              'target' =&gt; null, // added this
              'title' =&gt; null,
          ),
      );
  
      $extent = 0;
  
      $remainder = $Excerpt['text'];
  
      if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
      {
          $Element['text'] = $matches[1];
  
          $extent += strlen($matches[0]);
  
          $remainder = substr($remainder, $extent);
      }
      else
      {
          return;
      }
  
      if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches))
      {
          $Element['attributes']['href'] = $matches[1];
  
          if (isset($matches[2]))
          {
              $Element['attributes']['title'] = substr($matches[2], 1, - 1);
          }
  
          $extent += strlen($matches[0]);
      }
      else
      {
          if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
          {
              $definition = strlen($matches[1]) ? $matches[1] : $Element['text'];
              $definition = strtolower($definition);
  
              $extent += strlen($matches[0]);
          }
          else
          {
              $definition = strtolower($Element['text']);
          }
  
          if ( ! isset($this-&gt;DefinitionData['Reference'][$definition]))
          {
              return;
          }
  
          $Definition = $this-&gt;DefinitionData['Reference'][$definition];
  
          $Element['attributes']['href'] = $Definition['url'];
          if (strpos($Definition['url'], 'example.com') !== false) { // added this aswell, checking if its our own URL
              $Element['attributes']['target'] = '_blank';
          }
          $Element['attributes']['title'] = $Definition['title'];
      }
  
      $Element['attributes']['href'] = str_replace(array('&amp;', '&lt;'), array('&amp;amp;', '&amp;lt;'), $Element['attributes']['href']);
  
      return array(
          'extent' =&gt; $extent,
          'element' =&gt; $Element,
      );
  }
  </code></pre>
  
  <p>Any suggestions to accomplish this?</p>