Action Text Attachment¶ ↑
Attachments serialize attachables to HTML or plain text.
class Person < ApplicationRecord include ActionText::Attachable end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk..."
- C
- F
- N
- T
- W
Constants
| ATTRIBUTES | = | %w( sgid content-type url href filename filesize width height previewable presentation caption content ).freeze |
Attributes
| [R] | attachable | |
| [R] | node |
Class Public methods
fragment_by_canonicalizing_attachments(content) Link
from_attachable(attachable, attributes = {}) Link
from_attachables(attachables) Link
from_attributes(attributes, attachable = nil) Link
from_node(node, attachable = nil) Link
new(node, attachable) Link
Instance Public methods
caption() Link
full_attributes() Link
to_html() Link
Converts the attachment to HTML.
attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
to_markdown(attachment_links: false) Link
Converts the attachment to Markdown.
By default, ActiveStorage blob attachments render as bracketed text:
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_markdown # => "\\[racecar.jpg\\]"
Use the caption when set:
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom") attachment.to_markdown # => "\\[Vroom vroom\\]"
When attachment_links is true and a rendering context is available (e.g., controller or mailer action), ActiveStorage blob attachments generate Markdown links with URLs.
# Image blob attachment.to_markdown(attachment_links: true) # => "" # Non-image blob attachment.to_markdown(attachment_links: true) # => "[report.pdf](http://example.com/rails/active_storage/blobs/...)"
Remote images always render as Markdown image links when the URL scheme is allowed:
content = ActionText::Content.new('<action-text-attachment content-type="image/jpeg" url="https://example.com/photo.jpg" caption="A photo"></action-text-attachment>') content.to_markdown # => ""
Remote images with a disallowed URL scheme render as escaped bracketed text:
content = ActionText::Content.new('<action-text-attachment content-type="image/jpeg" url="data:text/html,PAYLOAD" caption="Click"></action-text-attachment>') content.to_markdown # => "\\[Click\\]"
The presentation can be overridden by implementing the attachable_markdown_representation method:
class Person < ApplicationRecord include ActionText::Attachable def attachable_markdown_representation(caption, attachment_links: false) ActionText::MarkdownConversion.markdown_link("@#{name}", Rails.application.routes.url_helpers.person_url(self)) end end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_markdown # => "[@Javan](http://example.com/people/1)"
NOTE: When overriding attachable_markdown_representation, the caption parameter is derived from the document and should be considered untrusted, so an implementation must escape any caption-derived text with ActionText::MarkdownConversion.escape_markdown_text, or pass it through ActionText::MarkdownConversion.markdown_link, which escapes the link title and rejects disallowed URI schemes. Returning the caption unchanged lets a stored rich text body inject arbitrary Markdown.
class Person < ApplicationRecord include ActionText::Attachable def attachable_markdown_representation(caption, attachment_links: false) ActionText::MarkdownConversion.escape_markdown_text(caption.to_s) # take care to escape the caption end end
# File actiontext/lib/action_text/attachment.rb, line 181 def to_markdown(attachment_links: false) if respond_to?(:attachable_markdown_representation) attachable_markdown_representation(caption, attachment_links: attachment_links) else MarkdownConversion.escape_markdown_text(caption.to_s) end end
to_plain_text() Link
Converts the attachment to plain text.
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_plain_text # => "[racecar.jpg]"
Use the caption when set:
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom") attachment.to_plain_text # => "[Vroom vroom]"
The presentation can be overridden by implementing the attachable_plain_text_representation method:
class Person < ApplicationRecord include ActionText::Attachable def attachable_plain_text_representation(caption) "[#{name}]" end end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_plain_text # => "[Javan]"