about summary refs log tree commit diff
path: root/src/lib/strings/rich-text-sanitize.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/strings/rich-text-sanitize.ts')
-rw-r--r--src/lib/strings/rich-text-sanitize.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib/strings/rich-text-sanitize.ts b/src/lib/strings/rich-text-sanitize.ts
new file mode 100644
index 000000000..0b5895707
--- /dev/null
+++ b/src/lib/strings/rich-text-sanitize.ts
@@ -0,0 +1,32 @@
+import {RichText} from './rich-text'
+
+const EXCESS_SPACE_RE = /[\r\n]([\u00AD\u2060\u200D\u200C\u200B\s]*[\r\n]){2,}/
+const REPLACEMENT_STR = '\n\n'
+
+export function removeExcessNewlines(richText: RichText): RichText {
+  return clean(richText, EXCESS_SPACE_RE, REPLACEMENT_STR)
+}
+
+// TODO: check on whether this works correctly with multi-byte codepoints
+export function clean(
+  richText: RichText,
+  targetRegexp: RegExp,
+  replacementString: string,
+): RichText {
+  richText = richText.clone()
+
+  let match = richText.text.match(targetRegexp)
+  while (match && typeof match.index !== 'undefined') {
+    const oldText = richText.text
+    const removeStartIndex = match.index
+    const removeEndIndex = removeStartIndex + match[0].length
+    richText.delete(removeStartIndex, removeEndIndex)
+    if (richText.text === oldText) {
+      break // sanity check
+    }
+    richText.insert(removeStartIndex, replacementString)
+    match = richText.text.match(targetRegexp)
+  }
+
+  return richText
+}