instance method to_markdown

Ruby on Rails edge

Since edge

Signature

to_markdown(attachment_links: false)

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) # => "![racecar.jpg](http://example.com/rails/active_storage/blobs/...)"

# 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 # => "![A photo](https://example.com/photo.jpg)"

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

Parameters

attachment_links key = false
Source
# 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

Defined in actiontext/lib/action_text/attachment.rb line 181 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionText::Attachment

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close