Skip to content

Instantly share code, notes, and snippets.

@atika
Last active September 29, 2018 03:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atika/491d9d160c81b7eea6c272e9d1c21ea6 to your computer and use it in GitHub Desktop.
Save atika/491d9d160c81b7eea6c272e9d1c21ea6 to your computer and use it in GitHub Desktop.
Use Marked 2 markdown preprocessor functionality to rewrite each image URL to an absolute path on the system before displaying the preview.
#!/usr/bin/ruby
require 'pathname'
imgURLRegex = /^(!\[.*?\]\()([^\s\)]*)(\s*.*\))/
rootsPaths = [
'/Users/username/Path/To/hexo/source/',
'/Another/PathTo/Website/Directory/'
]
filePath = ENV["MARKED_PATH"]
if RUBY_VERSION.to_f > 1.9
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
content = STDIN.read.force_encoding('utf-8')
else
content = STDIN.read
end
rootsPaths.each { |root|
if File.realpath(filePath).start_with?(root)
# Rewrite every image url with an absolute path on the system
content.gsub!(imgURLRegex) {|tag|
tag_parts = Regexp.last_match
tag_parts[1] + File.expand_path(File.join(root, tag_parts[2])) + tag_parts[3]
# puts File.join(root, tag_parts[2])
# puts tag_parts.inspect
}
print content
exit
end
}
print 'NOCUSTOM'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment