generatePreview static method

({bool hasMore, String preview}) generatePreview(
  1. String content,
  2. int maxBytes
)

Generate a preview of content, truncating at a newline boundary when possible.

Implementation

static ({String preview, bool hasMore}) generatePreview(
  String content,
  int maxBytes,
) {
  if (content.length <= maxBytes) {
    return (preview: content, hasMore: false);
  }

  final truncated = content.substring(0, maxBytes);
  final lastNewline = truncated.lastIndexOf('\n');

  final cutPoint = lastNewline > (maxBytes * 0.5).round()
      ? lastNewline
      : maxBytes;

  return (preview: content.substring(0, cutPoint), hasMore: true);
}