about summary refs log tree commit diff
path: root/app/javascript/mastodon/utils/hashtags.js
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2023-04-01 09:59:10 +0200
committerGitHub <noreply@github.com>2023-04-01 09:59:10 +0200
commit2b113764117c9ab98875141bcf1758ba8be58173 (patch)
tree8928280ea2321d85518b857e000d1016378e6246 /app/javascript/mastodon/utils/hashtags.js
parent46483ae849bc06ee74f4745f4564b213e742c51c (diff)
Change search pop-out in web UI (#24305)
Diffstat (limited to 'app/javascript/mastodon/utils/hashtags.js')
-rw-r--r--app/javascript/mastodon/utils/hashtags.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/javascript/mastodon/utils/hashtags.js b/app/javascript/mastodon/utils/hashtags.js
new file mode 100644
index 000000000..358ce37f5
--- /dev/null
+++ b/app/javascript/mastodon/utils/hashtags.js
@@ -0,0 +1,47 @@
+const HASHTAG_SEPARATORS = '_\\u00b7\\u200c';
+const ALPHA = '\\p{L}\\p{M}';
+const WORD = '\\p{L}\\p{M}\\p{N}\\p{Pc}';
+
+const buildHashtagPatternRegex = () => {
+  try {
+    return new RegExp(
+      '(?:^|[^\\/\\)\\w])#((' +
+      '[' + WORD + '_]' +
+      '[' + WORD + HASHTAG_SEPARATORS + ']*' +
+      '[' + ALPHA + HASHTAG_SEPARATORS + ']' +
+      '[' + WORD + HASHTAG_SEPARATORS +']*' +
+      '[' + WORD + '_]' +
+      ')|(' +
+      '[' + WORD + '_]*' +
+      '[' + ALPHA + ']' +
+      '[' + WORD + '_]*' +
+      '))', 'iu',
+    );
+  } catch {
+    return /(?:^|[^/)\w])#(\w*[a-zA-Z·]\w*)/i;
+  }
+};
+
+const buildHashtagRegex = () => {
+  try {
+    return new RegExp(
+      '^((' +
+      '[' + WORD + '_]' +
+      '[' + WORD + HASHTAG_SEPARATORS + ']*' +
+      '[' + ALPHA + HASHTAG_SEPARATORS + ']' +
+      '[' + WORD + HASHTAG_SEPARATORS +']*' +
+      '[' + WORD + '_]' +
+      ')|(' +
+      '[' + WORD + '_]*' +
+      '[' + ALPHA + ']' +
+      '[' + WORD + '_]*' +
+      '))$', 'iu',
+    );
+  } catch {
+    return /^(\w*[a-zA-Z·]\w*)$/i;
+  }
+};
+
+export const HASHTAG_PATTERN_REGEX = buildHashtagPatternRegex();
+
+export const HASHTAG_REGEX = buildHashtagRegex();