diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index c892895..d51404f 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -87,8 +87,13 @@ module Jekyll alias_method :mainEntityOfPage, :main_entity private :main_entity - def to_json - to_h.compact.to_json + # Returns a JSON-encoded object containing the JSON-LD data. + # Keys are sorted. + def to_json(state = nil) + keys.sort.each_with_object({}) do |(key, _), result| + v = self[key] + result[key] = v unless v.nil? + end.to_json(state) end private diff --git a/spec/jekyll_seo_tag_integration_spec.rb b/spec/jekyll_seo_tag_integration_spec.rb index cde2e77..d7e934e 100755 --- a/spec/jekyll_seo_tag_integration_spec.rb +++ b/spec/jekyll_seo_tag_integration_spec.rb @@ -30,6 +30,10 @@ RSpec.describe Jekyll::SeoTag do expect(output).to match(%r!Jekyll v#{version}!i) end + it "outputs JSON sorted by key" do + expect(json.strip).to eql('{"@context":"https://schema.org","@type":"WebPage","url":"/page.html"}') + end + it "outputs valid HTML" do site.process options = {