Module: WCC::Contentful::RichText
- Defined in:
- lib/wcc/contentful/rich_text.rb,
lib/wcc/contentful/rich_text/node.rb
Overview
This module contains a number of structs representing nodes in a Contentful rich text field. When the Model layer parses a Rich Text field from Contentful, it is turned into a WCC::Contentful::RichText::Document node. The content method of this node is an Array containing paragraph, blockquote, entry, and other nodes.
The various structs in the RichText object model are designed to mimic the Hash interface, so that the indexing operator ‘#[]` and the `#dig` method can be used to traverse the data. The data can also be accessed by the attribute reader methods defined on the structs. Both of these are considered part of the public API of the model and will not change.
In a future release we plan to implement automatic link resolution. When that happens, the ‘.data` attribute of embedded entries and assets will return a new class that is able to resolve the `.target` automatically into a full entry or asset. This future class will still respect the hash accessor methods `#[]`, `#dig`, `#keys`, and `#each`, so it is safe to use those.
Defined Under Namespace
Modules: Node Classes: Blockquote, Document, EmbeddedAssetBlock, EmbeddedEntryBlock, EmbeddedEntryInline, EmbeddedResourceBlock, HR, Heading, Hyperlink, ListItem, OrderedList, Paragraph, Table, TableCell, TableHeaderCell, TableRow, Text, Unknown, UnorderedList
Class Method Summary collapse
-
.tokenize(raw, renderer: nil) ⇒ Object
Recursively converts a raw JSON-parsed hash into the RichText object model.
Class Method Details
.tokenize(raw, renderer: nil) ⇒ Object
Recursively converts a raw JSON-parsed hash into the RichText object model. If renderer are provided, the model will be able to resolve links to entries and enable direct rendering of documents to HTML.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wcc/contentful/rich_text.rb', line 28 def self.tokenize(raw, renderer: nil) return unless raw 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+)/ 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, renderer: renderer) end |