Class: Gitlab::GithubImport::MarkdownText

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper
Defined in:
lib/gitlab/github_import/markdown_text.rb

Constant Summary collapse

GITHUB_MEDIA_CDN =

On github.com we have base url for docs and CDN url for media. On github EE as far as we can know there is no CDN urls and media is placed on base url.

'https://user-images.githubusercontent.com'
ISSUE_REF_MATCHER =
'%{github_url}/%{import_source}/issues'
PULL_REF_MATCHER =
'%{github_url}/%{import_source}/pull'

Constants included from EncodingHelper

EncodingHelper::BOM_UTF8, EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_escaping!, #encode_utf8_with_replacement_character, #strip_bom, #unquote_path

Constructor Details

#initialize(text, author, exists = false) ⇒ MarkdownText

text - The Markdown text as a String. author - An instance of ‘Gitlab::GithubImport::Representation::User` exists - Boolean that indicates the user exists in the GitLab database.



63
64
65
66
67
# File 'lib/gitlab/github_import/markdown_text.rb', line 63

def initialize(text, author, exists = false)
  @text = text.to_s
  @author = author
  @exists = exists
end

Class Method Details

Links like ‘domain.github.com/<namespace>/<project>/pull/<iid>` needs to be converted



24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/github_import/markdown_text.rb', line 24

def convert_ref_links(text, project)
  matcher_options = { github_url: github_url, import_source: project.import_source }
  issue_ref_matcher = ISSUE_REF_MATCHER % matcher_options
  pull_ref_matcher = PULL_REF_MATCHER % matcher_options

  url_helpers = Rails.application.routes.url_helpers
  text.gsub(issue_ref_matcher, url_helpers.project_issues_url(project))
      .gsub(pull_ref_matcher, url_helpers.project_merge_requests_url(project))
end

.fetch_attachments(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/github_import/markdown_text.rb', line 42

def fetch_attachments(text)
  attachments = []
  doc = CommonMarker.render_doc(text)

  doc.walk do |node|
    attachment = extract_attachment(node)
    attachments << attachment if attachment
  end
  attachments
end

.formatObject



19
20
21
# File 'lib/gitlab/github_import/markdown_text.rb', line 19

def format(...)
  new(...).to_s
end

.github_urlObject

Returns github domain without slash in the end



35
36
37
38
39
40
# File 'lib/gitlab/github_import/markdown_text.rb', line 35

def github_url
  oauth_config = Gitlab::Auth::OAuth::Provider.config_for('github') || {}
  url = oauth_config['url'].presence || 'https://github.com'
  url = url.chop if url.end_with?('/')
  url
end

Instance Method Details

#to_sObject



69
70
71
72
# File 'lib/gitlab/github_import/markdown_text.rb', line 69

def to_s
  # Gitlab::EncodingHelper#clean remove `null` chars from the string
  clean(format)
end