From da96f4d707adcbedec7bce1b459113093974b50f Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Thu, 16 Nov 2017 11:40:42 -0500 Subject: [PATCH] strip null values from JSON-LD hash --- lib/jekyll-seo-tag/json_ld_drop.rb | 4 ++++ spec/jekyll_seo_tag/json_ld_drop_spec.rb | 2 +- spec/jekyll_seo_tag_integration_spec.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 3c0120e..a8122ea 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -73,6 +73,10 @@ module Jekyll alias_method :mainEntityOfPage, :main_entity private :main_entity + def to_json + to_h.reject { |_k, v| v.nil? }.to_json + end + private attr_reader :page_drop diff --git a/spec/jekyll_seo_tag/json_ld_drop_spec.rb b/spec/jekyll_seo_tag/json_ld_drop_spec.rb index ea6a601..1c1b28b 100644 --- a/spec/jekyll_seo_tag/json_ld_drop_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_drop_spec.rb @@ -157,7 +157,7 @@ RSpec.describe Jekyll::SeoTag::JSONLDDrop do let(:metadata) { {} } it "does not return null values as json" do - expect(subject.to_json).to_not match(/:null/) + expect(subject.to_json).to_not match(%r!:null!) end end end diff --git a/spec/jekyll_seo_tag_integration_spec.rb b/spec/jekyll_seo_tag_integration_spec.rb index 9a13ac7..bfd8bb4 100755 --- a/spec/jekyll_seo_tag_integration_spec.rb +++ b/spec/jekyll_seo_tag_integration_spec.rb @@ -340,7 +340,7 @@ EOS end it "removes null values from JSON-LD" do - expect(output).to_not match(/:null/) + expect(output).to_not match(%r!:null!) end end end