title: Making external links open in a new window in wagtail
body: <p>I recently implemented adding <code>target="_blank"</code> to external links like this:</p>
  
  <pre><code>@hooks.register('after_edit_page')
  def do_after_page_edit(request, page):
      if hasattr(page, "body"):
          soup = BeautifulSoup(page.body)
          for a in soup.findAll('a'):
              if hasattr(a, "href"):
              a["target"] = "_blank"
          page.body = str(soup)
          page.body = page.body.replace("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;", "")
          page.body = page.body.replace("&lt;/body&gt;&lt;/html&gt;", "")
          page.body = page.body.replace("&gt;&lt;/embed&gt;", "/&gt;")
          page.save()
  
  @hooks.register('construct_whitelister_element_rules')
  def whitelister_element_rules():
      return {
          'a': attribute_rule({'href': check_url, 'target': True}),
      }
  </code></pre>
  
  <p>Problems:</p>
  
  <ol>
  <li><p>Beautiful soup messes with the output, adding <code>html, head &amp; body</code> tags - <a href="https://stackoverflow.com/questions/14822188/dont-put-html-head-and-body-tags-automatically-beautifulsoup">Don&#39;t put html, head and body tags automatically, beautifulsoup</a></p></li>
  <li><p>It also messes with the embed tags - <a href="https://stackoverflow.com/questions/14961497/how-to-get-beautifulsoup-4-to-respect-a-self-closing-tag">How to get BeautifulSoup 4 to respect a self-closing tag?</a></p></li>
  <li><p>Hence my crappy "<code>fix</code>" manually replacing parts of the output with blank strings.</p></li>
  </ol>
  
  <p>Question:</p>
  
  <p>What is the correct and best way to do this?</p>