FEATURE: Add support for Unicode usernames and group names

FEATURE: Add support for Unicode usernames and group names

Co-authored-by: Joffrey JAFFEUX j.jaffeux@gmail.com

diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6
index 5e997df..dbe6883 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6
@@ -44,7 +44,7 @@ function addHashtag(buffer, matches, state) {
 export function setup(helper) {
   helper.registerPlugin(md => {
     const rule = {
-      matcher: /#([\u00C0-\u1FFF\u2C00-\uD7FF\w-:]{1,101})/,
+      matcher: /#([\u00C0-\u1FFF\u2C00-\uD7FF\w:-]{1,101})/,
       onMatch: addHashtag
     };
 
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 d35500b..894fcc0 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
@@ -20,14 +20,44 @@ function addMention(buffer, matches, state) {
 export function setup(helper) {
   helper.registerOptions((opts, siteSettings) => {
     opts.features.mentions = !!siteSettings.enable_mentions;
+    opts.features.unicodeUsernames = !!siteSettings.unicode_usernames;
   });
 
   helper.registerPlugin(md => {
     const rule = {
-      matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/,
+      matcher: mentionRegex(md.options.discourse.features.unicodeUsernames),
       onMatch: addMention
     };
 
     md.core.textPostProcess.ruler.push("mentions", rule);
   });
 }
+
+function mentionRegex(unicodeUsernames) {
+  if (unicodeUsernames) {
+    try {
+      // Create the regex from a string, because Babel doesn't understand
+      // Unicode property escapes and completly mangles the regexp.
+      const alnum = "\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}";
+      return new RegExp(
+        `@([${alnum}_][${alnum}._-]{0,58}[${alnum}])|@([${alnum}_])`,
+        "u"
+      );
+    } catch (e) {
+      if (!(e instanceof SyntaxError)) throw e;
+
+      // Fallback for older browsers and MiniRacer.
+      // Created with regexpu-core@4.5.4 by executing the following in nodejs:
+      //
+      // const rewritePattern = require('regexpu-core')
+      // new RegExp(rewritePattern(/[\p{Alphabetic}\p{Mark}\p{Decimal_Number}]/u.source, 'u', { 'unicodePropertyEscape': true }))

[... diff too long, it was truncated ...]

GitHub sha: a7bc1ecb

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: