mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-13 15:58:50 +00:00
Add schema.org markup to SEO-enabled posts (#36075)
This commit is contained in:
16
app/lib/seo/adapter.rb
Normal file
16
app/lib/seo/adapter.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SEO::Adapter < ActiveModelSerializers::Adapter::Base
|
||||
def self.default_key_transform
|
||||
:camel_lower
|
||||
end
|
||||
|
||||
def self.transform_key_casing!(value, _options)
|
||||
SEO::CaseTransform.camel_lower(value)
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
serialized_hash = serializer.serializable_hash(serialization_options(options))
|
||||
self.class.transform_key_casing!(serialized_hash, instance_options)
|
||||
end
|
||||
end
|
||||
36
app/lib/seo/case_transform.rb
Normal file
36
app/lib/seo/case_transform.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module SEO::CaseTransform
|
||||
PREFIX_KEYS = %w(
|
||||
context
|
||||
id
|
||||
type
|
||||
).freeze
|
||||
|
||||
class << self
|
||||
def camel_lower_cache
|
||||
@camel_lower_cache ||= {}
|
||||
end
|
||||
|
||||
def camel_lower(value)
|
||||
case value
|
||||
when Array
|
||||
value.map { |item| camel_lower(item) }
|
||||
when Hash
|
||||
value.deep_transform_keys! { |key| camel_lower(key) }
|
||||
when Symbol
|
||||
camel_lower(value.to_s).to_sym
|
||||
when String
|
||||
camel_lower_cache[value] ||= begin
|
||||
if PREFIX_KEYS.include?(value.to_s)
|
||||
"@#{value}"
|
||||
else
|
||||
value.underscore.camelize(:lower)
|
||||
end
|
||||
end
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user