about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/compose/util
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-10-11 11:51:15 +0200
committerClaire <claire.github-309c@sitedethib.com>2022-10-11 12:15:08 +0200
commit0f4a4d7527433eef6e95c51edea47bcfa33bbf7c (patch)
tree5b936105aa4f776356921b30953f3b25cd5d7cf4 /app/javascript/flavours/glitch/features/compose/util
parent7097a459be125f0c18bc4c2c77a00ba92c408bf9 (diff)
Move some modules from flavours/glitch/utils/ back to flavours/glitch/features/compose/util/
Diffstat (limited to 'app/javascript/flavours/glitch/features/compose/util')
-rw-r--r--app/javascript/flavours/glitch/features/compose/util/counter.js9
-rw-r--r--app/javascript/flavours/glitch/features/compose/util/url_regex.js30
2 files changed, 39 insertions, 0 deletions
diff --git a/app/javascript/flavours/glitch/features/compose/util/counter.js b/app/javascript/flavours/glitch/features/compose/util/counter.js
new file mode 100644
index 000000000..7aa9e87b1
--- /dev/null
+++ b/app/javascript/flavours/glitch/features/compose/util/counter.js
@@ -0,0 +1,9 @@
+import { urlRegex } from './url_regex';
+
+const urlPlaceholder = '$2xxxxxxxxxxxxxxxxxxxxxxx';
+
+export function countableText(inputText) {
+  return inputText
+    .replace(urlRegex, urlPlaceholder)
+    .replace(/(^|[^\/\w])@(([a-z0-9_]+)@[a-z0-9\.\-]+[a-z0-9]+)/ig, '$1@$3');
+};
diff --git a/app/javascript/flavours/glitch/features/compose/util/url_regex.js b/app/javascript/flavours/glitch/features/compose/util/url_regex.js
new file mode 100644
index 000000000..9c2005c53
--- /dev/null
+++ b/app/javascript/flavours/glitch/features/compose/util/url_regex.js
@@ -0,0 +1,30 @@
+import regexSupplant from 'twitter-text/dist/lib/regexSupplant';
+import validUrlPrecedingChars from 'twitter-text/dist/regexp/validUrlPrecedingChars';
+import validDomain from 'twitter-text/dist/regexp/validDomain';
+import validPortNumber from 'twitter-text/dist/regexp/validPortNumber';
+import validUrlPath from 'twitter-text/dist/regexp/validUrlPath';
+import validUrlQueryChars from 'twitter-text/dist/regexp/validUrlQueryChars';
+import validUrlQueryEndingChars from 'twitter-text/dist/regexp/validUrlQueryEndingChars';
+
+// The difference with twitter-text's extractURL is that the protocol isn't
+// optional.
+
+export const urlRegex = regexSupplant(
+  '('                                                          + // $1 URL
+    '(#{validUrlPrecedingChars})'                              + // $2
+    '(https?:\\/\\/)'                                          + // $3 Protocol
+    '(#{validDomain})'                                         + // $4 Domain(s)
+    '(?::(#{validPortNumber}))?'                               + // $5 Port number (optional)
+    '(\\/#{validUrlPath}*)?'                                   + // $6 URL Path
+    '(\\?#{validUrlQueryChars}*#{validUrlQueryEndingChars})?'  + // $7 Query String
+  ')',
+  {
+    validUrlPrecedingChars,
+    validDomain,
+    validPortNumber,
+    validUrlPath,
+    validUrlQueryChars,
+    validUrlQueryEndingChars,
+  },
+  'gi',
+);