diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-04 00:09:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-04 00:09:24 +0300 |
commit | 9432ed811233643af1cb5ad78e2cd9755ed989a9 (patch) | |
tree | a6f4d254ebd818a58017b3bfb0911f4aae169fcf /app/presenters/blob_presenter.rb | |
parent | 44434461b3c58336624125b5ab002e7200e4a208 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/presenters/blob_presenter.rb')
-rw-r--r-- | app/presenters/blob_presenter.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/app/presenters/blob_presenter.rb b/app/presenters/blob_presenter.rb index 74ac47fa439..92dcfeed104 100644 --- a/app/presenters/blob_presenter.rb +++ b/app/presenters/blob_presenter.rb @@ -9,6 +9,19 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated presents ::Blob, as: :blob + def highlight_and_trim(ellipsis_svg:, trim_length:, plain: nil) + load_all_blob_data + + trimmed_lines, trimmed_idx = trimmed_blob_data(trim_length) + Gitlab::Highlight.highlight( + blob.path, + trimmed_lines, + language: blob_language, + plain: plain, + context: { ellipsis_indexes: trimmed_idx, ellipsis_svg: ellipsis_svg } + ) + end + def highlight(to: nil, plain: nil) load_all_blob_data @@ -26,6 +39,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated highlight(plain: false) end + def trimmed_blob_data(trim_length) + @_trimmed_blob_data ||= limited_trimmed_blob_data(trim_length) + end + def blob_data(to) @_blob_data ||= limited_blob_data(to: to) end @@ -169,6 +186,19 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated blob.load_all_data! if blob.respond_to?(:load_all_data!) end + def limited_trimmed_blob_data(trim_length) + trimmed_idx = [] + + trimmed_lines = all_lines.map.with_index do |line, index| + next line if line.length <= trim_length + + trimmed_idx << index + "#{line[0, trim_length]}\n" + end + + [trimmed_lines.join, trimmed_idx] + end + def limited_blob_data(to: nil) return blob.data if to.blank? |