Skip to content

Commit

Permalink
moved all the logic for shortening with delimiters out of the templat…
Browse files Browse the repository at this point in the history
…es into generator.js
  • Loading branch information
wiwie committed Feb 12, 2017
1 parent 0348f84 commit b973425
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ feed:
hub:
content:
content_limit: 140
content_limit_delim: ' '
content_limit_delim: [".", ":", ",", " "]
```

- **type** - Feed type. (atom/rss2)
Expand All @@ -38,4 +38,4 @@ feed:
- **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) If **content_limit** is used to shorten post contents, only cut at the last occurrence of this delimiter before reaching the character limit. Not used by default.
- **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.
16 changes: 1 addition & 15 deletions atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,7 @@
{% elif post.excerpt %}
{{ post.excerpt }}
{% elif post.content %}
{% set short_content = post.content.substring(0, config.feed.content_limit) %}
{% if config.feed.content_limit_delim %}
{% set delim_pos = short_content.lastIndexOf(config.feed.content_limit_delim) %}
{{ short_content }}
{{ short_content.lastIndexOf(config.feed.content_limit_delim) }}
{{ delim_pos }}
{{ delim_pos > -1 }}
{% if delim_pos > -1 %}
{{ short_content.substring(0, delim_pos+1) }}
{% else %}
{{ short_content }}
{% endif %}
{% else %}
{{ short_content }}
{% endif %}
{{ post.feedShortContent }}
{% endif %}
</summary>
{% for category in post.categories.toArray() %}
Expand Down
18 changes: 18 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,23 @@ module.exports = function(locals) {

var posts = locals.posts.sort('-date');
if (feedConfig.limit) posts = posts.limit(feedConfig.limit);

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
12 changes: 1 addition & 11 deletions rss2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@
{% elif post.excerpt %}
{{ post.excerpt }}
{% elif post.content %}
{% set short_content = post.content.substring(0, config.feed.content_limit) %}
{% if config.feed.content_limit_delim %}
{% set delim_pos = short_content.lastIndexOf(config.feed.content_limit_delim) %}
{% if delim_pos > -1 %}
{{ short_content.substring(0, delim_pos) }}
{% else %}
{{ short_content }}
{% endif %}
{% else %}
{{ short_content }}
{% endif %}
{{ post.feedShortContent }}
{% endif %}
</description>
{% if config.feed.content and post.content %}
Expand Down

0 comments on commit b973425

Please sign in to comment.