Module: RailsAiBridge::Registry::Truncatable
- Defined in:
- lib/rails_ai_bridge/registry/truncatable.rb
Overview
Shared mixin for truncating text and sanitizing markdown for safe output.
Instance Method Summary collapse
-
#sanitize_markdown(text) ⇒ String
Sanitizes a string for safe inline embedding in markdown output.
-
#truncate(text, max) ⇒ String
Truncates +text+ to at most +max+ characters, appending +…+ when truncated.
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.
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.
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 |