Repository: rails/rails
File: actiontext/lib/action_text/engine.rb (Ruby)

Code before:
    end

    initializer "action_text.asset" do
      if Rails.application.config.respond_to?(:assets)
        Rails.application.config.assets.precompile += %w( actiontext.js actiontext.esm.js )
      end
    end

    initializer "action_text.attachable" do
      ActiveSupport.on_load(:active_storage_blob) do
        include ActionText::Attachable

        def previewable_attachable?
          representable?
        end

        def attachable_plain_text_representation(caption = nil)
          "[#{caption || filename}]"
        end

        def attachable_markdown_representation(caption = nil)
          title = (caption || filename).to_s
          renderer = ActionText::Content.renderer
          url = begin
            renderer.url_for(self) if renderer.respond_to?(:url_for)
          rescue ActionController::UrlGenerationError, ArgumentError
            nil
          end

          if url
            if image?
              "!#{MarkdownConversion.markdown_link(title, url)}"
            else
              MarkdownConversion.markdown_link(title, url)
            end
          else
            "[#{MarkdownConversion.escape_markdown_text(title)}]"
          end
        end

        def to_trix_content_attachment_partial_path
          to_editor_content_attachment_partial_path
        end
        deprecate :to_trix_content_attachment_partial_path, deprecator: ActionText.deprecator

        def to_editor_content_attachment_partial_path
          nil
        end
      end
    end


Review comment (suggestion):
`ArgumentError` rescue smells over-broad; what's it rescuing?

Also, not technically necessary, but may be clearer to use safe nav for respond_to? to communicate that we know renderer may be nil.

Code after:
    end

    initializer "action_text.asset" do
      if Rails.application.config.respond_to?(:assets)
        Rails.application.config.assets.precompile += %w( actiontext.js actiontext.esm.js )
      end
    end

    initializer "action_text.attachable" do
      ActiveSupport.on_load(:active_storage_blob) do
        include ActionText::Attachable

        def previewable_attachable?
          representable?
        end

        def attachable_plain_text_representation(caption = nil)
          "[#{caption || filename}]"
        end

        def attachable_markdown_representation(caption = nil, attachment_links: false)
          title = (caption || filename).to_s

          if attachment_links
            renderer = ActionText::Content.renderer
            raise ArgumentError, "attachment_links requires a rendering context" unless renderer

            url = renderer.url_for(self)
            if image?
              "!#{MarkdownConversion.markdown_link(title, url)}"
            else
              MarkdownConversion.markdown_link(title, url)
            end
          else
            "[#{MarkdownConversion.escape_markdown_text(title)}]"
          end
        end

        def to_trix_content_attachment_partial_path
          to_editor_content_attachment_partial_path
        end
        deprecate :to_trix_content_attachment_partial_path, deprecator: ActionText.deprecator

        def to_editor_content_attachment_partial_path
          nil
        end
      end
    end

    initializer "action_text.helper" do
      %i[action_controller_base action_mailer].each do |base|