Skip to content

Commit 6a665a5

Browse files
authored
Merge pull request rails#48133 from p8/actiontext/document-actiontext-content
Add documentation for ActionText::Content [ci-skip]
2 parents 45411f2 + 6f6d1f9 commit 6a665a5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

actiontext/lib/action_text/content.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# frozen_string_literal: true
22

33
module ActionText
4+
# = Action Text \Content
5+
#
6+
# The <tt>ActionText::Content</tt> class wraps an HTML fragment to add support for
7+
# parsing, rendering and serialization. It can be used to extract links and
8+
# attachments, convert the fragment to plain text, or serialize the fragment
9+
# to the database.
10+
#
11+
# The <tt>ActionText::RichText</tt> record serializes the `body` attribute as
12+
# <tt>ActionText::Content</tt>.
13+
#
14+
# class Message < ActiveRecord::Base
15+
# has_rich_text :content
16+
# end
17+
#
18+
# message = Message.create!(content: "<h1>Funny times!</h1>")
19+
# body = message.content.body # => #<ActionText::Content "<div class=\"trix-conte...">
20+
# body.to_s # => "<h1>Funny times!</h1>"
21+
# body.to_plain_text # => "Funny times!"
422
class Content
523
include Rendering, Serialization
624

@@ -26,10 +44,21 @@ def initialize(content = nil, options = {})
2644
end
2745
end
2846

47+
# Extracts links from the HTML fragment:
48+
#
49+
# html = '<a href="http://example.com/">Example</a>'
50+
# content = ActionText::Content.new(html)
51+
# content.links # => ["http://example.com/"]
2952
def links
3053
@links ||= fragment.find_all("a[href]").map { |a| a["href"] }.uniq
3154
end
3255

56+
# Extracts ActionText::Attachments from the HTML fragment:
57+
#
58+
# attachable = ActiveStorage::Blob.first
59+
# html = %Q(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>)
60+
# content = ActionText::Content.new(html)
61+
# content.attachments # => [#<ActionText::Attachment attachable=#<ActiveStorage::Blob...
3362
def attachments
3463
@attachments ||= attachment_nodes.map do |node|
3564
attachment_for_node(node)
@@ -46,6 +75,12 @@ def gallery_attachments
4675
@gallery_attachments ||= attachment_galleries.flat_map(&:attachments)
4776
end
4877

78+
# Extracts ActionText::Attachables from the HTML fragment:
79+
#
80+
# attachable = ActiveStorage::Blob.first
81+
# html = %Q(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>)
82+
# content = ActionText::Content.new(html)
83+
# content.attachables # => [#<ActiveStorage::Blob...
4984
def attachables
5085
@attachables ||= attachment_nodes.map do |node|
5186
ActionText::Attachable.from_node(node)
@@ -71,6 +106,10 @@ def render_attachment_galleries(&block)
71106
self.class.new(content, canonicalize: false)
72107
end
73108

109+
# Returns the content as plain text with all HTML tags removed.
110+
#
111+
# content = ActionText::Content.new("<h1>Funny times!</h1>")
112+
# content.to_plain_text # => "Funny times!"
74113
def to_plain_text
75114
render_attachments(with_full_attributes: false, &:to_plain_text).fragment.to_plain_text
76115
end

0 commit comments

Comments
 (0)