Skip to content

Commit

Permalink
generalize replace pattern (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrying authored Mar 25, 2019
1 parent 0f4f9a2 commit 9df6af2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions _plugins/remote_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(tag_name, text, tokens)
check_extension(uri.path)

res = Net::HTTP.get_response(uri)
fail 'resource unavailable' unless res.is_a?(Net::HTTPSuccess)
raise 'resource unavailable' unless res.is_a?(Net::HTTPSuccess)

@content = content_blobber(uri, res).force_encoding('UTF-8')
.sub("\xEF\xBB\xBF", '')
Expand All @@ -37,29 +37,29 @@ def render(_context)
def content_blobber(uri, res)
path_reduced = Pathname.new(uri.to_s).parent.to_s

# Find cases of `](./*.md)`
rel_md_path = %r{][(][.]\/.*\.(?i)(markdown|mdown|mkdn|mkd|md)[)]}
# Find cases of `](./**)`
rel_md_path = %r{][(][.]\/.*[)]}

# Replace raw markdown with GitHub's rendered version
# Replace raw version with GitHub's rendered version
if path_reduced.include? 'raw.githubusercontent'
uri_branch = path_reduced.to_s.split('/')[5]
path_reduced = path_reduced.sub('raw.githubusercontent', 'github')
.sub("/#{uri_branch}", "/blob/#{uri_branch}")
end

# Prepend complete parent path to file with extension
res.body.gsub(rel_md_path) { |s| "](#{path_reduced}/#{s.split('/')[-1]}" }
# Append file to parent path
res.body.gsub(rel_md_path) { |s| "](#{path_reduced}/#{s.gsub('](./', '')}" }
end

def check_protocol(text)
error_message = "remote_markdown: invalid URI given #{text}"
fail error_message unless text =~ URI.regexp(%w[http https ftp ftps])
raise error_message unless text =~ URI.regexp(%w[http https ftp ftps])
end

def check_extension(path)
mdexts = %w[.markdown .mkdown .mkdn .mkd .md]
error_message = "remote_markdown: URI file extension not in #{mdexts}"
fail error_message unless mdexts.include?(File.extname(path))
raise error_message unless mdexts.include?(File.extname(path))
end
end
end
Expand Down

0 comments on commit 9df6af2

Please sign in to comment.