Skip to content

Commit

Permalink
optimize for jekyllrb
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed Jun 7, 2022
1 parent 92aa73f commit b6ebc7b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project can help you to make an auto-sync or auto-backup service from Mediu
- [X] Highly optimized markdown format for Medium
- [X] Native Markdown Style Render Engine
(Feel free to contribute if you any optimize idea! `MarkupStyleRender.rb`)
- [X] jekyllrb friendly

## Result
- [Original post on Medium](https://medium.com/pinkoi-engineering/%E5%AF%A6%E6%88%B0%E7%B4%80%E9%8C%84-4-%E5%80%8B%E5%A0%B4%E6%99%AF-7-%E5%80%8B-design-patterns-78507a8de6a5)
Expand Down
2 changes: 1 addition & 1 deletion ZMediumToMarkdown.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'ZMediumToMarkdown'
gem.version = '1.6.0'
gem.version = '1.6.1'

gem.license = "MIT"

Expand Down
2 changes: 1 addition & 1 deletion ZMediumToMarkdown_Github.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'zmediumtomarkdown'
gem.version = '1.6.0'
gem.version = '1.6.1'

gem.license = "MIT"

Expand Down
7 changes: 6 additions & 1 deletion lib/Helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ def self.downloadLatestVersion()
end

def self.createPostInfo(postInfo)

title = postInfo.title.gsub("[","")
title = title.gsub("]","")

result = "---\n"
result += "title: #{postInfo.title}\n"
result += "title: #{title}\n"
result += "author: #{postInfo.creator}\n"
result += "date: #{postInfo.firstPublishedAt.strftime('%Y-%m-%dT%H:%M:%S.%LZ')}\n"
result += "categories: #{postInfo.collectionName}\n"
result += "tags: [#{postInfo.tags.join(",")}]\n"
result += "---\n"
result += "\r\n"
Expand Down
9 changes: 7 additions & 2 deletions lib/Parsers/IMGParser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ def parse(paragraph)
imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), paragraph.postID)
absolutePath = imagePathPolicy.getAbsolutePath(fileName)

comment = ""
if paragraph.text != ""
comment = " \"#{paragraph.text}\""
end

if ImageDownloader.download(absolutePath, imageURL)
relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
"![#{paragraph.text}](#{relativePath} \"#{paragraph.text}\")"
"![#{paragraph.text}](/#{relativePath}#{comment})"
else
"![#{paragraph.text}](#{imageURL} \"#{paragraph.text}\")"
"![#{paragraph.text}](#{imageURL}#{comment})"
end
else
if !nextParser.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/Parsers/IframeParser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def parse(paragraph)
gistHTML.search('a').each do |a|
if a.text == 'view raw'
gistRAW = Request.body(Request.URL(a['href']))
result = "```#{lang}\n#{gistRAW}\n```"
result = "```#{lang.downcase}\n#{gistRAW}\n```"
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/Post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Post

class PostInfo
attr_accessor :title, :tags, :creator, :firstPublishedAt, :latestPublishedAt
attr_accessor :title, :tags, :creator, :firstPublishedAt, :latestPublishedAt, :collectionName
end

def self.getPostIDFromPostURLString(postURLString)
Expand Down Expand Up @@ -68,6 +68,13 @@ def self.parsePostInfoFromPostContent(content, postID)
postInfo.creator = content&.dig(creatorRef, "name")
end

colletionRef = content&.dig("Post:#{postID}", "collection", "__ref")
if !colletionRef.nil?
postInfo.collectionName = content&.dig(colletionRef, "name")
end



firstPublishedAt = content&.dig("Post:#{postID}", "firstPublishedAt")
if !firstPublishedAt.nil?
postInfo.firstPublishedAt = Time.at(0, firstPublishedAt, :millisecond)
Expand Down
7 changes: 5 additions & 2 deletions lib/ZMediumFetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require "Request"
require "Post"
require "User"
require 'date'

class ZMediumFetcher

Expand Down Expand Up @@ -204,15 +205,17 @@ def downloadPost(postURL, pathPolicy)

postPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "posts")

imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "images")
imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "assets")
startParser = buildParser(imagePathPolicy)

progress.totalPostParagraphsLength = paragraphs.length
progress.currentPostParagraphIndex = 0
progress.message = "Converting Post..."
progress.printLog()

absolutePath = postPathPolicy.getAbsolutePath("#{postPath}.md")
postWithDatePath = "#{postInfo.firstPublishedAt.strftime("%Y-%m-%d")}-#{postPath}"

absolutePath = postPathPolicy.getAbsolutePath("#{postWithDatePath}.md")

# if markdown file is exists and last modification time is >= latestPublishedAt(last update post time on medium)
if File.file?(absolutePath) && File.mtime(absolutePath) >= postInfo.latestPublishedAt
Expand Down

0 comments on commit b6ebc7b

Please sign in to comment.