From b973425c0910c3eb779d44736fdf7345431326a1 Mon Sep 17 00:00:00 2001 From: wiwiec Date: Sun, 12 Feb 2017 19:32:12 +0100 Subject: [PATCH] moved all the logic for shortening with delimiters out of the templates into generator.js --- README.md | 4 ++-- atom.xml | 16 +--------------- lib/generator.js | 18 ++++++++++++++++++ rss2.xml | 12 +----------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index d97cf97..fb0ec32 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ feed: hub: content: content_limit: 140 - content_limit_delim: ' ' + content_limit_delim: [".", ":", ",", " "] ``` - **type** - Feed type. (atom/rss2) @@ -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. diff --git a/atom.xml b/atom.xml index 4fa35e7..bada2c4 100644 --- a/atom.xml +++ b/atom.xml @@ -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 %} {% for category in post.categories.toArray() %} diff --git a/lib/generator.js b/lib/generator.js index 14807d6..d843a5e 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,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 += '/'; diff --git a/rss2.xml b/rss2.xml index cb63f96..a116516 100644 --- a/rss2.xml +++ b/rss2.xml @@ -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 %} {% if config.feed.content and post.content %}