Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce medium importer #499

Merged
merged 9 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do Medium articles typically have audio?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Till now I haven't found any post with audio option. But if I am not wrong, it's possible to add video in medium post

"canonical_link" => options.fetch("canonical_link", false),
"extract_tags" => "category",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why not support all of the RSS options? Should we just suggest to users on the docs page for Medium to use the RSS importer and show them how to use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why not support all of the RSS options?

If you see all the options from RSS importer is there except tag.

Should we just suggest to users on the docs page for Medium to use the RSS importer and show them how to use it?

It's possible but it'll be little verbose. Currently this Medium importer is just the wrapper for RSS importer.

})
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[yourfriends@medium.com]]></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>