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 c669851 commit 79b3999
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 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 = []

ch_url = "https://zhgchg.li/posts/#{id}/"
ch_url_exists = url_exists(ch_url)
ch_text = "[本文中文版本](#{ch_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")

newLines.append("\r\n\r\n===\r\n\r\nThis article was first published in Traditional Chinese on Medium ➡️ [**View Here**](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 79b3999

Please sign in to comment.