Skip to content

Commit

Permalink
Remove key swaps, increase max depth and add _enabled_editors
Browse files Browse the repository at this point in the history
  • Loading branch information
rphillips-nz committed Jul 28, 2021
1 parent 5313647 commit 3707914
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.0.2

* Add `_enabled_editors` to global scope
* Increase max jsonify depth
* Remove unused key swaps

# 2.0.1

* Fix potential for null keys in cc_jsonify filter
Expand Down
7 changes: 5 additions & 2 deletions lib/cloudcannon-jekyll/_cloudcannon/info-2.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% if config.timezone %}
"timezone": {{ config.timezone | cc_jsonify }},
{% endif %}
"collections-config": {{ collections_config | cc_jsonify: 'collections' }},
"collections-config": {{ collections_config | cc_jsonify }},
"collections": {
"drafts": {{ drafts | cc_jsonify }},
"posts": {{ site.posts | reverse | cc_jsonify }}{% if site.collections.size > 0 %},{% endif %}
Expand All @@ -36,6 +36,9 @@
{% if config._comments %}
"_comments": {{ config._comments | cc_jsonify }},
{% endif %}
{% if config._enabled_editors %}
"_enabled_editors": {{ config._enabled_editors | cc_jsonify }},
{% endif %}
{% if config._options %}
"_options": {{ config._options | cc_jsonify }},
{% endif %}
Expand Down Expand Up @@ -63,7 +66,7 @@
"layouts": {{ config.layouts_dir | cc_jsonify }}
},
{% if config._array_structures %}
"_array_structures": {{ config._array_structures | cc_jsonify: nil, 50 }},
"_array_structures": {{ config._array_structures | cc_jsonify: 50 }},
{% endif %}
{% assign select_data = config | cc_select_data_jsonify %}
{% if select_data %}
Expand Down
7 changes: 5 additions & 2 deletions lib/cloudcannon-jekyll/_cloudcannon/info-3.0-4.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% if config.timezone %}
"timezone": {{ config.timezone | cc_jsonify }},
{% endif %}
"collections-config": {{ collections_config | cc_jsonify: 'collections' }},
"collections-config": {{ collections_config | cc_jsonify }},
"collections": {
"drafts": {{ drafts | cc_jsonify }}{% if site.collections.size > 0 %},{% endif %}
{% for collection in site.collections %}"{{ collection.label | xml_escape }}": {{ collection.docs | cc_jsonify }}{% unless forloop.last %},{% endunless %}
Expand All @@ -35,6 +35,9 @@
{% if config._comments %}
"_comments": {{ config._comments | cc_jsonify }},
{% endif %}
{% if config._enabled_editors %}
"_enabled_editors": {{ config._enabled_editors | cc_jsonify }},
{% endif %}
{% if config._options %}
"_options": {{ config._options | cc_jsonify }},
{% endif %}
Expand Down Expand Up @@ -62,7 +65,7 @@
"layouts": {{ config.layouts_dir | cc_jsonify }}
},
{% if config._array_structures %}
"_array_structures": {{ config._array_structures | cc_jsonify: nil, 50 }},
"_array_structures": {{ config._array_structures | cc_jsonify: 50 }},
{% endif %}
{% assign select_data = config | cc_select_data_jsonify %}
{% if select_data %}
Expand Down
7 changes: 5 additions & 2 deletions lib/cloudcannon-jekyll/_cloudcannon/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% if config.timezone -%}
"timezone": {{ config.timezone | cc_jsonify }},
{%- endif %}
"collections-config": {{ collections_config | cc_jsonify: 'collections' }},
"collections-config": {{ collections_config | cc_jsonify }},
"collections": {
"drafts": {{ drafts | cc_jsonify }}{% if site.collections.size > 0 %},{% endif %}
{%- for collection in site.collections -%}
Expand Down Expand Up @@ -48,6 +48,9 @@
{% if config._comments -%}
"_comments": {{ config._comments | cc_jsonify }},
{%- endif %}
{% if config._enabled_editors -%}
"_enabled_editors": {{ config._enabled_editors | cc_jsonify }},
{%- endif %}
{% if config._options -%}
"_options": {{ config._options | cc_jsonify }},
{%- endif %}
Expand Down Expand Up @@ -75,7 +78,7 @@
"layouts": {{ config.layouts_dir | cc_jsonify }}
},
{% if config._array_structures -%}
"_array_structures": {{ config._array_structures | cc_jsonify: nil, 50 }},
"_array_structures": {{ config._array_structures | cc_jsonify: 50 }},
{%- endif %}
{% assign select_data = config | cc_select_data_jsonify -%}
{% if select_data -%}
Expand Down
39 changes: 11 additions & 28 deletions lib/cloudcannon-jekyll/jsonify-filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ module CloudCannonJekyll
module JsonifyFilter
STATIC_EXTENSIONS = [".html", ".htm"].freeze

