Module: Trifle::Docs::Helper::Llms

Defined in:
lib/trifle/docs/helper/llms.rb

Class Method Summary collapse

Class Method Details

.flatten_sitemap(node, path = []) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/trifle/docs/helper/llms.rb', line 61

def flatten_sitemap(node, path = [])
  return [] unless node.is_a?(Hash)

  entries = []
  meta = node['_meta']
  entries << { url: path.join('/'), meta: meta } if meta

  node.keys.reject { |key| key == '_meta' }.sort.each do |key|
    entries.concat(flatten_sitemap(node[key], path + [key]))
  end

  entries
end

.format_page(meta:, url:, raw_content:) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/trifle/docs/helper/llms.rb', line 75

def format_page(meta:, url:, raw_content:)
  lines = page_header(meta: meta, url: url)
  lines << raw_content.to_s.strip
  lines << ''
  lines << '---'

  lines.join("\n")
end

.full_markdown(config: nil, base_url: '') ⇒ Object



20
21
22
23
24
25
26
# File 'lib/trifle/docs/helper/llms.rb', line 20

def full_markdown(config: nil, base_url: '')
  pages = llms_pages(config: config, base_url: base_url)
  return nil if pages.nil?

  pages.filter_map { |page| render_llms_page(page, config: config) }
       .join("\n\n")
end

.homepage_markdown(config: nil, base_url: '') ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/trifle/docs/helper/llms.rb', line 9

def homepage_markdown(config: nil, base_url: '')
  base_url = normalize_base_url(base_url)
  meta = Trifle::Docs.meta(url: base_url, config: config)
  return nil if meta.nil?

  Trifle::Docs::Helper::MarkdownLayout.render(
    meta: meta,
    raw_content: Trifle::Docs.raw_content(url: base_url, config: config)
  )
end

.llms_pages(config:, base_url:) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/trifle/docs/helper/llms.rb', line 28

def llms_pages(config:, base_url:)
  base_url = normalize_base_url(base_url)
  sitemap = Trifle::Docs.sitemap(config: config)
  scoped_sitemap = sitemap_subtree(sitemap, base_url)
  return nil if scoped_sitemap.nil?

  path = base_url.empty? ? [] : base_url.split('/')
  flatten_sitemap(scoped_sitemap, path)
end

.normalize_base_url(base_url) ⇒ Object



57
58
59
# File 'lib/trifle/docs/helper/llms.rb', line 57

def normalize_base_url(base_url)
  base_url.to_s.gsub(%r{^/+}, '').gsub(%r{/+$}, '')
end

.page_header(meta:, url:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/trifle/docs/helper/llms.rb', line 84

def page_header(meta:, url:)
  title = meta['title'] || Trifle::Docs::Helper::MarkdownLayout.derive_title_from_url(url)
  page_url = meta['url'] || "/#{url}"

  [
    "# #{title}",
    '',
    "Source: #{page_url}",
    ''
  ]
end

.render_llms_page(page, config:) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/trifle/docs/helper/llms.rb', line 38

def render_llms_page(page, config:)
  meta = page[:meta]
  return nil if meta.nil? || meta['type'] == 'file'

  raw = Trifle::Docs.raw_content(url: page[:url], config: config)
  return nil if raw.nil? || raw.strip.empty?

  format_page(meta: meta, url: page[:url], raw_content: raw)
end

.sitemap_subtree(sitemap, base_url) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/trifle/docs/helper/llms.rb', line 48

def sitemap_subtree(sitemap, base_url)
  return nil unless sitemap.is_a?(Hash)

  base_url = normalize_base_url(base_url)
  return sitemap if base_url.empty?

  sitemap.dig(*base_url.split('/'))
end