Last active
September 29, 2018 03:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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