Skip to content

Instantly share code, notes, and snippets.

@chloerei
Last active December 24, 2015 05:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chloerei/6750044 to your computer and use it in GitHub Desktop.
Save chloerei/6750044 to your computer and use it in GitHub Desktop.
Fetch images and replace link in markdown posts.
# = Usage
# Download to your jekyll site directory, and run it
# `ruby jekyll_image_fetcher.rb`
Dir['_posts/*.md'].each do |path|
content = File.open(path).read
basename = File.basename path, '.md'
content.gsub!(/!\[\]\((.+)\)/) do |m|
url = $1
`mkdir -p images/posts/#{basename}`
`wget #{url} -P images/posts/#{basename}`
m.gsub $1, "/images/posts/#{basename}/#{File.basename url}"
end
File.open(path, 'w') do |file|
file.write content
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment