Skip to content

Commit

Permalink
Introduce medium importer (#499)
Browse files Browse the repository at this point in the history
Merge pull request 499
  • Loading branch information
sumanmaity112 authored Dec 10, 2022
1 parent 07479df commit a34ac70
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/_data/importer_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ jrnl:
- safe_yaml
marley:
- safe_yaml
medium:
- safe_yaml
mephisto:
- mysql2
- sequel
Expand Down
27 changes: 27 additions & 0 deletions docs/_importers/medium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
layout: docs
title: Medium
prev_section: marley
link_source: medium
next_section: mephisto
permalink: /docs/medium/
---

To import your posts from [Medium](https://medium.com/), run:

{% highlight bash %}
$ ruby -r rubygems -e 'require "jekyll-import";
JekyllImport::Importers::Medium.run({
"username" => "name",
"render_audio" => false,
"canonical_link" => false,
})'
{% endhighlight %}

The `username` refers to the medium username, and it's a mandatory field.

Other optional fields are as follows:
* `canonical_link` – copy original link as `canonical_url` to post. (default: `false`)
* `render_audio` – render `<audio>` element in posts for the enclosure URLs. (default: `false`)

_Note:_ This importer will also import the existing tags/labels from Medium post and include the tags to [Front Matter](https://jekyllrb.com/docs/front-matter/).
36 changes: 36 additions & 0 deletions lib/jekyll-import/importers/medium.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module JekyllImport
module Importers
class Medium < Importer
def self.specify_options(c)
c.option "username", "--username NAME", "Medium username"
c.option "canonical_link", "--canonical_link", "Copy original link as canonical_url to post (default: false)"
c.option "render_audio", "--render_audio", "Render <audio> element in posts for the enclosure URLs (default: false)"
end

def self.validate(options)
abort "Missing mandatory option --username." if options["username"].nil?
end

def self.require_deps
Importers::RSS.require_deps
end

# Medium posts and associated metadata are exported as an RSS Feed. Hence invoke our RSS Importer to create the
# Jekyll source directory.
#
# "Tags" attached to a Medium post are exported under the markup `<item><category>...</category></item>` in the
# export feed. Therefore, configure the RSS Importer to always look for tags in the `<category></category>` field
# of an RSS item.
def self.process(options)
Importers::RSS.process({
"source" => "https://medium.com/feed/@#{options.fetch("username")}",
"render_audio" => options.fetch("render_audio", false),
"canonical_link" => options.fetch("canonical_link", false),
"extract_tags" => "category",
})
end
end
end
end
50 changes: 50 additions & 0 deletions test/mocks/medium_feed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"
xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
<channel>
<title><![CDATA[Stories by John Doe on Medium]]></title>
<description><![CDATA[Stories by John Doe on Medium]]></description>
<link>https://medium.com/@John.Doe?source=rss-621a1d970ada------2</link>
<image>
<url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
<title>Stories by John Doe on Medium</title>
<link>https://medium.com/@John.Doe?source=rss-621a1d970ada------2</link>
</image>
<generator>Medium</generator>
<lastBuildDate>Tue, 06 Dec 2022 06:52:04 GMT</lastBuildDate>
<atom:link href="https://medium.com/@John.Doe/feed" rel="self" type="application/rss+xml"/>
<webMaster><![CDATA[[email protected]]]></webMaster>
<atom:link href="http://medium.superfeedr.com" rel="hub"/>
<item>
<title><![CDATA[This is a Dummy Post]]></title>
<link>https://example.com/blog/this-is-a-dummy-post-50703370aea7?source=rss-621a1d970ada------2</link>
<guid isPermaLink="false">https://medium.com/p/50703370aea7</guid>
<category><![CDATA[java]]></category>
<category><![CDATA[custom-jre]]></category>
<category><![CDATA[image-size-reduce]]></category>
<category><![CDATA[jvm]]></category>
<category><![CDATA[docker]]></category>
<dc:creator><![CDATA[John Doe]]></dc:creator>
<pubDate>Tue, 22 Nov 2022 08:01:46 GMT</pubDate>
<atom:updated>2022-11-23T14:51:57.412Z</atom:updated>
<content:encoded><<![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5nynoXfnHHGCpd0V0p8uw.png"
/></figure><p>The quick brown fox jumps over the lazy dog</p>]]></content:encoded>
</item>
<item>
<title><![CDATA[How I cross post Medium articles to GitHub Pages]]></title>
<link>https://example.com/blog/how-i-cross-post-medium-articles-to-github-pages-1cdf174bc674?source=rss-621a1d970ada------2</link>
<guid isPermaLink="false">https://medium.com/p/1cdf174bc674</guid>
<category><![CDATA[import]]></category>
<category><![CDATA[jekyll]]></category>
<category><![CDATA[github-pages]]></category>
<category><![CDATA[medium]]></category>
<category><![CDATA[personal-website]]></category>
<dc:creator><![CDATA[John Doe]]></dc:creator>
<pubDate>Thu, 10 Nov 2022 21:39:51 GMT</pubDate>
<atom:updated>2022-11-11T09:33:39.955Z</atom:updated>
<content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cR9eGZr50HArf_j9-rnjjQ.png"
/></figure><p>The quick brown fox jumps over the lazy dog</p>]]></content:encoded>
</item>
</channel>
</rss>

0 comments on commit a34ac70

Please sign in to comment.