diff --git a/README.md b/README.md index a0f9d1f2..3e326486 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,17 @@ feed: The same flag can be used directly in post file. It will disable `` tag for selected post. Settings in post file have higher priority than in config file. +## HTML Excerpts flag + +By default, the `` portion of the feed—containing the post's excerpt or description—will be stripped of all HTML, and will have all its whitespace normalized. The optional flag `html_excerpts` turns that behaviour off, allowing your excerpts to contain full HTML content. + +```yml +feed: + html_excerpts: true +``` + +This is useful in feeds where the excerpts have more complex content or formatting, such as when the post's `description` and `excerpt` front matter options are not set, causing Jekyll to [use the entire first paragraph of the post as the excerpt](https://jekyllrb.com/docs/posts/#post-excerpts). + ## Tags To automatically generate feeds for each tag you apply to your posts you can add a tags setting to your config: diff --git a/lib/jekyll-feed/feed.xml b/lib/jekyll-feed/feed.xml index 9068836f..141d61cd 100644 --- a/lib/jekyll-feed/feed.xml +++ b/lib/jekyll-feed/feed.xml @@ -96,7 +96,11 @@ {% assign post_summary = post.description | default: post.excerpt %} {% if post_summary and post_summary != empty %} - + {% if site.feed.html_excerpts %} + + {% else %} + + {% endif %} {% endif %} {% assign post_image = post.image.path | default: post.image %} diff --git a/spec/jekyll-feed_spec.rb b/spec/jekyll-feed_spec.rb index 41514d1f..51f1301c 100644 --- a/spec/jekyll-feed_spec.rb +++ b/spec/jekyll-feed_spec.rb @@ -162,6 +162,34 @@ expect(post.summary).to be_nil end + it "strips HTML in summaries by default" do + post = feed.items.detect { |item| item.title.content == "Pre" } + expect(post).to_not be_nil + expect(post.summary.content).to_not match "
"
+    end
+
+    it "normalizes whitespace in summaries by default" do
+      post = feed.items.detect { |item| item.title.content == "Pre" }
+      expect(post).to_not be_nil
+      expect(post.summary.content).to_not match "\n"
+    end
+
+    context "with feed.html_excerpts set" do
+      let(:overrides) { { "feed" => { "html_excerpts" => true } } }
+
+      it "doesn't strip HTML in summaries" do
+        post = feed.items.detect { |item| item.title.content == "Pre" }
+        expect(post).to_not be_nil
+        expect(post.summary.content).to match "
"
+      end
+
+      it "doesn't normalize whitespace in summaries" do
+        post = feed.items.detect { |item| item.title.content == "Pre" }
+        expect(post).to_not be_nil
+        expect(post.summary.content).to match "\n"
+      end
+    end
+
     context "with site.lang set" do
       lang = "en_US"
       let(:overrides) { { "lang" => lang } }