Created
October 28, 2022 12:21
-
-
Save erap129/93dafafc11a91574752f07b145cb6259 to your computer and use it in GitHub Desktop.
Jekyll Medium Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
require 'net/http' | |
require 'json' | |
require 'nokogiri' | |
module Jekyll | |
class JekyllDisplayMediumPosts < Generator | |
safe true | |
priority :high | |
def generate(site) | |
jekyll_coll = Jekyll::Collection.new(site, 'medium_posts_json') | |
site.collections['medium_posts_json'] = jekyll_coll | |
uri = URI("https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@erap129") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new(uri) | |
response = http.request(request) | |
data = JSON.parse(response.read_body) | |
data['items'].each do |item| | |
title = item['title'] | |
path = "./medium_posts/" + title + ".md" | |
path = site.in_source_dir(path) | |
doc = Jekyll::Document.new(path, { :site => site, :collection => jekyll_coll }) | |
puts "====== #{title} ======" | |
puts "#{item['link']}" | |
doc.data['title'] = title; | |
doc.data['image'] = item['thumbnail']; | |
doc.data['link'] = item['link']; | |
doc.data['date'] = item['pubDate']; | |
doc.data['categories'] = item['categories']; | |
html_document = Nokogiri::HTML(item['description']); | |
doc.data['description'] = html_document.search('p').to_html; | |
jekyll_coll.docs << doc | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment