lib/wcc/contentful/rich_text.rb in wcc-contentful-1.4.0 vs lib/wcc/contentful/rich_text.rb in wcc-contentful-1.5.0.rc1
- old
+ new
@@ -21,38 +21,60 @@
# entry or asset. This future class will still respect the hash accessor methods
# `#[]`, `#dig`, `#keys`, and `#each`, so it is safe to use those.
module WCC::Contentful::RichText
##
# Recursively converts a raw JSON-parsed hash into the RichText object model.
- def self.tokenize(raw, context = nil)
+ # If renderer are provided, the model will be able to resolve links to entries
+ # and enable direct rendering of documents to HTML.
+ def self.tokenize(raw, renderer: nil)
return unless raw
- return raw.map { |c| tokenize(c, context) } if raw.is_a?(Array)
+ return raw.map { |c| tokenize(c) } if raw.is_a?(Array)
klass =
case raw['nodeType']
when 'document'
Document
when 'paragraph'
Paragraph
+ when 'hr'
+ HR
when 'blockquote'
Blockquote
when 'text'
Text
+ when 'ordered-list'
+ OrderedList
+ when 'unordered-list'
+ UnorderedList
+ when 'list-item'
+ ListItem
+ when 'table'
+ Table
+ when 'table-row'
+ TableRow
+ when 'table-cell'
+ TableCell
+ when 'table-header-cell'
+ TableHeaderCell
when 'embedded-entry-inline'
EmbeddedEntryInline
when 'embedded-entry-block'
EmbeddedEntryBlock
when 'embedded-asset-block'
EmbeddedAssetBlock
when /heading-(\d+)/
- size = Regexp.last_match(1)
- const_get("Heading#{size}")
+ Heading
+ when /(\w+-)?hyperlink/
+ Hyperlink
else
+ # Future proofing for new node types introduced by Contentful.
+ # The best list of node types maintained by Contentful is here:
+ # https://github.com/contentful/rich-text/blob/master/packages/rich-text-types/src/blocks.ts
Unknown
end
- klass.tokenize(raw, context)
+ klass.tokenize(raw, renderer: renderer)
end
Document =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
@@ -61,20 +83,60 @@
Paragraph =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
end
+ HR =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
Blockquote =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
end
Text =
Struct.new(:nodeType, :value, :marks, :data) do
include WCC::Contentful::RichText::Node
end
+ OrderedList =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ UnorderedList =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ ListItem =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ Table =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ TableRow =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ TableCell =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ TableHeaderCell =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
EmbeddedEntryInline =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
end
@@ -86,20 +148,42 @@
EmbeddedAssetBlock =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
end
- (1..5).each do |i|
- struct =
- Struct.new(:nodeType, :data, :content) do
- include WCC::Contentful::RichText::Node
+ EmbeddedResourceBlock =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+ end
+
+ Heading =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+
+ def self.matches?(node_type)
+ node_type =~ /heading-(\d+)/
end
- sz = i
- struct.define_singleton_method(:node_type) { "heading-#{sz}" }
- const_set("Heading#{sz}", struct)
- end
+ def size
+ @size ||= /heading-(\d+)/.match(nodeType)[1]&.to_i
+ end
+ end
+
+ Hyperlink =
+ Struct.new(:nodeType, :data, :content) do
+ include WCC::Contentful::RichText::Node
+
+ def self.matches?(node_type)
+ node_type =~ /(\w+-)?hyperlink/
+ end
+ end
+
Unknown =
Struct.new(:nodeType, :data, :content) do
include WCC::Contentful::RichText::Node
+
+ # Unknown nodes are the catch all, so they always match anything that made it to the else case of the switch.
+ def self.matches?(_node_type)
+ true
+ end
end
end