Why Naive String Slicing Breaks Link Previews With Emoji and Urdu Text
Link preview titles and descriptions are often generated automatically from CMS fields, product feeds, or Markdown bodies rather than written by hand. A common one-line approach that slices strings to a fixed length introduces several bugs. JavaScript's slice counts UTF-16 code units, so it can split emoji such as flags or joined sequences, producing broken characters. The same issue affects Urdu, Arabic, and Hindi text, where combining marks can be detached from their base letters. The article proposes counting grapheme clusters with the built-in Intl.Segmenter, which reflects what readers perceive as single characters. It also offers a tested helper that cuts at word boundaries, falls back to grapheme cuts for overly long tokens, and includes the ellipsis within the length budget.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in