diff --git a/README.md b/README.md index 20db21f..fb0ec32 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ feed: limit: 20 hub: content: + content_limit: 140 + content_limit_delim: [".", ":", ",", " "] ``` - **type** - Feed type. (atom/rss2) @@ -35,3 +37,5 @@ feed: - **limit** - Maximum number of posts in the feed (Use `0` or `false` to show all posts) - **hub** - URL of the PubSubHubbub hubs (Leave it empty if you don't use it) - **content** - (optional) set to 'true' to include the contents of the entire post in the feed. +- **content_limit** - (optional) Default length of post content used in summary. Only used, if **content** setting is false and no custom post description present. +- **content_limit_delim** - (optional) A list of delimiters used to shorten the post content to a feed summary, if **content_limit** is set. If any of the contained delimiters is contained in the post content, cut at its occurence instead of the exact position according to **content_limit**. Delimiters in the list are probed in order for occurrence in the post content. Not used by default. diff --git a/atom.xml b/atom.xml index f67e3ac..1a356c4 100644 --- a/atom.xml +++ b/atom.xml @@ -1,5 +1,5 @@ - + {{ config.title }} {% if config.subtitle %}{{ config.subtitle }}{% endif %} @@ -30,7 +30,11 @@ {% elif post.excerpt %} {{ post.excerpt }} {% elif post.content %} - {{ post.content.substring(0, 140) }} + {% if not config.feed.content and config.feed.content_limit %} + {{ post.feedShortContent }} + {% else %} + {{ post.content }} + {% endif %} {% endif %} {% for category in post.categories.toArray() %} @@ -39,6 +43,9 @@ {% for tag in post.tags.toArray() %} {% endfor %} + {% if post.thumbnail %} + + {% endif %} {% endfor %} diff --git a/index.js b/index.js index f96de9e..08d417b 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,9 @@ var config = hexo.config.feed = assign({ type: 'atom', limit: 20, hub: '', - content: true + content: true, + content_limit: 140, + content_limit_delim: '' }, hexo.config.feed); var type = config.type.toLowerCase(); diff --git a/lib/generator.js b/lib/generator.js index 14807d6..e694bba 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -4,6 +4,7 @@ var nunjucks = require('nunjucks'); var env = new nunjucks.Environment(); var pathFn = require('path'); var fs = require('fs'); +var hexoutil = require('hexo-util'); env.addFilter('uriencode', function(str) { return encodeURI(str); @@ -21,6 +22,25 @@ module.exports = function(locals) { var posts = locals.posts.sort('-date'); if (feedConfig.limit) posts = posts.limit(feedConfig.limit); + + if (typeof feedConfig.content_limit != 'undefined') { + posts.forEach(function(post) { + var safe_content = hexoutil.stripHTML(post.content); + post.feedShortContent = safe_content.substring(0, feedConfig.content_limit); + + if (typeof feedConfig.content_limit_delim != 'undefined') { + var delim_pos = -1; + for (var ind in feedConfig.content_limit_delim) { + var delim = feedConfig.content_limit_delim[ind]; + delim_pos = post.feedShortContent.lastIndexOf(delim); + if (delim_pos > -1) { + post.feedShortContent = post.feedShortContent.substring(0, delim_pos+1); + break; + } + } + } + }); + } var url = config.url; if (url[url.length - 1] !== '/') url += '/'; diff --git a/package.json b/package.json index 8a8fcdf..14035bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-generator-feed", - "version": "1.2.0", + "version": "1.2.2", "description": "Feed generator plugin for Hexo", "main": "index", "scripts": { diff --git a/rss2.xml b/rss2.xml index 123a764..39c8959 100644 --- a/rss2.xml +++ b/rss2.xml @@ -1,7 +1,8 @@ + xmlns:content="http://purl.org/rss/1.0/modules/content/" + xmlns:media="http://search.yahoo.com/mrss/"> {{ config.title }} {{ url | uriencode }} @@ -22,13 +23,24 @@ {% elif post.excerpt %} {{ post.excerpt }} {% elif post.content %} - {{ post.content.substring(0, 140) }} + {% if not config.feed.content and config.feed.content_limit %} + {{ post.feedShortContent }} + {% else %} + {{ post.content }} + {% endif %} {% endif %} {% if config.feed.content and post.content %} {% endif %} {% if post.comments %}{{ (url + post.path) | uriencode }}#disqus_thread{% endif %} + + {% if post.featured_image %} + + {% endif %} + {% if post.thumbnail %} + + {% endif %} {% endfor %}