Class: ContentBlockTools::ContentBlockReference
- Inherits:
-
Data
- Object
- Data
- ContentBlockTools::ContentBlockReference
- Defined in:
- lib/content_block_tools/content_block_reference.rb,
lib/content_block_tools/content_block_reference.rb
Overview
Defines a reference pointer for a Content Block
Constant Summary collapse
- SUPPORTED_DOCUMENT_TYPES =
An array of the supported document types
%w[contact content_block_pension content_block_contact].freeze
- UUID_REGEX =
The regex used to find UUIDs
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/- CONTENT_ID_ALIAS_REGEX =
The regex used to find content ID aliases
/[a-z0-9\-–—]+/- FIELD_REGEX =
The regex to find optional field names after the UUID, begins with ‘/’
/(\/[a-z0-9_\-–—\/]*)?/- EMBED_REGEX =
The regex used when scanning a document using find_all_in_document
/({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):(#{UUID_REGEX}|#{CONTENT_ID_ALIAS_REGEX})#{FIELD_REGEX}}})/
Instance Attribute Summary collapse
-
#document_type ⇒ String
readonly
The document type of the content block - this will be used to work out which Presenter will be used to render the content block.
-
#embed_code ⇒ String
readonly
The embed_code used for a block.
-
#identifier ⇒ String
readonly
The identifier for a block - can be a UUID or a slug.
Class Method Summary collapse
-
.find_all_in_document(document) ⇒ Array<ContentBlockReference>
Finds all content block references within a document, using
ContentBlockReference::EMBED_REGEXto scan through the document. -
.from_match_data(match_data) ⇒ ContentBlockReference
private
Converts match data from a regex scan into a ContentBlockReference object.
-
.from_string(embed_code) ⇒ ContentBlockReference
Converts a single embed code string into a ContentBlockReference object.
Instance Method Summary collapse
-
#content_store_identifier ⇒ String
Returns the content store path for this content block reference.
-
#identifier_is_alias? ⇒ Boolean
Returns if the identifier is an alias.
Instance Attribute Details
#document_type ⇒ String (readonly)
The document type of the content block - this will be used to work out which Presenter will be used to render the content block. All supported document_types are documented in SUPPORTED_DOCUMENT_TYPES
29 30 31 |
# File 'lib/content_block_tools/content_block_reference.rb', line 29 def document_type @document_type end |
#embed_code ⇒ String (readonly)
The embed_code used for a block
29 30 31 |
# File 'lib/content_block_tools/content_block_reference.rb', line 29 def end |
#identifier ⇒ String (readonly)
The identifier for a block - can be a UUID or a slug. The UUID will refer to the content_id of a block within Publishing API, while the slug will refer to a block’s Content ID alias.
29 30 31 |
# File 'lib/content_block_tools/content_block_reference.rb', line 29 def identifier @identifier end |
Class Method Details
.find_all_in_document(document) ⇒ Array<ContentBlockReference>
Finds all content block references within a document, using ContentBlockReference::EMBED_REGEX to scan through the document
68 69 70 71 72 |
# File 'lib/content_block_tools/content_block_reference.rb', line 68 def find_all_in_document(document) document.scan(EMBED_REGEX).map do |match_data| ContentBlockReference.from_match_data(match_data) end end |
.from_match_data(match_data) ⇒ ContentBlockReference
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts match data from a regex scan into a ContentBlockReference object
This method is used internally by find_all_in_document and from_string to create ContentBlockReference instances from regex match data. It normalizes the match data by replacing en/em dashes with double/triple dashes (which can occur due to Kramdown’s markdown parsing) before creating the object.
114 115 116 117 118 |
# File 'lib/content_block_tools/content_block_reference.rb', line 114 def from_match_data(match_data) match = prepare_match(match_data) ContentBlockTools.logger.info("Found Content Block Reference: #{match}") ContentBlockReference.new(document_type: match[1], identifier: match[2], embed_code: match[0]) end |
.from_string(embed_code) ⇒ ContentBlockReference
Converts a single embed code string into a ContentBlockReference object
Parses an embed code string using EMBED_REGEX to extract the document type, identifier, and embed code, then creates a ContentBlockReference instance.
89 90 91 92 93 94 |
# File 'lib/content_block_tools/content_block_reference.rb', line 89 def from_string() match_data = .match(/^#{EMBED_REGEX}$/) raise InvalidEmbedCodeError unless match_data ContentBlockReference.from_match_data(match_data.captures) end |
Instance Method Details
#content_store_identifier ⇒ String
Returns the content store path for this content block reference
Constructs a path used to identify and retrieve the content block from the content store. The path follows the format ‘/content-blocks/#document_type/#identifier`.
59 60 61 |
# File 'lib/content_block_tools/content_block_reference.rb', line 59 def content_store_identifier "/content-blocks/#{document_type}/#{identifier}" end |
#identifier_is_alias? ⇒ Boolean
Returns if the identifier is an alias
44 45 46 |
# File 'lib/content_block_tools/content_block_reference.rb', line 44 def identifier_is_alias? !identifier.match?(UUID_REGEX) end |