Class: LLM::Repl::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl/markdown.rb

Overview

This class is designed to represent a markdown string (typically from a model's response) as a tree of objects where each object contains a piece of text, and also optional style information for that text (eg bold, underscore, ...)

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ LLM::Repl::Markdown

Parameters:

  • text (String)


14
15
16
17
# File 'lib/llm/repl/markdown.rb', line 14

def initialize(text)
  @doc = Kramdown::Document.new(text)
  @ast = []
end

Instance Method Details

#astArray<Hash>

Returns:

  • (Array<Hash>)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/llm/repl/markdown.rb', line 21

def ast
  @ast.tap do
    ##
    # Recurisvely travels the markdown document and
    # populates the `@ast` variable along the way.
    # The AST is composed of structured data that
    # carries both text and styling information that
    # is applied by the UI thread.
    walk(@doc.root)

    ##
    # This is required because the AST collects
    # empty nodes towards the end of the tree.
    # If we don't pop them we end up with excessive
    # amount of newlines between turns.
    last = @ast.last
    while last and last[:text].to_s.strip.empty?
      @ast.pop
      last = @ast.last
    end
  end
end