Skip to content
Snippets Groups Projects
Commit 7781bda9 authored by Douwe Maan's avatar Douwe Maan
Browse files

Move Markdown/reference logic from Gitlab::Markdown to Banzai

parent 9451db38
No related branches found
No related tags found
No related merge requests found
Showing
with 110 additions and 61 deletions
Loading
Loading
@@ -20,7 +20,7 @@ module GitlabMarkdownHelper
end
 
user = current_user if defined?(current_user)
gfm_body = Gitlab::Markdown.render(escaped_body, project: @project, current_user: user, pipeline: :single_line)
gfm_body = Banzai.render(escaped_body, project: @project, current_user: user, pipeline: :single_line)
 
fragment = Nokogiri::HTML::DocumentFragment.parse(gfm_body)
if fragment.children.size == 1 && fragment.children[0].name == 'a'
Loading
Loading
@@ -50,7 +50,7 @@ module GitlabMarkdownHelper
 
context[:project] ||= @project
 
html = Gitlab::Markdown.render(text, context)
html = Banzai.render(text, context)
 
context.merge!(
current_user: (current_user if defined?(current_user)),
Loading
Loading
@@ -61,7 +61,7 @@ module GitlabMarkdownHelper
ref: @ref
)
 
Gitlab::Markdown.post_process(html, context)
Banzai.post_process(html, context)
end
 
def asciidoc(text)
Loading
Loading
Loading
Loading
@@ -121,6 +121,6 @@ module IssuesHelper
end
end
 
# Required for Gitlab::Markdown::IssueReferenceFilter
# Required for Banzai::IssueReferenceFilter
module_function :url_for_issue
end
Loading
Loading
@@ -107,6 +107,6 @@ module LabelsHelper
options_from_collection_for_select(grouped_labels, 'name', 'title', params[:label_name])
end
 
# Required for Gitlab::Markdown::LabelReferenceFilter
# Required for Banzai::LabelReferenceFilter
module_function :render_colored_label, :text_color_for_bg, :escape_once
end
Loading
Loading
@@ -23,7 +23,7 @@ module Mentionable
 
included do
if self < Participable
participant ->(current_user) { mentioned_users(current_user, load_lazy_references: false) }
participant ->(current_user) { mentioned_users(current_user) }
end
end
 
Loading
Loading
@@ -43,9 +43,9 @@ module Mentionable
self
end
 
def all_references(current_user = self.author, text = nil, load_lazy_references: true)
ext = Gitlab::ReferenceExtractor.new(self.project, current_user, load_lazy_references: load_lazy_references)
def all_references(current_user = self.author, text = nil)
ext = Gitlab::ReferenceExtractor.new(self.project, current_user)
if text
ext.analyze(text)
else
Loading
Loading
@@ -59,13 +59,13 @@ module Mentionable
ext
end
 
def mentioned_users(current_user = nil, load_lazy_references: true)
all_references(current_user, load_lazy_references: load_lazy_references).users
def mentioned_users(current_user = nil)
all_references(current_user).users
end
 
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
def referenced_mentionables(current_user = self.author, text = nil, load_lazy_references: true)
refs = all_references(current_user, text, load_lazy_references: load_lazy_references)
def referenced_mentionables(current_user = self.author, text = nil)
refs = all_references(current_user, text)
refs = (refs.issues + refs.merge_requests + refs.commits)
 
# We're using this method instead of Array diffing because that requires
Loading
Loading
Loading
Loading
@@ -38,20 +38,21 @@ module Participable
# Be aware that this method makes a lot of sql queries.
# Save result into variable if you are going to reuse it inside same request
def participants(current_user = self.author, load_lazy_references: true)
participants = self.class.participant_attrs.flat_map do |attr|
value =
if attr.respond_to?(:call)
instance_exec(current_user, &attr)
else
send(attr)
end
participants =
Gitlab::ReferenceExtractor.lazily do
self.class.participant_attrs.flat_map do |attr|
value =
if attr.respond_to?(:call)
instance_exec(current_user, &attr)
else
send(attr)
end
 
participants_for(value, current_user)
end.compact.uniq
if load_lazy_references
participants = Gitlab::Markdown::ReferenceFilter::LazyReference.load(participants).uniq
participants_for(value, current_user)
end.compact.uniq
end
 
