Skip to content

Commit 83774e1

Browse files
alshedivatpecey
authored andcommitted
Add support for external blog posts (alshedivat#647)
* Add support for external blog posts * Cosmetic fixes
1 parent 6119853 commit 83774e1

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
@@ -92,6 +92,13 @@ pagination:
9292
# Comments
9393
disqus_shortname: #al-folio # put your disqus shortname
9494

95+
# External sources.
96+
# If you have blog posts published on medium.com or other exteranl sources,
97+
# you can display them in your blog by adding a link to the RSS feed.
98+
external_sources:
99+
- name: medium.com
100+
rss_url: https://medium.com/@al-folio/feed
101+
95102
# -----------------------------------------------------------------------------
96103
# Collections
97104
# -----------------------------------------------------------------------------
@@ -157,9 +164,9 @@ plugins:
157164
# Sitemap settings
158165
defaults:
159166
- scope:
160-
path: "assets/**/*.*"
167+
path: "assets/**/*.*"
161168
values:
162-
sitemap: false
169+
sitemap: false
163170
# Extras
164171
github: [metadata]
165172

@@ -170,10 +177,12 @@ github: [metadata]
170177
# HTML remove comments (<!-- .... -->)
171178
remove_HTML_comments: false
172179

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

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

179188
# CSS/SASS minify
@@ -185,7 +194,7 @@ sass:
185194
# -----------------------------------------------------------------------------
186195

187196
jekyll-archives:
188-
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
197+
enabled: [year, tags, categories] # enables year, tag and category archives (remove if you need to disable one of them).
189198
layouts:
190199
year: archive-year
191200
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
}
@@ -315,6 +315,7 @@ footer.sticky-bottom {
315315
color: var(--global-text-color-light);
316316
font-size: 0.875rem;
317317
padding-top: 0.25rem;
318+
padding-bottom: 0;
318319
}
319320
a {
320321
color: var(--global-text-color);
@@ -565,6 +566,7 @@ html.transition *:after {
565566
.post-tags{
566567
color: var(--global-text-color-light);
567568
font-size: 0.875rem;
569+
padding-top: 0.25rem;
568570
padding-bottom: 1rem;
569571
a {
570572
color: var(--global-text-color-light);
@@ -575,15 +577,9 @@ html.transition *:after {
575577
}
576578
}
577579
.post-content{
578-
blockquote {
579-
border-left: 5px solid var(--global-theme-color);
580-
padding: 8px;
581-
}
582-
}
583-
}
584-
585-
.post-tags {
586-
color: var(--global-text-color-light);
587-
font-size: 0.875rem;
588-
padding-top: 0.25rem;
580+
blockquote {
581+
border-left: 5px solid var(--global-theme-color);
582+
padding: 8px;
583+
}
584+
}
589585
}

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)