├── .gitignore
├── LICENSE
├── jquery.ghostrelated.min.js
├── README.md
└── jquery.ghostrelated.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | .DS_Store
3 | index.html
4 | test.xml
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Dane Grant
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/jquery.ghostrelated.min.js:
--------------------------------------------------------------------------------
1 | (function($){defaults={feed:"/rss",titleClass:".post-title",tagsClass:".post-meta",limit:5,debug:false,template:'
',messages:{noRelated:"No related posts were found."}};function RelatedPosts(element,options){this.element=element;this.options=$.extend({},defaults,options);this.parseRss()}RelatedPosts.prototype.displayRelated=function(posts){var self=this,count=0;this._currentPostTags=this.getCurrentPostTags(this.options.tagsClass);var related=this.matchByTag(this._currentPostTags,posts);var options=this.options;related.forEach(function(post){var template=options.template.replace(/{[^{}]+}/g,function(key){return post[key.replace(/[{}]+/g,"")]||""});if(count"+this.messages.noRelated+""))}};RelatedPosts.prototype.parseRss=function(pageNum,prevId,feeds){var page=pageNum||1,prevId=prevId||"",feeds=feeds||[],self=this;$.ajax({url:this.options.feed+"/"+page,type:"GET"}).done(function(data,textStatus,xhr){var curId=$(data).find("item > guid").text();if(curId!=prevId){feeds.push(data);self.parseRss(page+1,curId,feeds)}else{var posts=self.getPosts(feeds);self.displayRelated(posts)}}).fail(function(e){self.reportError(e)})};RelatedPosts.prototype.getCurrentPostTitle=function(titleClass){if(titleClass[0]!="."){titleClass="."+titleClass}var postTitle=$(titleClass).text();if(postTitle.length<1){this.reportError("Couldn't find the post title with class: "+titleClass)}return postTitle};RelatedPosts.prototype.getCurrentPostTags=function(tagsClass){if(tagsClass[0]!="."){tagsClass="."+tagsClass}var tags=[];$(tagsClass+" a").each(function(){tags.push($(this).text())});if(tags.length<1){this.reportError("Couldn't find any tags in this post")}return tags};RelatedPosts.prototype.getPosts=function(feeds){var posts=[],items=[];feeds.forEach(function(feed){items=$.merge(items,$(feed).find("item"))});for(var i=0;i"+error+""))}};$.fn.ghostRelated=function(options){return this.each(function(){new RelatedPosts(this,options)})}})(jQuery);
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | jquery.ghostrelated
2 | ===================
3 |
4 | This jQuery plugin displays a list of related posts for the Ghost blogging platform.
5 |
6 | ## How it works
7 |
8 | Ghost Related parses your blogs rss feed and matches the current post with posts from your blog that have any tags in common. You will need to attach the plugin to an unordered or ordered list in your post.hbs template to output the related posts
9 |
10 | ## Installation
11 |
12 | 1. Download jquery.ghostrelated.js or jquery.ghostrelated.min.js and save it to the js folder in your theme directory.
13 | 2. Include the script below `{{ghost_foot}}` in your themes default.hbs file ``
14 | ```html
15 | {{! Ghost outputs important scripts and data with this tag (jquery is included in ghost_foot) }}
16 | {{ghost_foot}}
17 | ```
18 |
19 |
20 |
21 |
22 | 3. Add an ordered or unordered list with a class identifier (related-posts is the default class identifier) in your post.hbs template file.
23 | ```html
24 |
25 |
26 |
27 | {{/post}}
28 | ```
29 |
30 | 4. Call the ghostrelated plugin on the list class identifier that you created in step 3 somewhere in your main js file
31 | ```javascript
32 | $('.related-posts').ghostRelated();
33 | ```
34 |
35 |
36 | ## Options
37 | The theme has some default options defined for you. Based on your theme you may need to override some of these when you initiate the plugin
38 | ```javascript
39 | defaults = {
40 | feed: '/rss',
41 | titleClass: '.post-title',
42 | tagsClass: '.post-meta',
43 | debug: false,
44 | template: '
',
45 | messages: {
46 | noRelated: 'No related posts were found.'
47 | }
48 | }
49 | ```
50 |
51 | #### feed:
52 | This is the location of the rss feed for your blog. Ghost uses /rss, so you shouldn't need to change this.
53 |
54 | #### titleClass:
55 | This is the class identifier for the heading element for your single post title. This makes sure that the post that is currently being read does not come back as a related post.
56 |
57 | Ghost's default casper theme uses: `
Title of current post
` by default.
58 |
59 | #### tagsClass:
60 | This is the class identifier for the element that the current post tags are in. This is used to grab the current post tags and match them against other posts.
61 |
62 | It looks like this in casper:
63 | ```html
64 |
65 | on
66 | Getting Started |
67 | Another Tag
68 |
69 | ```
70 |
71 | So the option uses .post-meta and the plugin looks for the anchor tags inside of it
72 |
73 | #### limit:
74 |
75 | Limit amount of related posts to be displayed
76 |
77 | #### debug:
78 |
79 | If the plugin isn't returning any related posts, set this option to true. This option will output error information to the list you created to help you figure out why the plugin is failing.
80 |
81 | #### template:
82 |
83 | Specify the template for the related posts. You have access to all variables within the `post` object from the RSS feed, and you can echo them by wrapping them in braces.
84 |
85 | ## Start with custom options
86 |
87 | ```javascript
88 | $('.related-posts').ghostRelated({
89 | titleClass: '.my-title',
90 | tagsClass: '.my-tags-class',
91 | template: '