Skip to content

Commit

Permalink
Add support for external blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
alshedivat committed Apr 23, 2022
1 parent f7dd7f6 commit 8d0c2a6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ group :jekyll_plugins do
gem 'htmlcompressor'
gem 'htmlbeautifier'
end
group :other_plugins do
gem 'httparty'
gem 'feedjira'
end
23 changes: 18 additions & 5 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ pagination:
disqus_shortname: al-folio # put your disqus shortname
# https://help.disqus.com/en/articles/1717111-what-s-a-shortname

# External sources.
# If you have blog posts published on medium.com or other exteranl sources,
# you can display them in your blog by adding a link to the RSS feed.
external_sources:
- name: medium.com
rss_url: https://medium.com/@al-folio/feed
# If the feed contains posts from multiple authors, you can specify the
# author whose posts you want to display on the blog. Leave it empty or
# just delete if you want to show all posts from the feed.
author:

# -----------------------------------------------------------------------------
# Collections
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -161,9 +172,9 @@ plugins:
# Sitemap settings
defaults:
- scope:
path: "assets/**/*.*"
path: "assets/**/*.*"
values:
sitemap: false
sitemap: false
# Extras
github: [metadata]

Expand All @@ -174,10 +185,12 @@ github: [metadata]
# HTML remove comments (<!-- .... -->)
remove_HTML_comments: false

# HTML beautifier (_plugins/beautify.rb) / https://github.com/threedaymonk/htmlbeautifier
# HTML beautifier (_plugins/beautify.rb).
# Source: https://github.com/threedaymonk/htmlbeautifier
beautify: false # This function has conflict with the code snippets, they can be displayed incorrectly

# HTML minify (_plugins/minify.rb) Thanks to: https://www.ffbit.com/blog/2021/03/17/html-minification-in-jekyll.html
# HTML minify (_plugins/minify.rb).
# Source: https://www.ffbit.com/blog/2021/03/17/html-minification-in-jekyll.html
minify: false

# CSS/SASS minify
Expand All @@ -189,7 +202,7 @@ sass:
# -----------------------------------------------------------------------------

jekyll-archives:
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
layouts:
year: archive-year
tag: archive-tag
Expand Down
33 changes: 33 additions & 0 deletions _plugins/external-posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'feedjira'
require 'httparty'
require 'jekyll'

module ExternalPosts
class ExternalPostsGenerator < Jekyll::Generator
safe true
priority :high

def generate(site)
site.config['external_sources'].each do |src|
p "Fetching external posts from #{src['name']}:"
xml = HTTParty.get(src['rss_url']).body
feed = Feedjira.parse(xml)
feed.entries.each do |e|
p "...fetching #{e.url}"
slug = e.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
path = site.in_source_dir("_posts/#{slug}.md")
doc = Jekyll::Document.new(path, { :site => site, :collection => site.collections['posts'] })
doc.data['external_source'] = src['name'];
doc.data['feed_content'] = e.content;
doc.data['title'] = "#{e.title}";
doc.data['description'] = e.summary;
doc.data['date'] = e.published;
doc.data['redirect'] = e.url;
doc.data['categories'] = ["#{src['name']}"];
site.collections['posts'].docs << doc
end
end
end
end

end
17 changes: 14 additions & 3 deletions blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ <h2>{{ site.blog_description }}</h2>
<ul class="post-list">
{% for post in paginator.posts %}

{% assign read_time = post.content | number_of_words | divided_by: 180 | plus: 1 %}
{% if post.external_source == blank %}
{% assign read_time = post.content | number_of_words | divided_by: 180 | plus: 1 %}
{% else %}
{% assign read_time = post.feed_content | strip_html | number_of_words | divided_by: 180 | plus: 1 %}
{% endif %}
{% assign year = post.date | date: "%Y" %}
{% assign tags = post.tags | join: "" %}
{% assign categories = post.categories | join: "" %}
Expand All @@ -34,12 +38,19 @@ <h3>
{% if post.redirect == blank %}
<a class="post-title" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
{% else %}
<a class="post-title" href="{% if post.redirect contains '://' %}{{ post.redirect }}{% else %}{{ post.redirect | relative_url }}{% endif %}">{{ post.title }}</a>
<a class="post-title" href="{% if post.redirect contains '://' %}{{ post.redirect }}{% else %}{{ post.redirect | relative_url }}{% endif %}" target="_blank">{{ post.title }}</a>
<svg width="2rem" height="2rem" viewBox="0 0 40 40" xmlns="http://www.w3.org/2000/svg">
<path d="M17 13.5v6H5v-12h6m3-3h6v6m0-6-9 9" class="icon_svg-stroke" stroke="#999" stroke-width="1.5" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
{% endif %}
</h3>
<p>{{ post.description }}</p>
<p class="post-meta"> {{read_time}} min read &nbsp; &middot; &nbsp;
<p class="post-meta">
{{ read_time }} min read &nbsp; &middot; &nbsp;
{{ post.date | date: '%B %-d, %Y' }}
{%- if post.external_source %}
&nbsp; &middot; &nbsp; {{ post.external_source }}
{%- endif %}
</p>
<p class="post-tags">
<a href="{{ year | prepend: '/blog/' | prepend: site.baseurl}}">
Expand Down

0 comments on commit 8d0c2a6

Please sign in to comment.