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 7faaad2
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions app/models/concerns/publishing_api/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,19 @@ def content
.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(hash)
case hash
in {blocks: [*blocks]}
blocks.flat_map { values_from_blocks(_1) }
in {content_type: _, content:}
values_from_blocks(content)
in {content: String}
content
in Hash
hash.values.flat_map { values_from_blocks(_1) }
else
raise "Unexpected values_from_blocks input #{hash}"
end

values
end
end
end

0 comments on commit 7faaad2

Please sign in to comment.