Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only shorten feeds at occurrences of delimiters #52

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ feed:
limit: 20
hub:
content:
content_limit: 140
content_limit_delim: [".", ":", ",", " "]
```

- **type** - Feed type. (atom/rss2)
- **path** - Feed path. (Default: atom.xml/rss2.xml)
- **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.
11 changes: 9 additions & 2 deletions atom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<title>{{ config.title }}</title>
{% if config.subtitle %}<subtitle>{{ config.subtitle }}</subtitle>{% endif %}
<link href="{{ feed_url | uriencode }}" rel="self"/>
Expand Down Expand Up @@ -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 %}
</summary>
{% for category in post.categories.toArray() %}
Expand All @@ -39,6 +43,9 @@
{% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ (url + tag.path) | uriencode }}"/>
{% endfor %}
{% if post.thumbnail %}
<media:thumbnail url="{{ (url + post.thumbnail) | uriencode }}" width="48" height="48" />
{% endif %}
</entry>
{% endfor %}
</feed>
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 += '/';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
16 changes: 14 additions & 2 deletions rss2.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>{{ config.title }}</title>
<link>{{ url | uriencode }}</link>
Expand All @@ -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 %}
</description>
{% if config.feed.content and post.content %}
<content:encoded><![CDATA[{{ post.content | safe }}]]></content:encoded>
{% endif %}
{% if post.comments %}<comments>{{ (url + post.path) | uriencode }}#disqus_thread</comments>{% endif %}

{% if post.featured_image %}
<media:content medium="image" url="{{ (url + post.featured_image) | uriencode }}" />
{% endif %}
{% if post.thumbnail %}
<media:thumbnail url="{{ (url + post.thumbnail) | uriencode }}" width="48" height="48" />
{% endif %}
</item>
{% endfor %}
</channel>
Expand Down