Module: RailsAiBridge::Registry::Truncatable

Included in:
RakePresenter, Tools::ListRegistry::RegistryCatalogFormatter, Tools::ResolveSkill
Defined in:
lib/rails_ai_bridge/registry/truncatable.rb

Overview

Shared mixin for truncating text and sanitizing markdown for safe output.

Examples:

include Truncatable
truncate('A very long description here', 20) #=> "A very long descript…"
sanitize_markdown("skill | bad\ncontent") #=> "skill \\| bad content"

Instance Method Summary collapse

Instance Method Details

#sanitize_markdown(text) ⇒ String

Sanitizes a string for safe inline embedding in markdown output.

Strips newlines (to prevent header/block injection) and escapes pipe characters (to prevent markdown table structure breakage). Used for skill names, pack names, and descriptions sourced from third-party manifests that may contain adversarial content.

Parameters:

  • text (String, nil)

    the text to sanitize

Returns:

  • (String)

    sanitized text safe for markdown inline use



32
33
34
# File 'lib/rails_ai_bridge/registry/truncatable.rb', line 32

def sanitize_markdown(text)
  text.to_s.gsub(/[\r\n]+/, ' ').gsub('|', '\\|')
end

#truncate(text, max) ⇒ String

Truncates +text+ to at most +max+ characters, appending +…+ when truncated.

Parameters:

  • text (String)

    the text to truncate

  • max (Integer)

    maximum number of characters (including the ellipsis)

Returns:

  • (String)

    original text when short enough, or truncated form



17
18
19
20
21
# File 'lib/rails_ai_bridge/registry/truncatable.rb', line 17

def truncate(text, max)
  return text if text.length <= max

  "#{text[0, max - 1]}"
end