File size: 990 Bytes
61bb557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
diff --git a/slugify/slugify.py b/slugify/slugify.py
index 0194378..ebe424a 100644
--- a/slugify/slugify.py
+++ b/slugify/slugify.py
@@ -92,13 +92,18 @@ def _modern_truncate(text: str, max_length: int, word_boundary: bool, separator:
     # A hard cut may shorten a word but must not emit a partial/trailing delimiter.
     parts: list[str] = []
     length = 0
+    previous_index = 0
     for index, word in enumerate(tokens):
-        delimiter = separator if index else ''
+        if not word:
+            continue
+        # Keep repeated delimiters with the next nonempty word until it fits.
+        delimiter = separator * (index - previous_index)
         remaining = max_length - length - len(delimiter)
         if remaining <= 0:
             break
         parts.append(delimiter + word[:remaining])
         length += len(delimiter) + min(len(word), remaining)
+        previous_index = index
         if len(word) > remaining:
             break
     return ''.join(parts)