Class: GfmToBlockkit::TextSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/gfm_to_blockkit/text_splitter.rb

Constant Summary collapse

MAX_LENGTH =
3000

Class Method Summary collapse

Class Method Details

.split(text, max_length: MAX_LENGTH) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gfm_to_blockkit/text_splitter.rb', line 7

def self.split(text, max_length: MAX_LENGTH)
  return [] if text.nil? || text.empty?
  return [text] if text.length <= max_length

  chunks = []
  remaining = text

  while remaining.length > max_length
    point = find_split_point(remaining, max_length)
    chunks << remaining[0...point].rstrip
    remaining = remaining[point..].lstrip
  end

  chunks << remaining unless remaining.empty?
  chunks
end