Skip to content

Commit

Permalink
Support JSON-LD named graph (mastodon#31288)
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 authored Aug 6, 2024
1 parent 135493c commit e9e63ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion app/lib/link_details_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def root_array(root)
end

def json
@json ||= root_array(Oj.load(@data)).compact.find { |obj| SUPPORTED_TYPES.include?(obj['@type']) } || {}
@json ||= root_array(Oj.load(@data))
.map { |node| JSON::LD::API.compact(node, 'https://schema.org') }
.find { |node| SUPPORTED_TYPES.include?(node['type']) } || {}
end
end

Expand Down
36 changes: 26 additions & 10 deletions spec/lib/link_details_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
},
}.to_json
end
let(:html) { <<~HTML }
<!doctype html>
<html>
<body>
<script type="application/ld+json">
#{ld_json}
</script>
</body>
</html>
HTML

shared_examples 'structured data' do
it 'extracts the expected values from structured data' do
Expand Down Expand Up @@ -224,21 +234,27 @@
},
}.to_json
end
let(:html) { <<~HTML }
<!doctype html>
<html>
<body>
<script type="application/ld+json">
#{ld_json}
</script>
</body>
</html>
HTML

it 'joins author names' do
expect(subject.author_name).to eq 'Author 1, Author 2'
end
end

context 'with named graph' do
let(:ld_json) do
{
'@context' => 'https://schema.org',
'@graph' => [
'@type' => 'NewsArticle',
'headline' => "What's in a name",
],
}.to_json
end

it 'descends into @graph node' do
expect(subject.title).to eq "What's in a name"
end
end
end

context 'when Open Graph protocol data is present' do
Expand Down

0 comments on commit e9e63ed

Please sign in to comment.