diff --git a/lib/cross-platform/index.js b/lib/cross-platform/index.js index 78d002b..e72ea5f 100644 --- a/lib/cross-platform/index.js +++ b/lib/cross-platform/index.js @@ -303,15 +303,21 @@ function formatSection(title, content) { */ /** - * Truncate text to limit with ellipsis + * Truncate text to limit with ellipsis. + * + * Slices on Unicode code points (not UTF-16 code units) so multi-byte + * chars like emoji never end up as orphan surrogates. Non-positive + * maxLength returns the original string unchanged. * * @param {string} text - Text to truncate - * @param {number} maxLength - Maximum length + * @param {number} maxLength - Maximum length (in code points) * @returns {string} Truncated text */ function truncate(text, maxLength) { - if (text.length <= maxLength) return text; - return text.substring(0, maxLength - 3) + '...'; + if (maxLength <= 0) return text; + const codePoints = [...text]; + if (codePoints.length <= maxLength) return text; + return codePoints.slice(0, maxLength - 3).join('') + '...'; } /**