diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index c98174777..afde12d2c 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -104,39 +104,71 @@ module RepositoriesHelper end # author hui.he - def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name) + def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name) file_path = readme_path.include?('/') ? readme_path.gsub("/#{readme_name}", '') : readme_path.gsub("#{readme_name}", '') return nil if str.blank? content = Base64.decode64(str).force_encoding('UTF-8') - s_regex = /\[.*?\]\((.*?)\)/ + # s_regex = /\s\!\[.*?\]\((.*?)\)\s/ + s_regex_c = /`{1,2}[^`](.*?)`{1,2}/ + s_regex = /```([\s\S]*?)```[\s]?/ + s_regex_1 = /\[.*?\]\((.*?)\)/ src_regex = /src=\"(.*?)\"/ + src_regex_1 = /src=\'(.*?)\'/ + ss_c = content.to_s.scan(s_regex_c) ss = content.to_s.scan(s_regex) + ss_1 = content.to_s.scan(s_regex_1) ss_src = content.to_s.scan(src_regex) - total_sources = ss + ss_src - total_sources.uniq! - total_sources.each do |s| - begin - s_content = s[0] - # 链接直接跳过不做替换 - next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:') || s_content.blank? - ext = File.extname(s_content)[1..-1] - if (image_type?(ext) || download_type(ext)) && !ext.blank? - s_content = File.expand_path(s_content, file_path) - s_content = s_content.split("#{Rails.root}/")[1] - # content = content.gsub(s[0], "/#{s_content}") - s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join - content = content.gsub(s[0], s_content) - else - path = [owner&.login, repo&.identifier, 'tree', ref, file_path].join("/") - s_content = File.expand_path(s_content, path) - s_content = s_content.split("#{Rails.root}/")[1] - content = content.gsub('('+s[0]+')', '('+"/#{s_content}"+')') + ss_src_1 = content.to_s.scan(src_regex_1) + total_sources = {ss_c: ss_c,ss: ss, ss_1: ss_1, ss_src: ss_src, ss_src_1: ss_src_1} + # total_sources.uniq! + total_sources.except(:ss, :ss_c).each do |k, sources| + sources.each do |s| + begin + s_content = s[0] + # 链接直接跳过不做替换 + next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:') || s_content.blank? + ext = File.extname(s_content)[1..-1] + if (image_type?(ext) || download_type(ext)) && !ext.blank? + s_content = File.expand_path(s_content, file_path) + s_content = s_content.split("#{Rails.root}/")[1] + # content = content.gsub(s[0], "/#{s_content}") + s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join + case k.to_s + when 'ss_src' + content = content.gsub("src=\"#{s[0]}\"", "src=\"#{s_content}\"") + when 'ss_src_1' + content = content.gsub("src=\'#{s[0]}\'", "src=\'#{s_content}\'") + else + content = content.gsub("(#{s[0]})", "(#{s_content})") + end + else + path = [owner&.login, repo&.identifier, 'tree', ref, file_path].join("/") + s_content = File.expand_path(s_content, path) + s_content = s_content.split("#{Rails.root}/")[1] + case k.to_s + when 'ss_src' + content = content.gsub("src=\"#{s[0]}\"", "src=\"/#{s_content}\"") + when 'ss_src_1' + content = content.gsub("src=\'#{s[0]}\'", "src=\'/#{s_content}\'") + else + content = content.gsub("(#{s[0]})", "(/#{s_content})") + end + end + rescue + next end - rescue - next end end + after_ss_souces = content.to_s.scan(s_regex) + after_ss_souces.each_with_index do |s, index| + content = content.gsub("#{s[0]}","#{total_sources[:ss][index][0]}") + end + after_ss_c_souces = content.to_s.scan(s_regex_c) + after_ss_c_souces.each_with_index do |s, index| + content = content.gsub("#{s[0]}","#{total_sources[:ss_c][index][0]}") + end + return content rescue return str diff --git a/app/views/api/v1/issues/_detail.json.jbuilder b/app/views/api/v1/issues/_detail.json.jbuilder index 712e7a960..7c3adecc3 100644 --- a/app/views/api/v1/issues/_detail.json.jbuilder +++ b/app/views/api/v1/issues/_detail.json.jbuilder @@ -1,5 +1,5 @@ json.(issue, :id, :subject, :project_issues_index, :description, :branch_name, :start_date, :due_date) -json.blockchain_token_num issue.project&.use_blockchain ? issue.blockchain_token_num : nil +json.blockchain_token_num (Site.has_blockchain? && issue.project&.use_blockchain) ? issue.blockchain_token_num : nil json.created_at issue.created_on.strftime("%Y-%m-%d %H:%M") json.updated_at issue.updated_on.strftime("%Y-%m-%d %H:%M") json.tags issue.show_issue_tags.each do |tag| diff --git a/app/views/api/v1/issues/_simple_detail.json.jbuilder b/app/views/api/v1/issues/_simple_detail.json.jbuilder index 4f288ce20..8b4bb6af3 100644 --- a/app/views/api/v1/issues/_simple_detail.json.jbuilder +++ b/app/views/api/v1/issues/_simple_detail.json.jbuilder @@ -1,5 +1,5 @@ json.(issue, :id, :subject, :project_issues_index) -json.blockchain_token_num issue.project&.use_blockchain ? issue.blockchain_token_num : nil +json.blockchain_token_num (Site.has_blockchain? && issue.project&.use_blockchain) ? issue.blockchain_token_num : nil json.created_at issue.created_on.strftime("%Y-%m-%d %H:%M") json.updated_at issue.updated_on.strftime("%Y-%m-%d %H:%M") json.tags issue.show_issue_tags.each do |tag| diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index 339214cc4..42342f07b 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -2,7 +2,7 @@ if @project.forge? is_dir = @sub_entries.is_a?(Array) file_name = entry['name'] file_type = file_name.starts_with?('.') ? file_name[1..-1] : File.extname(file_name.to_s)[1..-1] - direct_download = ["makefile","dockerfile"].exclude?(file_name.to_s.downcase) && download_type(file_type) + direct_download = %w(makefile dockerfile readme).exclude?(file_name.to_s.downcase) && download_type(file_type) image_type = image_type?(file_type) json.name file_name json.sha entry['sha']