From f8164956dd05de8b71d405b2b4d097d71b839036 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 20 Nov 2017 16:28:03 -0500 Subject: [PATCH] Add quote and mention support for username formatters --- .../discourse/components/composer-editor.js.es6 | 6 +++++- .../javascripts/discourse/helpers/user-avatar.js.es6 | 8 +++++--- .../engines/discourse-markdown/mentions.js.es6 | 6 ++++-- .../engines/discourse-markdown/quotes.js.es6 | 12 +++++++++--- .../javascripts/pretty-text/pretty-text.js.es6 | 2 ++ lib/pretty_text.rb | 1 + lib/pretty_text/helpers.rb | 6 ++++++ lib/pretty_text/shims.js | 4 ++++ 8 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index 9a9b33aba0d..2e002c8a3b5 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -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; } diff --git a/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 b/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 index 75e60da098e..71e6e87249c 100644 --- a/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 +++ b/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 @@ -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 { diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 index 54a5284372b..7314bc1f98d 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 @@ -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; diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6 index 620d54135f8..3a0523bef66 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6 @@ -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); diff --git a/app/assets/javascripts/pretty-text/pretty-text.js.es6 b/app/assets/javascripts/pretty-text/pretty-text.js.es6 index 7d70cbf0d92..002f489d7db 100644 --- a/app/assets/javascripts/pretty-text/pretty-text.js.es6 +++ b/app/assets/javascripts/pretty-text/pretty-text.js.es6 @@ -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, diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 9b88f4eed24..7655c76c8d5 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -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; diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb index db1cfc3914f..dcb42d76d47 100644 --- a/lib/pretty_text/helpers.rb +++ b/lib/pretty_text/helpers.rb @@ -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) diff --git a/lib/pretty_text/shims.js b/lib/pretty_text/shims.js index 9b36f270ea2..aa95290e6cf 100644 --- a/lib/pretty_text/shims.js +++ b/lib/pretty_text/shims.js @@ -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); }