mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
REFACTOR: Migrate markdown functionality in ES6
This commit is contained in:
73
lib/pretty_text/helpers.rb
Normal file
73
lib/pretty_text/helpers.rb
Normal file
@@ -0,0 +1,73 @@
|
||||
module PrettyText
|
||||
module Helpers
|
||||
extend self
|
||||
|
||||
# functions here are available to v8
|
||||
def t(key, opts)
|
||||
key = "js." + key
|
||||
unless opts
|
||||
I18n.t(key)
|
||||
else
|
||||
str = I18n.t(key, Hash[opts.entries].symbolize_keys).dup
|
||||
opts.each { |k,v| str.gsub!("{{#{k.to_s}}}", v.to_s) }
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
def avatar_template(username)
|
||||
return "" unless username
|
||||
user = User.find_by(username_lower: username.downcase)
|
||||
return "" unless user.present?
|
||||
|
||||
# TODO: Add support for ES6 and call `avatar-template` directly
|
||||
if !user.uploaded_avatar_id
|
||||
avatar_template = User.default_template(username)
|
||||
else
|
||||
avatar_template = user.avatar_template
|
||||
end
|
||||
|
||||
UrlHelper.schemaless UrlHelper.absolute avatar_template
|
||||
end
|
||||
|
||||
def mention_lookup(name)
|
||||
return false if name.blank?
|
||||
return "group" if Group.where(name: name).exists?
|
||||
return "user" if User.where(username_lower: name.downcase).exists?
|
||||
nil
|
||||
end
|
||||
|
||||
def category_hashtag_lookup(category_slug)
|
||||
if category = Category.query_from_hashtag_slug(category_slug)
|
||||
[category.url_with_id, category_slug]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_topic_info(topic_id)
|
||||
return unless Fixnum === topic_id
|
||||
# TODO this only handles public topics, secured one do not get this
|
||||
topic = Topic.find_by(id: topic_id)
|
||||
if topic && Guardian.new.can_see?(topic)
|
||||
{
|
||||
title: topic.title,
|
||||
href: topic.url
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def category_tag_hashtag_lookup(text)
|
||||
tag_postfix = '::tag'
|
||||
is_tag = text =~ /#{tag_postfix}$/
|
||||
|
||||
if !is_tag && category = Category.query_from_hashtag_slug(text)
|
||||
[category.url_with_id, text]
|
||||
elsif is_tag && tag = Tag.find_by_name(text.gsub!("#{tag_postfix}", ''))
|
||||
["#{Discourse.base_url}/tags/#{tag.name}", text]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
51
lib/pretty_text/shims.js
Normal file
51
lib/pretty_text/shims.js
Normal file
@@ -0,0 +1,51 @@
|
||||
__PrettyText = require('pretty-text/pretty-text').default;
|
||||
__buildOptions = require('pretty-text/pretty-text').buildOptions;
|
||||
__performEmojiUnescape = require('pretty-text/emoji').performEmojiUnescape;
|
||||
|
||||
__utils = require('discourse/lib/utilities');
|
||||
__setUnicode = require('pretty-text/engines/discourse-markdown/emoji').setUnicodeReplacements;
|
||||
|
||||
__paths = {};
|
||||
|
||||
function __getURLNoCDN(url) {
|
||||
if (!url) return url;
|
||||
|
||||
// if it's a non relative URL, return it.
|
||||
if (url !== '/' && !/^\/[^\/]/.test(url)) { return url; }
|
||||
|
||||
if (url.indexOf(__paths.baseUri) !== -1) { return url; }
|
||||
if (url[0] !== "/") url = "/" + url;
|
||||
|
||||
return __paths.baseUri + url;
|
||||
}
|
||||
|
||||
function __getURL(url) {
|
||||
url = __getURLNoCDN(url);
|
||||
// only relative urls
|
||||
if (__paths.CDN && /^\\\/[^\\\/]/.test(url)) {
|
||||
url = __paths.CDN + url;
|
||||
} else if (__paths.S3CDN) {
|
||||
url = url.replace(__paths.S3BaseUrl, __paths.S3CDN);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function __getTopicInfo(i) {
|
||||
return __helpers.get_topic_info(i);
|
||||
}
|
||||
|
||||
function __categoryLookup(c) {
|
||||
return __helpers.category_tag_hashtag_lookup(c);
|
||||
}
|
||||
|
||||
function __mentionLookup(u) {
|
||||
return __helpers.mention_lookup(u);
|
||||
}
|
||||
|
||||
function __lookupAvatar(p) {
|
||||
return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
|
||||
}
|
||||
|
||||
I18n = {
|
||||
t: function(a,b) { return __helpers.t(a,b); }
|
||||
};
|
||||
Reference in New Issue
Block a user