Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update remote_markdown.rb #252

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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