2016-05-19 20:25:08 +08:00
|
|
|
require 'mini_racer'
|
2013-02-06 03:16:51 +08:00
|
|
|
require 'nokogiri'
|
2016-06-15 02:31:51 +08:00
|
|
|
require 'erb'
|
2015-01-30 05:53:48 +08:00
|
|
|
require_dependency 'url_helper'
|
2013-05-28 07:48:47 +08:00
|
|
|
require_dependency 'excerpt_parser'
|
2016-05-02 09:36:09 +08:00
|
|
|
require_dependency 'discourse_tagging'
|
2016-06-15 02:31:51 +08:00
|
|
|
require_dependency 'pretty_text/helpers'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
module PrettyText
|
2016-06-15 02:31:51 +08:00
|
|
|
@mutex = Mutex.new
|
|
|
|
@ctx_init = Mutex.new
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
def self.app_root
|
|
|
|
Rails.root
|
|
|
|
end
|
2013-07-16 15:48:48 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
def self.find_file(root, filename)
|
|
|
|
return filename if File.file?("#{root}#{filename}")
|
2015-03-13 03:51:28 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
es6_name = "#{filename}.js.es6"
|
|
|
|
return es6_name if File.file?("#{root}#{es6_name}")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
js_name = "#{filename}.js"
|
|
|
|
return js_name if File.file?("#{root}#{js_name}")
|
2015-09-25 11:35:14 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
erb_name = "#{filename}.js.es6.erb"
|
|
|
|
return erb_name if File.file?("#{root}#{erb_name}")
|
|
|
|
end
|
2015-12-28 14:28:16 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
def self.apply_es6_file(ctx, root_path, part_name)
|
|
|
|
filename = find_file(root_path, part_name)
|
|
|
|
if filename
|
|
|
|
source = File.read("#{root_path}#{filename}")
|
2016-04-26 03:55:15 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
if filename =~ /\.erb$/
|
|
|
|
source = ERB.new(source).result(binding)
|
|
|
|
end
|
2016-04-26 03:55:15 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
|
|
|
transpiled = template.module_transpile(source, "#{Rails.root}/app/assets/javascripts/", part_name)
|
|
|
|
ctx.eval(transpiled)
|
|
|
|
else
|
|
|
|
# Look for vendored stuff
|
|
|
|
vendor_root = "#{Rails.root}/vendor/assets/javascripts/"
|
|
|
|
filename = find_file(vendor_root, part_name)
|
|
|
|
if filename
|
|
|
|
ctx.eval(File.read("#{vendor_root}#{filename}"))
|
2016-04-26 03:55:15 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
def self.ctx_load_manifest(ctx, name)
|
|
|
|
manifest = File.read("#{Rails.root}/app/assets/javascripts/#{name}")
|
2016-06-15 02:31:51 +08:00
|
|
|
root_path = "#{Rails.root}/app/assets/javascripts/"
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
manifest.each_line do |l|
|
2016-08-25 19:45:29 +08:00
|
|
|
l = l.chomp
|
2016-06-15 02:31:51 +08:00
|
|
|
if l =~ /\/\/= require (\.\/)?(.*)$/
|
|
|
|
apply_es6_file(ctx, root_path, Regexp.last_match[2])
|
|
|
|
elsif l =~ /\/\/= require_tree (\.\/)?(.*)$/
|
|
|
|
path = Regexp.last_match[2]
|
2016-08-11 01:20:39 +08:00
|
|
|
Dir["#{root_path}/#{path}/**"].sort.each do |f|
|
2016-06-15 02:31:51 +08:00
|
|
|
apply_es6_file(ctx, root_path, f.sub(root_path, '')[1..-1].sub(/\.js.es6$/, ''))
|
|
|
|
end
|
|
|
|
end
|
2016-05-19 20:25:08 +08:00
|
|
|
end
|
2017-06-09 06:02:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_es6_context
|
|
|
|
ctx = MiniRacer::Context.new(timeout: 15000)
|
|
|
|
|
|
|
|
ctx.eval("window = {}; window.devicePixelRatio = 2;") # hack to make code think stuff is retina
|
|
|
|
|
|
|
|
if Rails.env.development? || Rails.env.test?
|
|
|
|
ctx.attach("console.log", proc { |l| p l })
|
2017-06-24 03:24:11 +08:00
|
|
|
ctx.eval('window.console = console;')
|
2017-06-09 06:02:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
ctx_load(ctx, "#{Rails.root}/app/assets/javascripts/discourse-loader.js")
|
|
|
|
ctx_load(ctx, "vendor/assets/javascripts/lodash.js")
|
|
|
|
ctx_load_manifest(ctx, "pretty-text-bundle.js")
|
2017-07-14 20:27:28 +08:00
|
|
|
ctx_load_manifest(ctx, "markdown-it-bundle.js")
|
2017-06-09 06:02:30 +08:00
|
|
|
root_path = "#{Rails.root}/app/assets/javascripts/"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-05-05 17:21:07 +08:00
|
|
|
apply_es6_file(ctx, root_path, "discourse/helpers/parse-html")
|
|
|
|
apply_es6_file(ctx, root_path, "discourse/lib/to-markdown")
|
2016-06-15 02:31:51 +08:00
|
|
|
apply_es6_file(ctx, root_path, "discourse/lib/utilities")
|
|
|
|
|
|
|
|
PrettyText::Helpers.instance_methods.each do |method|
|
|
|
|
ctx.attach("__helpers.#{method}", PrettyText::Helpers.method(method))
|
2013-08-09 06:14:12 +08:00
|
|
|
end
|
2016-06-15 02:31:51 +08:00
|
|
|
ctx.load("#{Rails.root}/lib/pretty_text/shims.js")
|
|
|
|
ctx.eval("__setUnicode(#{Emoji.unicode_replacements_json})")
|
2013-08-09 06:14:12 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
to_load = []
|
|
|
|
DiscoursePluginRegistry.each_globbed_asset do |a|
|
|
|
|
to_load << a if File.file?(a) && a =~ /discourse-markdown/
|
|
|
|
end
|
|
|
|
to_load.uniq.each do |f|
|
|
|
|
if f =~ /^.+assets\/javascripts\//
|
|
|
|
root = Regexp.last_match[0]
|
|
|
|
apply_es6_file(ctx, root, f.sub(root, '').sub(/\.js\.es6$/, ''))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-10 14:37:16 +08:00
|
|
|
DiscoursePluginRegistry.vendored_core_pretty_text.each do |vpt|
|
|
|
|
ctx.eval(File.read(vpt))
|
|
|
|
end
|
|
|
|
|
2017-04-19 05:49:56 +08:00
|
|
|
DiscoursePluginRegistry.vendored_pretty_text.each do |vpt|
|
|
|
|
ctx.eval(File.read(vpt))
|
|
|
|
end
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.v8
|
|
|
|
return @ctx if @ctx
|
|
|
|
|
|
|
|
# ensure we only init one of these
|
|
|
|
@ctx_init.synchronize do
|
|
|
|
return @ctx if @ctx
|
2016-06-15 02:31:51 +08:00
|
|
|
@ctx = create_es6_context
|
2013-08-16 06:12:10 +08:00
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
@ctx
|
|
|
|
end
|
|
|
|
|
2014-11-14 14:51:04 +08:00
|
|
|
def self.reset_context
|
|
|
|
@ctx_init.synchronize do
|
2017-07-20 12:17:45 +08:00
|
|
|
@ctx&.dispose
|
2014-11-14 14:51:04 +08:00
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.markdown(text, opts = {})
|
2013-02-06 03:16:51 +08:00
|
|
|
# we use the exact same markdown converter as the client
|
2013-02-26 00:42:20 +08:00
|
|
|
# TODO: use the same extensions on both client and server (in particular the template for mentions)
|
2013-02-06 03:16:51 +08:00
|
|
|
baked = nil
|
2016-05-19 20:25:08 +08:00
|
|
|
text = text || ""
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-04 08:12:53 +08:00
|
|
|
protect do
|
2013-08-16 11:03:47 +08:00
|
|
|
context = v8
|
2013-10-12 04:24:27 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
custom_emoji = {}
|
2016-11-18 02:35:39 +08:00
|
|
|
Emoji.custom.map { |e| custom_emoji[e.name] = e.url }
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
buffer = <<~JS
|
|
|
|
__optInput = {};
|
|
|
|
__optInput.siteSettings = #{SiteSetting.client_settings_json};
|
2018-05-23 22:47:09 +08:00
|
|
|
__paths = #{paths_json};
|
2017-06-09 06:02:30 +08:00
|
|
|
__optInput.getURL = __getURL;
|
|
|
|
__optInput.getCurrentUser = __getCurrentUser;
|
|
|
|
__optInput.lookupAvatar = __lookupAvatar;
|
2017-11-03 21:51:40 +08:00
|
|
|
__optInput.lookupPrimaryUserGroup = __lookupPrimaryUserGroup;
|
2017-11-21 05:28:03 +08:00
|
|
|
__optInput.formatUsername = __formatUsername;
|
2017-06-09 06:02:30 +08:00
|
|
|
__optInput.getTopicInfo = __getTopicInfo;
|
|
|
|
__optInput.categoryHashtagLookup = __categoryLookup;
|
|
|
|
__optInput.customEmoji = #{custom_emoji.to_json};
|
2017-06-29 01:47:22 +08:00
|
|
|
__optInput.emojiUnicodeReplacer = __emojiUnicodeReplacer;
|
2017-08-22 23:46:15 +08:00
|
|
|
__optInput.lookupImageUrls = __lookupImageUrls;
|
2017-06-29 04:56:44 +08:00
|
|
|
__optInput.censoredWords = #{WordWatcher.words_for_action(:censor).join('|').to_json};
|
2017-06-09 06:02:30 +08:00
|
|
|
JS
|
|
|
|
|
|
|
|
if opts[:topicId]
|
|
|
|
buffer << "__optInput.topicId = #{opts[:topicId].to_i};\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:user_id]
|
|
|
|
buffer << "__optInput.userId = #{opts[:user_id].to_i};\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
buffer << "__textOptions = __buildOptions(__optInput);\n"
|
2017-07-14 20:27:28 +08:00
|
|
|
buffer << ("__pt = new __PrettyText(__textOptions);")
|
|
|
|
|
2016-08-12 02:59:20 +08:00
|
|
|
# Be careful disabling sanitization. We allow for custom emails
|
|
|
|
if opts[:sanitize] == false
|
2017-07-14 20:27:28 +08:00
|
|
|
buffer << ('__pt.disableSanitizer();')
|
2016-08-12 02:59:20 +08:00
|
|
|
end
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
opts = context.eval(buffer)
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2016-01-29 22:59:15 +08:00
|
|
|
DiscourseEvent.trigger(:markdown_context, context)
|
2016-06-15 02:31:51 +08:00
|
|
|
baked = context.eval("__pt.cook(#{text.inspect})")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
# if baked.blank? && !(opts || {})[:skip_blank_test]
|
|
|
|
# # we may have a js engine issue
|
|
|
|
# test = markdown("a", skip_blank_test: true)
|
|
|
|
# if test.blank?
|
|
|
|
# Rails.logger.warn("Markdown engine appears to have crashed, resetting context")
|
|
|
|
# reset_context
|
|
|
|
# opts ||= {}
|
|
|
|
# opts = opts.dup
|
|
|
|
# opts[:skip_blank_test] = true
|
|
|
|
# baked = markdown(text, opts)
|
|
|
|
# end
|
|
|
|
# end
|
2014-11-14 14:51:04 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
baked
|
|
|
|
end
|
|
|
|
|
2018-05-23 22:47:09 +08:00
|
|
|
def self.paths_json
|
|
|
|
paths = {
|
|
|
|
baseUri: Discourse::base_uri,
|
|
|
|
CDN: Rails.configuration.action_controller.asset_host,
|
|
|
|
}
|
|
|
|
|
|
|
|
if SiteSetting.Upload.enable_s3_uploads
|
|
|
|
if SiteSetting.Upload.s3_cdn_url.present?
|
|
|
|
paths[:S3CDN] = SiteSetting.Upload.s3_cdn_url
|
|
|
|
end
|
|
|
|
paths[:S3BaseUrl] = Discourse.store.absolute_base_url
|
|
|
|
end
|
|
|
|
|
|
|
|
paths.to_json
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# leaving this here, cause it invokes v8, don't want to implement twice
|
2013-08-14 04:08:29 +08:00
|
|
|
def self.avatar_img(avatar_template, size)
|
2014-02-04 08:12:53 +08:00
|
|
|
protect do
|
2018-05-23 22:47:09 +08:00
|
|
|
v8.eval(<<~JS)
|
|
|
|
__paths = #{paths_json};
|
|
|
|
__utils.avatarImg({size: #{size.inspect}, avatarTemplate: #{avatar_template.inspect}}, __getURL);
|
|
|
|
JS
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-15 15:59:29 +08:00
|
|
|
def self.unescape_emoji(title)
|
2016-06-15 02:31:51 +08:00
|
|
|
return title unless SiteSetting.enable_emoji?
|
|
|
|
|
|
|
|
set = SiteSetting.emoji_set.inspect
|
2015-10-15 15:59:29 +08:00
|
|
|
protect do
|
2018-05-23 22:47:09 +08:00
|
|
|
v8.eval(<<~JS)
|
|
|
|
__paths = #{paths_json};
|
|
|
|
__performEmojiUnescape(#{title.inspect}, { getURL: __getURL, emojiSet: #{set} });
|
|
|
|
JS
|
2015-10-15 15:59:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.cook(text, opts = {})
|
2015-04-24 01:33:29 +08:00
|
|
|
options = opts.dup
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# we have a minor inconsistency
|
2015-04-24 01:33:29 +08:00
|
|
|
options[:topicId] = opts[:topic_id]
|
|
|
|
|
2015-12-31 03:35:25 +08:00
|
|
|
working_text = text.dup
|
2016-11-09 05:36:34 +08:00
|
|
|
|
2018-02-27 04:48:59 +08:00
|
|
|
sanitized = markdown(working_text, options)
|
2015-05-26 09:13:12 +08:00
|
|
|
|
|
|
|
doc = Nokogiri::HTML.fragment(sanitized)
|
|
|
|
|
|
|
|
if !options[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content
|
|
|
|
add_rel_nofollow_to_user_content(doc)
|
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
if SiteSetting.Upload.enable_s3_uploads && SiteSetting.Upload.s3_cdn_url.present?
|
2015-05-26 09:13:12 +08:00
|
|
|
add_s3_cdn(doc)
|
|
|
|
end
|
|
|
|
|
2018-11-22 16:01:03 +08:00
|
|
|
if SiteSetting.enable_mentions
|
|
|
|
add_mentions(doc, user_id: opts[:user_id])
|
|
|
|
end
|
2018-11-22 14:28:48 +08:00
|
|
|
|
2015-05-26 09:13:12 +08:00
|
|
|
doc.to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_s3_cdn(doc)
|
|
|
|
doc.css("img").each do |img|
|
2015-06-10 16:56:44 +08:00
|
|
|
next unless img["src"]
|
2016-06-30 22:55:01 +08:00
|
|
|
img["src"] = Discourse.store.cdn_url(img["src"])
|
2015-05-26 09:13:12 +08:00
|
|
|
end
|
2013-02-11 08:43:07 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2015-05-26 09:13:12 +08:00
|
|
|
def self.add_rel_nofollow_to_user_content(doc)
|
2013-02-11 15:58:19 +08:00
|
|
|
whitelist = []
|
2013-02-11 16:01:33 +08:00
|
|
|
|
2013-11-20 11:38:21 +08:00
|
|
|
domains = SiteSetting.exclude_rel_nofollow_domains
|
2014-03-30 07:50:44 +08:00
|
|
|
whitelist = domains.split('|') if domains.present?
|
2013-02-11 16:01:33 +08:00
|
|
|
|
2013-02-11 08:43:07 +08:00
|
|
|
site_uri = nil
|
|
|
|
doc.css("a").each do |l|
|
|
|
|
href = l["href"].to_s
|
2013-02-26 00:42:20 +08:00
|
|
|
begin
|
2013-02-11 08:43:07 +08:00
|
|
|
uri = URI(href)
|
|
|
|
site_uri ||= URI(Discourse.base_url)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
if !uri.host.present? ||
|
2015-05-27 12:31:01 +08:00
|
|
|
uri.host == site_uri.host ||
|
|
|
|
uri.host.ends_with?("." << site_uri.host) ||
|
2017-07-28 09:20:09 +08:00
|
|
|
whitelist.any? { |u| uri.host == u || uri.host.ends_with?("." << u) }
|
2013-02-11 08:43:07 +08:00
|
|
|
# we are good no need for nofollow
|
2018-03-26 18:24:39 +08:00
|
|
|
l.remove_attribute("rel")
|
2013-02-11 08:43:07 +08:00
|
|
|
else
|
2016-11-20 20:49:14 +08:00
|
|
|
l["rel"] = "nofollow noopener"
|
2013-02-11 08:43:07 +08:00
|
|
|
end
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2013-02-26 00:42:20 +08:00
|
|
|
# add a nofollow anyway
|
2016-11-20 20:49:14 +08:00
|
|
|
l["rel"] = "nofollow noopener"
|
2013-02-11 08:43:07 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-02-06 21:45:04 +08:00
|
|
|
class DetectedLink < Struct.new(:url, :is_quote); end
|
2014-07-11 12:17:01 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.extract_links(html)
|
|
|
|
links = []
|
2013-06-06 02:53:07 +08:00
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
2017-02-06 21:45:04 +08:00
|
|
|
|
2016-03-17 05:35:08 +08:00
|
|
|
# remove href inside quotes & elided part
|
2017-02-06 21:45:04 +08:00
|
|
|
doc.css("aside.quote a, .elided a").each { |a| a["href"] = "" }
|
2014-07-11 12:17:01 +08:00
|
|
|
|
2017-02-06 21:45:04 +08:00
|
|
|
# extract all links
|
|
|
|
doc.css("a").each do |a|
|
|
|
|
if a["href"].present? && a["href"][0] != "#".freeze
|
|
|
|
links << DetectedLink.new(a["href"], false)
|
2014-07-11 12:17:01 +08:00
|
|
|
end
|
2017-02-06 21:45:04 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2017-02-06 21:45:04 +08:00
|
|
|
# extract quotes
|
|
|
|
doc.css("aside.quote[data-topic]").each do |aside|
|
|
|
|
if aside["data-topic"].present?
|
|
|
|
url = "/t/topic/#{aside["data-topic"]}"
|
|
|
|
url << "/#{aside["data-post"]}" if aside["data-post"].present?
|
|
|
|
links << DetectedLink.new(url, true)
|
2013-02-14 04:22:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-06 21:45:04 +08:00
|
|
|
# extract Youtube links
|
|
|
|
doc.css("div[data-youtube-id]").each do |div|
|
|
|
|
if div["data-youtube-id"].present?
|
|
|
|
links << DetectedLink.new("https://www.youtube.com/watch?v=#{div['data-youtube-id']}", false)
|
|
|
|
end
|
2016-09-23 04:50:05 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
links
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.excerpt(html, max_length, options = {})
|
2014-11-06 03:37:00 +08:00
|
|
|
# TODO: properly fix this HACK in ExcerptParser without introducing XSS
|
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
strip_image_wrapping(doc)
|
|
|
|
html = doc.to_html
|
|
|
|
|
2013-05-28 07:48:47 +08:00
|
|
|
ExcerptParser.get_excerpt(html, max_length, options)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 03:28:10 +08:00
|
|
|
def self.strip_links(string)
|
|
|
|
return string if string.blank?
|
|
|
|
|
|
|
|
# If the user is not basic, strip links from their bio
|
|
|
|
fragment = Nokogiri::HTML.fragment(string)
|
2017-07-28 09:20:09 +08:00
|
|
|
fragment.css('a').each { |a| a.replace(a.inner_html) }
|
2013-06-06 03:28:10 +08:00
|
|
|
fragment.to_html
|
|
|
|
end
|
|
|
|
|
2018-05-10 01:24:44 +08:00
|
|
|
def self.make_all_links_absolute(doc)
|
|
|
|
site_uri = nil
|
|
|
|
doc.css("a").each do |link|
|
|
|
|
href = link["href"].to_s
|
|
|
|
begin
|
|
|
|
uri = URI(href)
|
|
|
|
site_uri ||= URI(Discourse.base_url)
|
2018-06-09 01:11:52 +08:00
|
|
|
unless uri.host.present? || href.start_with?('mailto')
|
|
|
|
link["href"] = "#{site_uri}#{link['href']}"
|
2018-06-09 01:56:20 +08:00
|
|
|
end
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2018-05-10 01:24:44 +08:00
|
|
|
# leave it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-21 23:12:30 +08:00
|
|
|
|
2014-04-18 00:32:51 +08:00
|
|
|
def self.strip_image_wrapping(doc)
|
|
|
|
doc.css(".lightbox-wrapper .meta").remove
|
|
|
|
end
|
|
|
|
|
2018-05-10 01:24:44 +08:00
|
|
|
def self.convert_vimeo_iframes(doc)
|
|
|
|
doc.css("iframe[src*='player.vimeo.com']").each do |iframe|
|
|
|
|
vimeo_id = iframe['src'].split('/').last
|
|
|
|
iframe.replace "<p><a href='https://vimeo.com/#{vimeo_id}'>https://vimeo.com/#{vimeo_id}</a></p>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-21 23:12:30 +08:00
|
|
|
def self.format_for_email(html, post = nil)
|
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
DiscourseEvent.trigger(:reduce_cooked, doc, post)
|
|
|
|
strip_image_wrapping(doc)
|
2018-05-10 01:24:44 +08:00
|
|
|
convert_vimeo_iframes(doc)
|
2016-06-21 23:12:30 +08:00
|
|
|
make_all_links_absolute(doc)
|
|
|
|
doc.to_html
|
2013-11-29 04:57:21 +08:00
|
|
|
end
|
|
|
|
|
2013-05-28 07:48:47 +08:00
|
|
|
protected
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-04 08:12:53 +08:00
|
|
|
class JavaScriptError < StandardError
|
|
|
|
attr_accessor :message, :backtrace
|
|
|
|
|
|
|
|
def initialize(message, backtrace)
|
|
|
|
@message = message
|
|
|
|
@backtrace = backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.protect
|
|
|
|
rval = nil
|
|
|
|
@mutex.synchronize do
|
2016-05-19 20:25:08 +08:00
|
|
|
rval = yield
|
2014-02-04 08:12:53 +08:00
|
|
|
end
|
|
|
|
rval
|
|
|
|
end
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
def self.ctx_load(ctx, *files)
|
2013-05-28 07:48:47 +08:00
|
|
|
files.each do |file|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx.load(app_root + file)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 14:28:48 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
USER_TYPE ||= 'user'
|
|
|
|
GROUP_TYPE ||= 'group'
|
|
|
|
|
2018-11-22 16:01:03 +08:00
|
|
|
def self.add_mentions(doc, user_id: nil)
|
2018-11-22 14:28:48 +08:00
|
|
|
elements = doc.css("span.mention")
|
2018-11-23 08:31:52 +08:00
|
|
|
names = elements.map { |element| element.text[1..-1] }
|
2018-11-22 14:28:48 +08:00
|
|
|
|
2018-11-22 16:01:03 +08:00
|
|
|
mentions = lookup_mentions(names, user_id: user_id)
|
2018-11-22 14:28:48 +08:00
|
|
|
|
|
|
|
doc.css("span.mention").each do |element|
|
|
|
|
name = element.text[1..-1]
|
|
|
|
name.downcase!
|
|
|
|
|
|
|
|
if type = mentions[name]
|
|
|
|
element.name = 'a'
|
|
|
|
|
|
|
|
element.children = PrettyText::Helpers.format_username(
|
|
|
|
element.children.text
|
|
|
|
)
|
|
|
|
|
|
|
|
case type
|
|
|
|
when USER_TYPE
|
2019-01-30 10:54:29 +08:00
|
|
|
element['href'] = "#{Discourse::base_uri}/u/#{name}"
|
2018-11-22 14:28:48 +08:00
|
|
|
when GROUP_TYPE
|
|
|
|
element['class'] = 'mention-group'
|
2019-01-30 10:54:29 +08:00
|
|
|
element['href'] = "#{Discourse::base_uri}/groups/#{name}"
|
2018-11-22 14:28:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 16:01:03 +08:00
|
|
|
def self.lookup_mentions(names, user_id: nil)
|
2018-11-22 16:42:56 +08:00
|
|
|
return {} if names.blank?
|
|
|
|
|
2018-11-22 14:28:48 +08:00
|
|
|
sql = <<~SQL
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
:user_type AS type,
|
|
|
|
username_lower AS name
|
|
|
|
FROM users
|
2018-11-22 15:00:46 +08:00
|
|
|
WHERE username_lower IN (:names) AND staged = false
|
2018-11-22 14:28:48 +08:00
|
|
|
)
|
|
|
|
UNION
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
:group_type AS type,
|
2018-11-26 23:34:56 +08:00
|
|
|
lower(name) AS name
|
2018-11-22 14:28:48 +08:00
|
|
|
FROM groups
|
2018-11-26 23:34:56 +08:00
|
|
|
WHERE lower(name) IN (:names) AND (#{Group.mentionable_sql_clause})
|
2018-11-22 14:28:48 +08:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2018-11-22 16:01:03 +08:00
|
|
|
user = User.find_by(id: user_id)
|
2018-11-22 16:32:56 +08:00
|
|
|
names.each(&:downcase!)
|
2018-11-22 16:01:03 +08:00
|
|
|
|
2018-11-22 14:28:48 +08:00
|
|
|
results = DB.query(sql,
|
|
|
|
names: names,
|
|
|
|
user_type: USER_TYPE,
|
2018-11-22 16:01:03 +08:00
|
|
|
group_type: GROUP_TYPE,
|
|
|
|
levels: Group.alias_levels(user),
|
|
|
|
user_id: user_id
|
2018-11-22 14:28:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
mentions = {}
|
|
|
|
results.each { |result| mentions[result.name] = result.type }
|
|
|
|
mentions
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|