CC_JSONIFY_KEY_SWAPS = {
"collections" => {
"_sort_key" => "_sort-key",
"_subtext_key" => "_subtext-key",
"_image_key" => "_image-key",
"_image_size" => "_image-size",
"_singular_name" => "_singular-name",
"_singular_key" => "_singular-key",
"_disable_add" => "_disable-add",
"_add_options" => "_add-options",
},
}.freeze

@simple_types = [
String,
Numeric,
Expand Down Expand Up @@ -137,19 +124,19 @@ def self.document_to_json(input, depth, max_depth)
"{#{out.join(",")}}"
end

def self.array_to_json(input, depth, max_depth, key_swaps = {})
def self.array_to_json(input, depth, max_depth)
array = input.map do |value|
JsonifyFilter.to_json(value, depth, max_depth, key_swaps)
JsonifyFilter.to_json(value, depth, max_depth)
end

"[#{array.join(",")}]"
end

def self.hash_to_json(input, depth, max_depth, key_swaps = {})
def self.hash_to_json(input, depth, max_depth)
out = input.map do |key, value|
next_max_depth = key == "_array_structures" ? 20 : max_depth
string_key = (key_swaps[key] || key).to_s.to_json
"#{string_key}: #{JsonifyFilter.to_json(value, depth, next_max_depth, key_swaps)}"
string_key = key.to_s.to_json
"#{string_key}: #{JsonifyFilter.to_json(value, depth, next_max_depth)}"
end

"{#{out.join(",")}}"
Expand All @@ -163,7 +150,7 @@ def self.config_to_select_data_json(input, depth)
baseurl show_dir_listing permalink paginate_path timezone quiet verbose defaults
liquid kramdown title url description uploads_dir _comments _options _editor
_explore _source_editor _array_structures maruku redcloth rdiscount redcarpet
gems plugins cloudcannon _collection_groups)
gems plugins cloudcannon _collection_groups _enabled_editors)

out = input.map do |key, value|
next unless value.is_a?(Array) || value.is_a?(Hash)
Expand All @@ -178,7 +165,7 @@ def self.config_to_select_data_json(input, depth)
"{#{out.join(",")}}" if out.any?
end

def self.to_json(input, depth, max_depth = 9, key_swaps = {})
def self.to_json(input, depth, max_depth = 12)
depth += 1

if depth > max_depth || (depth > 3 && JsonifyFilter.document_type?(input))
Expand All @@ -194,9 +181,9 @@ def self.to_json(input, depth, max_depth = 9, key_swaps = {})
elsif input.is_a?(Jekyll::Document)
JsonifyFilter.document_to_json(input, depth, max_depth)
elsif input.is_a?(Array)
JsonifyFilter.array_to_json(input, depth, max_depth, key_swaps)
JsonifyFilter.array_to_json(input, depth, max_depth)
elsif input.is_a?(Hash)
JsonifyFilter.hash_to_json(input, depth, max_depth, key_swaps)
JsonifyFilter.hash_to_json(input, depth, max_depth)
else
input.class.to_s.prepend("UNSUPPORTED:").to_json
end
Expand All @@ -220,12 +207,8 @@ def cc_select_data_jsonify(input)
end
end

def cc_jsonify(input, key_swaps_key = nil, max_depth = 8)
if CC_JSONIFY_KEY_SWAPS.key? key_swaps_key
JsonifyFilter.to_json(input, 0, max_depth, CC_JSONIFY_KEY_SWAPS[key_swaps_key])
else
JsonifyFilter.to_json(input, 0, max_depth)
end
def cc_jsonify(input, max_depth = 12)
JsonifyFilter.to_json(input, 0, max_depth)
end
end
end
2 changes: 1 addition & 1 deletion lib/cloudcannon-jekyll/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CloudCannonJekyll
VERSION = "2.0.1"
VERSION = "2.0.2"
end
14 changes: 11 additions & 3 deletions spec/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@
expect(staff_members).not_to be_nil
expect(staff_members["output"]).to eq(false)
expect(staff_members["path"]).to eq("_staff_members")
expect(staff_members["_sort-key"]).to eq("name")
expect(staff_members["_singular-name"]).to eq("staff_member")
expect(staff_members["_sort_key"]).to eq("name")
expect(staff_members["_singular_name"]).to eq("staff_member")

empty = collections["empty"]
expect(empty).not_to be_nil
Expand Down Expand Up @@ -468,7 +468,15 @@
"nested2" => {
"nested3" => {
"nested4" => {
"nested5" => { "nested6" => "MAXIMUM_DEPTH" },
"nested5" => {
"nested6" => {
"nested7" => {
"nested8" => {
"nested9" => { "nested10" => "MAXIMUM_DEPTH" },
},
},
},
},
},
},
},
Expand Down

0 comments on commit 3707914

Please sign in to comment.