Class: LlmsTxt::MarkdownTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/llms_txt/markdown_transformer.rb

Overview

Transforms markdown files to be AI-friendly

Processes individual markdown files to make them more suitable for LLM consumption by expanding relative links to absolute URLs and converting HTML URLs to markdown-friendly formats.

Examples:

Transform with base URL

transformer = LlmsTxt::MarkdownTransformer.new('README.md',
  base_url: 'https://myproject.io'
)
content = transformer.transform

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, options = {}) ⇒ MarkdownTransformer

Initialize a new markdown transformer

Parameters:

  • file_path (String)

    path to markdown file to transform

  • options (Hash) (defaults to: {})

    transformation options

Options Hash (options):

  • :base_url (String)

    base URL for expanding relative links

  • :convert_urls (Boolean)

    convert HTML URLs to markdown format



30
31
32
33
# File 'lib/llms_txt/markdown_transformer.rb', line 30

def initialize(file_path, options = {})
  @file_path = file_path
  @options = options
end

Instance Attribute Details

#file_pathString (readonly)

Returns path to markdown file.

Returns:

  • (String)

    path to markdown file



19
20
21
# File 'lib/llms_txt/markdown_transformer.rb', line 19

def file_path
  @file_path
end

#optionsHash (readonly)

Returns transformation options.

Returns:

  • (Hash)

    transformation options



22
23
24
# File 'lib/llms_txt/markdown_transformer.rb', line 22

def options
  @options
end

Instance Method Details

#transformString

Transform markdown content to be AI-friendly

Applies transformations to make the markdown more suitable for LLM processing:

  • Expands relative links to absolute URLs (if base_url provided)

  • Converts HTML URLs to markdown format (if convert_urls enabled)

Returns:

  • (String)

    transformed markdown content



42
43
44
45
46
47
48
49
# File 'lib/llms_txt/markdown_transformer.rb', line 42

def transform
  content = File.read(file_path)

  content = expand_relative_links(content) if options[:base_url]
  content = convert_html_urls(content) if options[:convert_urls]

  content
end