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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -33,6 +33,12 @@ module PrettyText
user.primary_group.try(:name) || "" user.primary_group.try(:name) || ""
end 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) def mention_lookup(name)
return false if name.blank? return false if name.blank?
return "group" if Group.exists?(name: name) 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); return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
} }
function __formatUsername(u) {
return __helpers.format_username(u);
}
function __lookupPrimaryUserGroup(p) { function __lookupPrimaryUserGroup(p) {
return __helpers.lookup_primary_user_group(p); return __helpers.lookup_primary_user_group(p);
} }