ghost

Open external links in a new window - Ghost

I've come to love [Ghost](ghost.org" target="_blank) even with its minor limitations. However, the platform is growing fast and can only get better, so if you're looking to set up a blog, you couldn't go wrong with Ghost. Going back to the limitations I mentioned earlier, one of the problems I've come across recently is opening links on a new page. By default, any hyperlink you define in markdown will open within the same browser tab and there is no way to override this in the editor. Although this is not a bug, it takes your users away from your content. But we don't want this, do we? So how do we fix it?

Fix it with MarkDown

The first solution is simple. Instead of creating links like this:

[My Github](https://github.com/cmatskas)

You create them like this:

[My Github](https://github.com/cmatskas" target="_blank)

This is a very simple, yet effective solution. The only problem with this option is that you have to remember to type this every time! If you tend to forget (like me) and you're looking for some kind of automation, then you're in luck.

Fix it with JavaScript

You can inject a bit of JavaScript in your pages to append a target="_blank to all your post content. The code that performs all the "magic" is attached below. Rest assured that there are more flavors of the same script, some in pure JavaScript and some requiring jQuery to be present. I chose this because I like the exclusion list.

// the original version of this code can be found here and full disclosure - it's not mine: 
// https://github.com/kyleshockey/ghost-external-links/blob/master/ghost-external-links.js

window.jQuery || document.write("<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'>\x3C/script>") || console.warn("Ghost External Links was unable to detect or add jQuery to the page.");

$(document).ready(function() {
   $("a[href^=http]").each(function(){
      var excluded = [
         // format for whitelist: 'google.com', 'apple.com', 'myawesomeblog.com'
         // add your excluded domains here
         ];
      for(i=0; i<excluded.length; i++) {
         if(this.href.indexOf(excluded[i]) != -1) {
            return true;
         }
      }
      if(this.href.indexOf(location.hostname) == -1) {
           $(this).click(function() { return true; }); 
           $(this).attr({
               target: "_blank",
               title: "Opens in a new window"
           });
           $(this).click();
      }
   })
});

View ghost-replace-link-target.js on GitHub

There are 2 ways to inject this code to your site: The first involves you editing the actual theme pages. The best place to place this code is in the post.hbs page. Go to content\themes"your current theme"\post.hbs and open the file in the editor of your choice. Add the code supplied above at the bottom of the page, just before the closing {{/post}} tag. Save the file, deploy your changes, restart the site and test that it works. In the example below, I edited the default Casper theme page:

{{!< default}}

{{! The comment above "< default" means - insert everything in this file into
    the {body} of the default.hbs template, which contains our header/footer. }}

{{! Everything inside the #post tags pulls data from the post }}
{{#post}}

<header class="main-header post-head {{#if image}}" style="background-image: url({{image}}){{else}}no-cover{{/if}}">
    <nav class="main-nav {{#if image}}overlay{{/if}} clearfix">
        {{#if @blog.logo}}<a class="blog-logo" href="{{@blog.url}}"><img src="{{@blog.logo}}" alt="{{@blog.title}}" /></a>{{/if}}
        {{#if @blog.navigation}}
            <a class="menu-button icon-menu" href="#"><span class="word">Menu</span></a>
        {{/if}}
    </nav>
</header>

<main class="content" role="main">
    <article class="{{post_class}}">

        <header class="post-header">
            <h1 class="post-title">{{title}}</h1>
            <section class="post-meta">
                <time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time> {{tags prefix=" on "}}
            </section>
        </header>

        <section class="post-content">
            {{content}}
        </section>

        <footer class="post-footer">

        {{! Everything inside the #author tags pulls data from the author }}
        {{#author}}

            {{#if image}}
            <figure class="author-image">
                <a class="img" href="{{url}}" style="background-image: url({{image}})"><span class="hidden">{{name}}'s Picture</span></a>
            </figure>
            {{/if}}

            <section class="author">
                <h4><a href="{{url}}">{{name}}</a></h4>

                {{#if bio}}
                    <p>{{bio}}</p>
                {{else}}
                    <p>Read <a href="{{url}}">more posts</a> by this author.</p>
                {{/if}}
                <div class="author-meta">
                    {{#if location}}<span class="author-location icon-location">{{location}}</span>{{/if}}
                    {{#if website}}<span class="author-link icon-link"><a href="{{website}}">{{website}}</a></span>{{/if}}
                </div>
            </section>

        {{/author}}

            <section class="share">
                <h4>Share this post</h4>
                <a class="icon-twitter" href="https://twitter.com/share?text={{encode title}}&amp;url={{url absolute="true"}}"
                    onclick="window.open(this.href, 'twitter-share', 'width=550,height=235');return false;">
                    <span class="hidden">Twitter</span>
                </a>
                <a class="icon-facebook" href="https://www.facebook.com/sharer/sharer.php?u={{url absolute="true"}}"
                    onclick="window.open(this.href, 'facebook-share','width=580,height=296');return false;">
                    <span class="hidden">Facebook</span>
                </a>
                <a class="icon-google-plus" href="https://plus.google.com/share?url={{url absolute="true"}}"
                   onclick="window.open(this.href, 'google-plus-share', 'width=490,height=530');return false;">
                    <span class="hidden">Google+</span>
                </a>
            </section>

        </footer>

    </article>
</main>

<aside class="read-next">
    {{#next_post}}
    <a class="read-next-story {{#if image}}" style="background-image: url({{image}}){{else}}no-cover{{/if}}" href="{{url}}">
        <section class="post">
            <h2>{{title}}</h2>
            <p>{{excerpt words="19"}}&hellip;</p>
        </section>
    </a>
    {{/next_post}}
    {{#prev_post}}
    <a class="read-next-story prev {{#if image}}" style="background-image: url({{image}}){{else}}no-cover{{/if}}" href="{{url}}">
        <section class="post">
            <h2>{{title}}</h2>
            <p>{{excerpt words="19"}}&hellip;</p>
        </section>
    </a>
    {{/prev_post}}
</aside>

<script>
$(document).ready(function() {
   $("a[href^=http]").each(function(){
      var excluded = [
         // format for whitelist: 'google.com', 'apple.com', 'myawesomeblog.com'
         // add your excluded domains here
         ];
      for(i=0; i<excluded.length; i++) {
         if(this.href.indexOf(excluded[i]) != -1) {
            return true;
         }
      }
      if(this.href.indexOf(location.hostname) == -1) {
           $(this).click(function() { return true; }); 
           $(this).attr({
               target: "_blank",
               title: "Opens in a new window"
           });
           $(this).click();
      }
   })
});
</script>
{{/post}}

View link-javascript-post.hbs on GitHub

The alternative way is to use the admin settings section to inject the code in the footer. Go to Settings -> Code Injection and in the Blog Footer text box, add the code from above. Make sure you click on the "Save" button before you exit. I've included a snapshot below:

That's all you need for now. I'm sure there will soon be a fix for this either in a form of a plugin or an extension within the Markdown editor to support opening links to a new tab/page.

Let me know if you found a better solution to this problem or if you have any questions regarding my suggestions.