diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 75e409e66..cc284b4cb 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -32,6 +32,7 @@ class RepositoriesController < ApplicationController @project_owner = @project.owner @entries = Gitea::Repository::Entries::ListService.new(@project_owner, @project.identifier, ref: @ref).call @entries = @entries.sort_by{ |hash| hash['type'] } + @path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" end def sub_entries diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index b35fa2b99..be1029be1 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -18,4 +18,36 @@ module RepositoriesHelper return nil if author_json.blank? find_user_by_login author_json['login'] end + + def readme_render_decode64_content(str, path) + return nil if str.blank? + content = Base64.decode64(str).force_encoding('UTF-8') + + c_regex = /\!\[.*?\]\((.*?)\)/ + src_regex = /src=\"(.*?)\"/ + ss = content.to_s.scan(c_regex) + ss_src = content.to_s.scan(src_regex) + total_images = ss + ss_src + if total_images.length > 0 + total_images.each do |s| + image_title = /\"(.*?)\"/ + r_content = s[0] + remove_title = r_content.to_s.scan(image_title) + if remove_title.length > 0 + r_content = r_content.gsub(/#{remove_title[0]}/, "").strip + end + if r_content.include?("?") + new_r_content = r_content + "&raw=true" + else + new_r_content = r_content + "?raw=true" + end + unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") + new_r_content = "#{path}" + new_r_content + end + content = content.gsub(/#{r_content}/, new_r_content) + end + end + + return content + end end diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index fcd6f7be4..b246de32f 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -17,7 +17,7 @@ json.entries do content = if entry['name'] === 'README.md' content = Gitea::Repository::Entries::GetService.call(@project_owner, @project.identifier, entry['name'], ref: @ref)['content'] - render_decode64_content content + readme_render_decode64_content(content, @path) else entry['content'] end