Sort JSON-LD data by key (#458)

Merge pull request 458
This commit is contained in:
Parker Moore 2022-02-04 08:17:52 -08:00 committed by GitHub
parent 8303d31d79
commit 54d37a8396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -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

View File

@ -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 = {