Skip to content

Commit

Permalink
WIP - experimental recursive approach to getting blocks values
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Oct 23, 2024
1 parent 3f4d8c8 commit f75bcca
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions app/models/concerns/publishing_api/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,36 @@ def content
["<h1>#{part[:title]}</h1>", BodyContent.new(part[:body]).html_content]
end

[*values_from_json_paths, *values_from_parts, *values_from_blocks(document_hash)]
[*values_from_json_paths, *values_from_parts, *values_from_blocks_document(document_hash)]
.flatten
.compact_blank
.join(INDEXABLE_CONTENT_SEPARATOR)
.truncate_bytes(INDEXABLE_CONTENT_MAX_BYTE_SIZE)
end

def values_from_blocks(document_hash)
matches = JsonPath.new("$.details.blocks..content", use_symbols: true).on(document_hash)
return [] unless matches.any?

ignore_set = []
values = []
matches.each do |match|
case match
in Array
match.each { |m| ignore_set << m[:content] if m[:content].present? }
values << BodyContent.new(match).html_content
else
if ignore_set.index(match).present?
ignore_set.delete_at(ignore_set.index(match))
else
values << BodyContent.new(match).html_content
end
end
def values_from_blocks_document(document_hash)
if document_hash in { details: { blocks: [*blocks] } }
blocks.flat_map { values_from_blocks(_1) }
else
[]
end
end

values
def values_from_blocks(input)
case input
in {blocks: [*blocks]}
blocks.flat_map { values_from_blocks(_1) }
in {content_type:, content:}
content_type == "text/html" ? [content] : []
in {content: String => content}
[content]
in Hash
input.values.flat_map { values_from_blocks(_1) }
in Array
input.flat_map { values_from_blocks(_1) }
else
[]
end
end
end
end

0 comments on commit f75bcca

Please sign in to comment.