Skip to content

Commit

Permalink
Sort JSON-LD data by key (#458)
Browse files Browse the repository at this point in the history
Merge pull request 458
  • Loading branch information
parkr committed Feb 4, 2022
1 parent 8303d31 commit 54d37a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/jekyll-seo-tag/json_ld_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ def main_entity
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
Expand Down
4 changes: 4 additions & 0 deletions spec/jekyll_seo_tag_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
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 = {
Expand Down

0 comments on commit 54d37a8

Please sign in to comment.