Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort JSON-LD data by key #458

Merged
merged 3 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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