Class: LLM::Repl::Markdown

Inherits:
Object
  • Object
show all
Includes:
Table
Defined in:
lib/llm/repl/markdown.rb,
lib/llm/repl/markdown/table.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, ...)

Defined Under Namespace

Modules: Table

Constant Summary collapse

SYMBOLS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Kramdown goes a bit beyond a standard markdown parser by representing certain characters or character sequences as distinct node types that are represented by :typographic_sym, and :smart_quote.

The node's value maps back to one of the keys in this Hash, and the values are unicode characters that provide a visual representation of the node.

{
  hellip: "",
  ndash:  "", mdash:  "",
  laquo:  "«", raquo:  "»",
  laquo_space: "« ", raquo_space: "» ",
  lsquo:  "", rsquo:  "",
  ldquo:  "", rdquo:  ""
}

Instance Method Summary collapse

Methods included from Table

#walk_table

Constructor Details

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

Parameters:

  • text (String)
  • width (Integer)


38
39
40
41
42
# File 'lib/llm/repl/markdown.rb', line 38

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

Instance Method Details

#astArray<Node>

Returns:

  • (Array<Node>)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/llm/repl/markdown.rb', line 46

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