unless Gitlab::ReferenceExtractor.lazy?
participants.select! do |user|
user.can?(:read_project, project)
end
Loading
Loading
@@ -64,12 +65,12 @@ module Participable
 
def participants_for(value, current_user = nil)
case value
when User, Gitlab::Markdown::ReferenceFilter::LazyReference
when User, Banzai::LazyReference
[value]
when Enumerable, ActiveRecord::Relation
value.flat_map { |v| participants_for(v, current_user) }
when Participable
value.participants(current_user, load_lazy_references: false)
value.participants(current_user)
end
end
end
Loading
Loading
@@ -88,7 +88,7 @@ class Issue < ActiveRecord::Base
note.all_references(load_lazy_references: false).merge_requests
end.uniq
 
Gitlab::Markdown::ReferenceFilter::LazyReference.load(references).uniq.sort_by(&:iid)
Banzai::LazyReference.load(references).uniq.sort_by(&:iid)
end
 
# Reset issue events cache
Loading
Loading
Loading
Loading
@@ -373,11 +373,11 @@ class Note < ActiveRecord::Base
end
 
def contains_emoji_only?
note =~ /\A#{Gitlab::Markdown::EmojiFilter.emoji_pattern}\s?\Z/
note =~ /\A#{Banzai::EmojiFilter.emoji_pattern}\s?\Z/
end
 
def award_emoji_name
original_name = note.match(Gitlab::Markdown::EmojiFilter.emoji_pattern)[1]
original_name = note.match(Banzai::EmojiFilter.emoji_pattern)[1]
AwardEmoji.normilize_emoji_name(original_name)
end
end
module Banzai
def self.render(text, context = {})
Renderer.render(text, context)
end
def self.render_result(text, context = {})
Renderer.render_result(text, context)
end
def self.post_process(html, context)
Renderer.post_process(html, context)
end
end
require 'banzai'
module Banzai
# Common methods for ReferenceFilters that support an optional cross-project
# reference.
module CrossProjectReference
# Given a cross-project reference string, get the Project record
#
# Defaults to value of `context[:project]` if:
# * No reference is given OR
# * Reference given doesn't exist
#
# ref - String reference.
#
# Returns a Project, or nil if the reference can't be found
def project_from_ref(ref)
return context[:project] unless ref
Project.find_with_namespace(ref)
end
end
end
require 'active_support/core_ext/string/output_safety'
require 'banzai'
module Banzai
module Filter
def self.[](name)
const_get("#{name.to_s.camelize}Filter")
end
end
end
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# Issues, Merge Requests, Snippets, Commits and Commit Ranges share
# similar functionality in reference filtering.
class AbstractReferenceFilter < ReferenceFilter
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
require 'html/pipeline/filter'
require 'uri'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML Filter for auto-linking URLs in HTML.
#
# Based on HTML::Pipeline::AutolinkFilter
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces commit range references with links.
#
# This filter supports cross-project references.
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces commit references with links.
#
# This filter supports cross-project references.
Loading
Loading
require 'action_controller'
require 'gitlab/markdown'
require 'banzai'
require 'gitlab_emoji'
require 'html/pipeline/filter'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces :emoji: with images.
#
# Based on HTML::Pipeline::EmojiFilter
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces external issue tracker references with links.
# References are ignored if the project doesn't use an external issue
# tracker.
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
require 'html/pipeline/filter'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML Filter to add a `rel="nofollow"` attribute to external links
#
class ExternalLinkFilter < HTML::Pipeline::Filter
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces issue references with links. References to
# issues that do not exist are ignored.
#
Loading
Loading
require 'gitlab/markdown'
require 'banzai'
 
module Gitlab
module Markdown
module Banzai
module Filter
# HTML filter that replaces label references with links.
class LabelReferenceFilter < ReferenceFilter
# Public: Find label references in text
Loading
Loading
module Gitlab
module Markdown
require 'banzai'
require 'html/pipeline/filter'
module Banzai
module Filter
class MarkdownFilter < HTML::Pipeline::TextFilter
def initialize(text, context = nil, result = nil)
super text, context, result
Loading
Loading
@@ -11,8 +14,8 @@ module Gitlab
html.rstrip!
html
end
private
private
 
def self.redcarpet_options
# https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment