summaryrefslogtreecommitdiff
path: root/packages/frontend-shared/js
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-02-12 14:12:13 -0500
committerHazelnoot <acomputerdog@gmail.com>2025-02-16 19:20:41 -0500
commit563e32316f808995fe2500286ca8df8954ff8c14 (patch)
tree6b35c1537e45cbc0e0c4e00a0236080990fc55d6 /packages/frontend-shared/js
parentremove mandatory CW logic from NoteCreateService and NoteEditService (diff)
downloadsharkey-563e32316f808995fe2500286ca8df8954ff8c14.tar.gz
sharkey-563e32316f808995fe2500286ca8df8954ff8c14.tar.bz2
sharkey-563e32316f808995fe2500286ca8df8954ff8c14.zip
factor out common append-content-warning routine for use in both frontend and backend
Diffstat (limited to 'packages/frontend-shared/js')
-rw-r--r--packages/frontend-shared/js/append-content-warning.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/frontend-shared/js/append-content-warning.ts b/packages/frontend-shared/js/append-content-warning.ts
new file mode 100644
index 0000000000..7f24a66f23
--- /dev/null
+++ b/packages/frontend-shared/js/append-content-warning.ts
@@ -0,0 +1,62 @@
+/*
+ * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+/*
+ * Important Note: this file must be kept in sync with packages/backend/src/misc/append-content-warning.ts
+ */
+
+/**
+ * Appends an additional content warning onto an existing one.
+ * The additional value will not be added if it already exists within the original input.
+ * @param original Existing content warning
+ * @param additional Content warning to append
+ * @param reverse If true, then the additional CW will be prepended instead of appended.
+ */
+export function appendContentWarning(original: string | null | undefined, additional: string, reverse = false): string {
+ // Easy case - if original is empty, then additional replaces it.
+ if (!original) {
+ return additional;
+ }
+
+ // Easy case - if the additional CW is empty, then don't append it.
+ if (!additional) {
+ return original;
+ }
+
+ // If the additional CW already exists in the input, then we *don't* append another copy!
+ if (includesWholeWord(original, additional)) {
+ return original;
+ }
+
+ return reverse
+ ? `${additional}, ${original}`
+ : `${original}, ${additional}`;
+}
+
+/**
+ * Emulates a regular expression like /\b(pattern)\b/, but with a raw non-regex pattern.
+ * We're checking to see whether the default CW appears inside the existing CW, but *only* if there's word boundaries on either side.
+ * @param input Input string to search
+ * @param target Target word / phrase to search for
+ */
+function includesWholeWord(input: string, target: string): boolean {
+ const parts = input.split(target);
+
+ // The additional string could appear multiple times within the original input.
+ // We need to check each occurrence, since any of them could potentially match.
+ for (let i = 0; i + 1 < parts.length; i++) {
+ const before = parts[i];
+ const after = parts[i + 1];
+
+ // If either the preceding or following tokens are a "word", then this "match" is actually just part of a longer word.
+ // Likewise, if *neither* token is a word, then this is a real match and the CW already exists in the input.
+ if (!/\w$/.test(before) && !/^\w/.test(after)) {
+ return true;
+ }
+ }
+
+ // If we don't match, then there is no existing CW.
+ return false;
+}