You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.4 KiB

4 years ago
  1. # jquery.rss [![Build Status](https://travis-ci.org/sdepold/jquery-rss.svg?branch=master)](https://travis-ci.org/sdepold/jquery-rss)
  2. This plugin can be used to read a RSS feed and transform it into a custom piece of HTML.
  3. ## Alternatives
  4. A vanilla JavaScript version of this library can be found here: [Vanilla RSS](https://github.com/sdepold/vanilla-rss).
  5. This plugin uses [Feedr](https://github.com/sdepold/feedr), a backend server that parses and converts RSS feeds into its JSON representation. The server was built as a drop-in replacement for Google's former Feed API.
  6. ## Support
  7. Since version 3.4.0 of jquery.rss, users have the chance to support funding future developments and
  8. covering the costs for the hosting of jquery.rss' respective server side companion app [feedr](https://github.com/sdepold/feedr).
  9. Every once in a while supporters will get affiliate links instead of one of the feed's entries.
  10. If you are not interested in supporting the authors of the plugin, then you can easily opt-out of it by setting the respective
  11. `support` option. See below for further details.
  12. Thanks in advance!
  13. ## Installation
  14. Through npm:
  15. ```
  16. $ npm install jquery
  17. $ npm install jquery-rss
  18. const $ = require('jquery');
  19. require('jquery-rss); // This will add the plugin to the jQuery namespace
  20. ```
  21. Through cdnjs:
  22. ```
  23. <script src="http://code.jquery.com/jquery-1.11.0.js"></script>
  24. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-rss/3.3.0/jquery.rss.min.js"></script>
  25. ```
  26. ## Setup
  27. ```html
  28. <!DOCTYPE html>
  29. <html>
  30. <head>
  31. <title>jquery.rss example</title>
  32. <script src="lib/jquery-1.6.4.min.js"></script>
  33. <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
  34. <script src="dist/jquery.rss.min.js"></script>
  35. <script>
  36. jQuery(function($) {
  37. $("#rss-feeds").rss("http://feeds.feedburner.com/premiumpixels");
  38. });
  39. </script>
  40. </head>
  41. <body>
  42. <div id="rss-feeds"></div>
  43. </body>
  44. </html>
  45. ```
  46. Demo link for above code: http://embed.plnkr.co/WQRoCYLld162uplnz1rc/preview
  47. Note: Moment.js is _optional_. If you include it, jquery.rss will use it to format dates.
  48. If you do not want to include Moment.js, you may opt for providing your own date formatting function, or for not formatting dates at all.
  49. ## Options
  50. ```javascript
  51. $("#rss-feeds").rss(
  52. // You can either provide a single feed URL or a list of URLs (via an array)
  53. "http://feeds.feedburner.com/premiumpixels",
  54. {
  55. // how many entries do you want?
  56. // default: 4
  57. // valid values: any integer
  58. limit: 10,
  59. // want to offset results being displayed?
  60. // default: false
  61. // valid values: any integer
  62. offsetStart: false, // offset start point
  63. offsetEnd: false, // offset end point
  64. // will request the API via https
  65. // default: false
  66. // valid values: false, true
  67. ssl: true,
  68. // which server should be requested for feed parsing
  69. // the server implementation is here: https://github.com/sdepold/feedr
  70. // default: feedrapp.info
  71. // valid values: any string
  72. host: "my-own-feedr-instance.com",
  73. // option to seldomly render ads
  74. // ads help covering the costs for the feedrapp server hosting and future improvements
  75. // default: true
  76. // valid values: false, true
  77. support: false,
  78. // outer template for the html transformation
  79. // default: "<ul>{entries}</ul>"
  80. // valid values: any string
  81. layoutTemplate: "<div class='feed-container'>{entries}</div>",
  82. // inner template for each entry
  83. // default: '<li><a href="{url}">[{author}@{date}] {title}</a><br/>{shortBodyPlain}</li>'
  84. // valid values: any string
  85. entryTemplate: "<p>{title}</p>",
  86. // additional token definition for in-template-usage
  87. // default: {}
  88. // valid values: any object/hash
  89. tokens: {
  90. foo: "bar",
  91. bar: function(entry, tokens) {
  92. return entry.title;
  93. }
  94. },
  95. // formats the date with moment.js (optional)
  96. // default: 'dddd MMM Do'
  97. // valid values: see http://momentjs.com/docs/#/displaying/
  98. dateFormat: "MMMM Do, YYYY",
  99. // localizes the date with moment.js (optional)
  100. // default: 'en'
  101. dateLocale: "de",
  102. // Defines the format which is used for the feed.
  103. // Default: null (utf8)
  104. // valid values: https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings
  105. encoding: "ISO-8859-1",
  106. // Defined the order of the feed's entries.
  107. // Default: undefined (keeps the order of the original feed)
  108. // valid values: All entry properties; title, link, content, contentSnippet, publishedDate, categories, author, thumbnail
  109. // Order can be reversed by prefixing a dash (-)
  110. order: "-publishedDate",
  111. // formats the date in whatever manner you choose. (optional)
  112. // this function should return your formatted date.
  113. // this is useful if you want to format dates without moment.js.
  114. // if you don't use moment.js and don't define a dateFormatFunction, the dates will
  115. // not be formatted; they will appear exactly as the RSS feed gives them to you.
  116. dateFormatFunction: function(date) {},
  117. // a callback, which gets triggered when an error occurs
  118. // default: function() { throw new Error("jQuery RSS: url don't link to RSS-Feed") }
  119. error: function() {},
  120. // a callback, which gets triggered when everything was loaded successfully
  121. // this is an alternative to the next parameter (callback function)
  122. // default: function(){}
  123. success: function() {},
  124. // a callback, which gets triggered once data was received but before the rendering.
  125. // this can be useful when you need to remove a spinner or something similar
  126. onData: function() {}
  127. },
  128. // callback function
  129. // called after feeds are successfully loaded and after animations are done
  130. function callback() {}
  131. );
  132. ```
  133. ### Note about the host option
  134. Since version 3.0.0 the plugin is no longer using the Google Feed API but a drop-in replacement called [feedr](https://feedrapp.info). That server is currently running on Heroku and might have some downtimes, interruptions or unexpected issues. While I will try to keep those problems as rare as possible, it can totally happen from time to time. I might move the service to some other provide or even improve the infrastructure.
  135. If you don't want to rely on the [provided server](http://feedrapp.info) and instead run your own version, you can just download feedr, install the dependencies and run it. As written above, you can specify the host which is used to parse the feeds with the `host` option.
  136. ## Templating
  137. As seen in the options, you can specify a template in order to transform the json objects into HTML. In order to that, you can either define the outer template (which describes the html around the entries) or the entry template (which describes the html of an entry).
  138. The basic format of those templates are:
  139. ```html
  140. <!-- layoutTemplate: -->
  141. "<outer-html>{entries}</outer-html>"
  142. <!-- entryTemplate: -->
  143. "<any-html>{token1}{token2}</any-html>"
  144. ```
  145. So, let's say you have specified a limit of 2, using the upper pseudo html. This will result in the following:
  146. ```html
  147. <outer-html>
  148. <any-html>{token1}{token2}</any-html>
  149. <any-html>{token1}{token2}</any-html>
  150. </outer-html>
  151. ```
  152. There are some predefined tokens:
  153. - url: the url to the post
  154. - author: the author of the post
  155. - date: the publishing date
  156. - title: the title of the post
  157. - body: the complete content of the post
  158. - shortBody: the shortened content of the post
  159. - bodyPlain: the complete content of the post without html
  160. - shortBodyPlain: the shortened content of the post without html
  161. - teaserImage: the first image in the post's body
  162. - teaserImageUrl: the url of the first image in the post's body
  163. - index: the index of the current entry
  164. - totalEntries: the total count of the entries
  165. - feed: contains high level information of the feed (e.g. title of the website)
  166. You can also define custom tokens using the `tokens` option:
  167. ```javascript
  168. $("#foo").rss(url, {
  169. entryTemplate: "{dynamic}, {static}, {re-use}",
  170. tokens: {
  171. dynamic: function(entry, tokens) {
  172. return "dynamic-stuff: " + entry.title;
  173. },
  174. "re-use": function(entry, tokens) {
  175. return encodeURIComponent(tokens.teaserImageUrl);
  176. },
  177. static: "static"
  178. }
  179. });
  180. ```
  181. Please make sure to NOT define infinite loops. The following example is really BAD:
  182. ```javascript
  183. $('#foo').rss(url, {
  184. entryTemplate: "{loop}",
  185. tokens: {
  186. whoops: function(entry, tokens) { return tokens.loop() }
  187. loop: function(entry, tokens) { return tokens.whoops() }
  188. }
  189. })
  190. ```
  191. Here is a real-world example:
  192. ```javascript
  193. $("#foo").rss(url, {
  194. layoutTemplate: "<table><tr><th>Title</th></tr>{entries}</table>",
  195. entryTemplate: "<tr><td>{title}</td></tr>"
  196. });
  197. ```
  198. ## Filtering
  199. The plugin also allows you to filter specific entries in order to only print them:
  200. ```javascript
  201. $("#foo").rss(url, {
  202. limit: 100,
  203. filterLimit: 10,
  204. filter: function(entry, tokens) {
  205. return tokens.title.indexOf("my filter") > -1;
  206. }
  207. });
  208. ```
  209. This will request 100 entries via the Feed API and renders the first 10 matching entries.
  210. ## Testing
  211. The test suite is using BusterJS. In order to successfully run the tests you will need [phantomjs](http://phantomjs.org/).
  212. If that is installed you only have to run `npm test`.
  213. ## Authors/Contributors
  214. - Sascha Depold ([Twitter](http://twitter.com/sdepold) | [Github](http://github.com/sdepold) | [Website](http://depold.com))
  215. - Steffen Schröder ([Twitter](http://twitter.com/ChaosSteffen) | [Github](http://github.com/ChaosSteffen) | [Website](http://schroeder-blog.de))