Skip to content

Commit

Permalink
update script
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed May 26, 2024
1 parent 7e5110f commit 19de744
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion _data/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ copyright:
meta: >-
Using the :PLATFORM theme :THEME <br/>
Automatically sync Medium articles with <a href="https://github.com/ZhgChgLi/ZMediumToJekyll" target="_blank">ZMediumToJekyll</a>.<br/>
<a href="https://en.zhgchg.li" target="_blank">English Version of ZhgChg.Li</a><br/>
Powered by <a href="https://github.com/ZhgChgLi/ZMediumToMarkdown" target="_blank">ZMediumToMarkdown</a>.
not_found:
Expand Down
27 changes: 25 additions & 2 deletions tools/zhgchgli_optimize_footer.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
require 'net/http'
require 'uri'

class Main
def run()
files = Dir['_posts/zmediumtomarkdown/*.md']
files.each do |file|

id = file.split("-").last.split(".").first

lines = File.readlines(file)
newLines = []

en_url = "https://en.zhgchg.li/posts/#{id}/"
en_url_exists = url_exists(en_url)
en_text = "[View the English version of this article here.](#{en_url}){:target=\"_blank\"}\r\n"

lines.each do |line|
if line.include? "延伸閱讀" or line.include? "本文同步發表於" or line.include? "Like Z Realm" or line.include? "有任何問題及指教歡迎與我聯絡。" or line.include? "converted from Medium by [ZMediumToMarkdown]"
id = file.split("-").last.split(".").first

newLines.append("\r\n\r\n===\r\n\r\n 本文首次發表於 Medium ➡️ [**前往查看**](https://medium.com/p/#{id}){:target=\"_blank\"}\r\n")
if ch_url_exists
newLines.append(ch_text)
end

break
end
newLines.append(line)
Expand All @@ -22,6 +34,17 @@ def run()

puts "Optimze Markdown Footer Success!"
end

def url_exists(url)
uri = URI.parse(url)
request = Net::HTTP.new(uri.host, uri.port)
request.use_ssl = (uri.scheme == 'https')

path = uri.path.empty? ? '/' : uri.path
response = request.request_head(path)

response.code.to_i != 404
end
end

main = Main.new()
Expand Down

0 comments on commit 19de744

Please sign in to comment.