Skip to content

Commit a3e0083

Browse files
alshedivathoraciochacon
authored andcommitted
Add support for external blog posts (alshedivat#647)
* Add support for external blog posts * Cosmetic fixes
1 parent fe94097 commit a3e0083

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ group :jekyll_plugins do
1818
gem 'htmlcompressor'
1919
gem 'htmlbeautifier'
2020
end
21+
group :other_plugins do
22+
gem 'httparty'
23+
gem 'feedjira'
24+
end

_config.yml

+14-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ pagination:
9595
disqus_shortname: al-folio # put your disqus shortname
9696
# https://help.disqus.com/en/articles/1717111-what-s-a-shortname
9797

98+
# External sources.
99+
# If you have blog posts published on medium.com or other exteranl sources,
100+
# you can display them in your blog by adding a link to the RSS feed.
101+
external_sources:
102+
- name: medium.com
103+
rss_url: https://medium.com/@al-folio/feed
104+
98105
# -----------------------------------------------------------------------------
99106
# Collections
100107
# -----------------------------------------------------------------------------
@@ -160,9 +167,9 @@ plugins:
160167
# Sitemap settings
161168
defaults:
162169
- scope:
163-
path: "assets/**/*.*"
170+
path: "assets/**/*.*"
164171
values:
165-
sitemap: false
172+
sitemap: false
166173
# Extras
167174
github: [metadata]
168175

@@ -173,10 +180,12 @@ github: [metadata]
173180
# HTML remove comments (<!-- .... -->)
174181
remove_HTML_comments: false
175182

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

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

182191
# CSS/SASS minify
@@ -188,7 +197,7 @@ sass:
188197
# -----------------------------------------------------------------------------
189198

190199
jekyll-archives:
191-
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
200+
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
192201
layouts:
193202
year: archive-year
194203
tag: archive-tag

_plugins/external-posts.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'feedjira'
2+
require 'httparty'
3+
require 'jekyll'
4+
5+
module ExternalPosts
6+
class ExternalPostsGenerator < Jekyll::Generator
7+
safe true
8+
priority :high
9+
10+
def generate(site)
11+
if site.config['external_sources'] != nil
12+
site.config['external_sources'].each do |src|
13+
p "Fetching external posts from #{src['name']}:"
14+
xml = HTTParty.get(src['rss_url']).body
15+
feed = Feedjira.parse(xml)
16+
feed.entries.each do |e|
17+
p "...fetching #{e.url}"
18+
slug = e.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
19+
path = site.in_source_dir("_posts/#{slug}.md")
20+
doc = Jekyll::Document.new(
21+
path, { :site => site, :collection => site.collections['posts'] }
22+
)
23+
doc.data['external_source'] = src['name'];
24+
doc.data['feed_content'] = e.content;
25+
doc.data['title'] = "#{e.title}";
26+
doc.data['description'] = e.summary;
27+
doc.data['date'] = e.published;
28+
doc.data['redirect'] = e.url;
29+
site.collections['posts'].docs << doc
30+
end
31+
end
32+
end
33+
end
34+
end
35+
36+
end

_sass/_base.scss

+8-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ blockquote {
6464

6565
.card {
6666
background-color: var(--global-card-bg-color);
67-
67+
6868
img {
6969
width: 100%;
7070
}
@@ -314,6 +314,7 @@ footer.sticky-bottom {
314314
color: var(--global-text-color-light);
315315
font-size: 0.875rem;
316316
padding-top: 0.25rem;
317+
padding-bottom: 0;
317318
}
318319
a {
319320
color: var(--global-text-color);
@@ -564,6 +565,7 @@ html.transition *:after {
564565
.post-tags{
565566
color: var(--global-text-color-light);
566567
font-size: 0.875rem;
568+
padding-top: 0.25rem;
567569
padding-bottom: 1rem;
568570
a {
569571
color: var(--global-text-color-light);
@@ -574,15 +576,9 @@ html.transition *:after {
574576
}
575577
}
576578
.post-content{
577-
blockquote {
578-
border-left: 5px solid var(--global-theme-color);
579-
padding: 8px;
580-
}
581-
}
582-
}
583-
584-
.post-tags {
585-
color: var(--global-text-color-light);
586-
font-size: 0.875rem;
587-
padding-top: 0.25rem;
579+
blockquote {
580+
border-left: 5px solid var(--global-theme-color);
581+
padding: 8px;
582+
}
583+
}
588584
}

blog/index.html

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
enabled: true
66
collection: posts
77
permalink: /page/:num/
8-
per_page: 3
8+
per_page: 5
99
sort_field: date
1010
sort_reverse: true
1111
trail:
@@ -24,7 +24,11 @@ <h2>{{ site.blog_description }}</h2>
2424
<ul class="post-list">
2525
{% for post in paginator.posts %}
2626

27-
{% assign read_time = post.content | number_of_words | divided_by: 180 | plus: 1 %}
27+
{% if post.external_source == blank %}
28+
{% assign read_time = post.content | number_of_words | divided_by: 180 | plus: 1 %}
29+
{% else %}
30+
{% assign read_time = post.feed_content | strip_html | number_of_words | divided_by: 180 | plus: 1 %}
31+
{% endif %}
2832
{% assign year = post.date | date: "%Y" %}
2933
{% assign tags = post.tags | join: "" %}
3034
{% assign categories = post.categories | join: "" %}
@@ -34,12 +38,23 @@ <h3>
3438
{% if post.redirect == blank %}
3539
<a class="post-title" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
3640
{% else %}
37-
<a class="post-title" href="{% if post.redirect contains '://' %}{{ post.redirect }}{% else %}{{ post.redirect | relative_url }}{% endif %}">{{ post.title }}</a>
41+
{% if post.redirect contains '://' %}
42+
<a class="post-title" href="{{ post.redirect }}" target="_blank">{{ post.title }}</a>
43+
<svg width="2rem" height="2rem" viewBox="0 0 40 40" xmlns="http://www.w3.org/2000/svg">
44+
<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>
45+
</svg>
46+
{% else %}
47+
<a class="post-title" href="{{ post.redirect | relative_url }}">{{ post.title }}</a>
48+
{% endif %}
3849
{% endif %}
3950
</h3>
4051
<p>{{ post.description }}</p>
41-
<p class="post-meta"> {{read_time}} min read &nbsp; &middot; &nbsp;
52+
<p class="post-meta">
53+
{{ read_time }} min read &nbsp; &middot; &nbsp;
4254
{{ post.date | date: '%B %-d, %Y' }}
55+
{%- if post.external_source %}
56+
&nbsp; &middot; &nbsp; {{ post.external_source }}
57+
{%- endif %}
4358
</p>
4459
<p class="post-tags">
4560
<a href="{{ year | prepend: '/blog/' | prepend: site.baseurl}}">

0 commit comments

Comments
 (0)