Add quote and mention support for username formatters

This commit is contained in:
Robin Ward 2017-11-20 16:28:03 -05:00
parent c53ed61bd3
commit f8164956dd
8 changed files with 36 additions and 9 deletions

View File

@ -12,7 +12,9 @@ import { findRawTemplate } from 'discourse/lib/raw-templates';
import { tinyAvatar,
displayErrorForUpload,
getUploadMarkdown,
validateUploadedFiles } from 'discourse/lib/utilities';
validateUploadedFiles,
formatUsername
} from 'discourse/lib/utilities';
import { cacheShortUploadUrl, resolveAllShortUrls } from 'pretty-text/image-short-url';
export default Ember.Component.extend({
@ -31,6 +33,8 @@ export default Ember.Component.extend({
return {
previewing: true,
formatUsername,
lookupAvatarByPostNumber: (postNumber, topicId) => {
const topic = this.get('topic');
if (!topic) { return; }

View File

@ -1,5 +1,5 @@
import { registerUnbound } from 'discourse-common/lib/helpers';
import { avatarImg } from 'discourse/lib/utilities';
import { avatarImg, formatUsername } from 'discourse/lib/utilities';
function renderAvatar(user, options) {
options = options || {};
@ -11,6 +11,8 @@ function renderAvatar(user, options) {
if (!username || !avatarTemplate) { return ''; }
let formattedUsername = formatUsername(username);
let title = options.title;
if (!title && !options.ignoreTitle) {
// first try to get a title
@ -22,7 +24,7 @@ function renderAvatar(user, options) {
// if a description has been provided
if (description && description.length > 0) {
// preprend the username before the description
title = username + " - " + description;
title = formattedUsername + " - " + description;
}
}
}
@ -30,7 +32,7 @@ function renderAvatar(user, options) {
return avatarImg({
size: options.imageSize,
extraClasses: Em.get(user, 'extras') || options.extraClasses,
title: title || username,
title: title || formattedUsername,
avatarTemplate: avatarTemplate
});
} else {

View File

@ -1,7 +1,6 @@
function addMention(buffer, matches, state) {
let username = matches[1] || matches[2];
let mentionLookup = state.md.options.discourse.mentionLookup;
let getURL = state.md.options.discourse.getURL;
let { getURL, mentionLookup, formatUsername } = state.md.options.discourse;
let type = mentionLookup && mentionLookup(username);
@ -25,6 +24,9 @@ function addMention(buffer, matches, state) {
}
buffer.push(token);
if (formatUsername) {
username = formatUsername(username);
}
token = new state.Token('text', '', 0);
token.content = '@'+username;

View File

@ -3,13 +3,13 @@ import { performEmojiUnescape } from 'pretty-text/emoji';
const rule = {
tag: 'quote',
before: function(state, tagInfo) {
before(state, tagInfo) {
const attrs = tagInfo.attrs;
let options = state.md.options.discourse;
let quoteInfo = attrs['_default'];
let username, postNumber, topicId, avatarImg, primaryGroupName, full;
let username, postNumber, topicId, avatarImg, primaryGroupName, full, displayName;
if (quoteInfo) {
let split = quoteInfo.split(/\,\s*/);
@ -50,6 +50,12 @@ const rule = {
primaryGroupName = options.lookupPrimaryUserGroup(username);
}
if (options.formatUsername) {
displayName = options.formatUsername(username);
} else {
displayName = username;
}
let token = state.push('bbcode_open', 'aside', 1);
token.attrs = [];
@ -118,7 +124,7 @@ const rule = {
}
} else {
token = state.push('text', '', 0);
token.content = ` ${username}:`;
token.content = ` ${displayName}:`;
}
token = state.push('quote_header_close', 'div', -1);

View File

@ -21,6 +21,7 @@ export function buildOptions(state) {
currentUser,
lookupAvatarByPostNumber,
lookupPrimaryUserGroupByPostNumber,
formatUsername,
emojiUnicodeReplacer,
lookupInlineOnebox,
lookupImageUrls,
@ -61,6 +62,7 @@ export function buildOptions(state) {
currentUser,
lookupAvatarByPostNumber,
lookupPrimaryUserGroupByPostNumber,
formatUsername,
mentionLookup,
emojiUnicodeReplacer,
lookupInlineOnebox,

View File

@ -159,6 +159,7 @@ module PrettyText
__optInput.getCurrentUser = __getCurrentUser;
__optInput.lookupAvatar = __lookupAvatar;
__optInput.lookupPrimaryUserGroup = __lookupPrimaryUserGroup;
__optInput.formatUsername = __formatUsername;
__optInput.getTopicInfo = __getTopicInfo;
__optInput.categoryHashtagLookup = __categoryLookup;
__optInput.mentionLookup = __mentionLookup;

View File

@ -33,6 +33,12 @@ module PrettyText
user.primary_group.try(:name) || ""
end
# Overwrite this in a plugin to change how markdown can format
# usernames on the server side
def format_username(username)
username
end
def mention_lookup(name)
return false if name.blank?
return "group" if Group.exists?(name: name)

View File

@ -73,6 +73,10 @@ function __lookupAvatar(p) {
return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
}
function __formatUsername(u) {
return __helpers.format_username(u);
}
function __lookupPrimaryUserGroup(p) {
return __helpers.lookup_primary_user_group(p);
}