dato_dast 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ec9fd6604eb5d6466b341c3071baec1ec8936426657459ef3631e1bd8100133
4
- data.tar.gz: dbc4702832122215ba3d6f475b9d5a0ca21a5febdd21bbfc1698913217b80c9c
3
+ metadata.gz: 1b5e7ecc48727c561c2097696d4535dfa553172c06650e61bc0ce880213dc73c
4
+ data.tar.gz: f7b23ba06e1a0821747b2d8cbd284831edc8a4a9e529e14aab759f956d2526f3
5
5
  SHA512:
6
- metadata.gz: eae2e50636e043db21e72bdd69f4a9d9c9473f107ba38e0c1e9498181c3d7012c13ad1500a00cc686e153d7429cdd7d56cf0800d32db23ed8c24b0c6afb9427c
7
- data.tar.gz: b3f5667c3d741a975eea7fb56e08b12985dac0310bc5eec32cc48a5314d3706f320445d8a406543d85403699c7db88a45641d985c7397f50b5cfb0275d516cd4
6
+ metadata.gz: 9f99e282c32c80bd00d2ccab60aa54bc30fae81cef1d8a9589afb1aae0183989e03a246007b53800c833006af390297ba8303c241a7ee7e028203d8f5100e9d2
7
+ data.tar.gz: 7e11c2139df3f9b66e6abd2e850826c4d110d7e90e705a8e23c9849f761f54ba08af1b65975a69ab7f38c21223c096997657799e3ac1774642432b19e4869fa3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.1.0] - 2021-11-10
2
+
3
+ Initial minor release
4
+ - Full functionality now available with inline items
5
+ - Block logic extracted into generic item logic to share with inline items
6
+ - Rename errors to match items instead of blocks
7
+
1
8
  ## [0.0.1] - 2021-11-10
2
9
 
3
10
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dato_dast (0.0.1)
4
+ dato_dast (0.1.0)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -159,6 +159,14 @@ This would use the `<i>` tag instead of `<emphasis>` and wrap that `<i>` tag wit
159
159
 
160
160
  </details>
161
161
 
162
+ ### `config.blocks`
163
+
164
+ The blocks configuration is a hash with a block `item_type` key and the block configuration. See the [Blocks And Inline Items](#blocks_and_inline_items) section for specific details on block configuration.
165
+
166
+ ### `config.inline_items`
167
+
168
+ The inline items configuration is a hash with an inline_item `item_type` key and the inline_item configuration. See the [Blocks And Inline Items](#blocks_and_inline_items) section for specific details on block configuration.
169
+
162
170
  ### `config.types`
163
171
 
164
172
  <details>
@@ -173,13 +181,11 @@ TYPE_CONFIG = {
173
181
  "blockquote" => { "tag" => "blockquote", "node" => Nodes::AttributedQuote },
174
182
  "code" => { "tag" => "code", "node" => Nodes::Code, "wrappers" => ["pre"] },
175
183
  "generic" => { "node" => Nodes::Generic },
176
- "heading" => { "tag" => "h#", "node" => Nodes::Heading },
184
+ "heading" => { "tag" => ->(node) { "h#{node.level}" }, "node" => Nodes::Heading },
185
+ "inlineItem" => { "node" => Nodes::InlineItem },
177
186
  "itemLink" => { "tag" => "a", "node" => Nodes::ItemLink, "url_key" => :slug },
178
187
  "link" => { "tag" => "a", "node" => Nodes::Link },
179
- "list" => {
180
- "bulleted" => { "tag" => "ul", "node" => Nodes::List },
181
- "numbered" => { "tag" => "ol", "node" => Nodes::List },
182
- },
188
+ "list" => { "tag" => ->(node) { node.style == "bulleted" ? "ul" : "ol" }, "node" => Nodes::List },
183
189
  "listItem" => { "tag" => "li", "node" => Nodes::ListItem },
184
190
  "paragraph" => { "tag" => "p", "node" => Nodes::Paragraph },
185
191
  "root" => { "tag" => "div", "node" => Nodes::Root },
@@ -192,7 +198,7 @@ Each type configuration takes the following keys:
192
198
  - `tag`: The default html tag to use. `nil` can be used to not use a key. Additionally you can provide a lambda that takes the Node object.
193
199
  - `node`: This represents the Node object used for rendering. See [Nodes](#nodes) for more details.
194
200
  - `css_class`: This is a string that is used in the `class=""` attribute of the tag. Additionally you can provide a lambda that takes the Node object.
195
- - [ ] `meta`: This is an array of hashes matching the dast meta structure. E.g. Found in the [`link`](https://www.datocms.com/docs/structured-text/dast#link) node. Additionally you can provide a lamdbda that takes the Node object.
201
+ - `meta`: This is an array of hashes matching the dast meta structure. E.g. Found in the [`link`](https://www.datocms.com/docs/structured-text/dast#link) node. Additionally you can provide a lamdbda that takes the Node object.
196
202
  - The structure is `{ "id" => "data-value", "value" => "1"}` renders as `<div data-value="1">`
197
203
  - `wrappers`: This represents additional wrappers use to surrounded the given node type. See [Wrappers](#wrappers) for more details.
198
204
 
@@ -342,6 +348,16 @@ One note about the `Heading` node, is that it accepts a tag with a `#` symbol wh
342
348
 
343
349
  </details>
344
350
 
351
+ ### `inlineItem`
352
+
353
+ <details>
354
+
355
+ Represents the [DatoCMS `inlineItem`](https://www.datocms.com/docs/structured-text/dast#inlineItem) node.
356
+
357
+ Inline items should be configured on a per-item basis. See the [Block and Inline Items](#blocks_and_inline_items) section on how to configure specific inline items.
358
+
359
+ </details>
360
+
345
361
  ### `itemLink`
346
362
 
347
363
  <details>
@@ -643,7 +659,7 @@ This would be rendered using the `ThematicBreak` node, which would render as:
643
659
 
644
660
  ## Wrappers
645
661
 
646
- Marks, Types, and Blocks all supports configuring a `wrappers` field.
662
+ Marks, Types, Inline Items, and Blocks all supports configuring a `wrappers` field.
647
663
 
648
664
  The wrappers are rendered from the outside in, so the first wrapper will wrap the following wrappers.
649
665
 
@@ -702,36 +718,54 @@ end
702
718
 
703
719
  As a result, the thematic break node can't be wrapped nor does it apply a specific tag.
704
720
 
705
- Additionally, the `block` type has a specific render method for the complex rendering that blocks entail.
721
+ Additionally, the `block` and `inlineItem` types have a specific render method for the complex rendering that items entail.
706
722
 
707
723
  </details>
708
724
 
709
- ## Blocks
725
+ ## Blocks and Inline Items
710
726
 
711
- Blocks are the most powerful parts of structured text. We can take objects and render them in a specific way.
727
+ Blocks and Inline Items are the most powerful parts of structured text. We can take DatoCMS objects and render them in a specific way.
712
728
 
713
- Blocks can take the same values as nodes. One difference with blocks is that the block object is provided to the proc instead of the node when a Proc is provided.
729
+ We refer to both blocks and inline items as "items". In the DatoCMS api, objects have an `item_type` and they are referred to instructured texts as `items` so the name seems a good fit.
714
730
 
715
- - `tag`
716
- - `css_class`
717
- - `meta`
731
+ **Configuration**
718
732
 
719
- The block configuration takes `item_type` value and you must provide one of three methods for rendering:
733
+ Blocks and Inline Items take the exact type of configuration, just under a different key:
720
734
 
721
- - `node`
722
- - `render_value`
723
- - `structure`
735
+ ```ruby
736
+ config = {...}
737
+ DatoDast.configure do |config|
738
+ config.block = config
739
+ config.inline_items = config
740
+ end
741
+ ```
742
+
743
+ This is a valid configuration setup. However, blocks are typically a DatoCMS block, while Inline Items are typically a model. Additionally, inline items are rendered...well...inline, so you may want to render something different when it's in block form vs inline.
744
+
745
+ Items can take the same values as nodes. (One difference with items is that the item object is provided to the proc instead of the node when a Proc is provided.)
746
+
747
+ - `tag`: The default html tag to use. `nil` can be used to not use a key. Additionally you can provide a lambda that takes the Node object.
748
+ - `css_class`: This is a string that is used in the `class=""` attribute of the tag. Additionally you can provide a lambda that takes the Node object.
749
+ - `meta`: This is an array of hashes matching the dast meta structure. E.g. Found in the [`link`](https://www.datocms.com/docs/structured-text/dast#link) node. Additionally you can provide a lamdbda that takes the Node object.
750
+ - The structure is `{ "id" => "data-value", "value" => "1"}` renders as `<div data-value="1">`
751
+ - `wrappers`: This represents additional wrappers use to surrounded the given node type. See [Wrappers](#wrappers) for more details.
752
+
753
+ The item configuration takes `item_type` value and you must provide one of three methods for rendering:
754
+
755
+ - `node`: This represents the Node object used for rendering. See [Nodes](#nodes) for more details.
756
+ - `render_value` : This represents a lambda that takes the item as an argument and returns the result of the lambda.
757
+ - `structure`: This is a hash structure for rendering complex setups without needing to use nodes.
724
758
 
725
759
  ### `node`
726
760
 
727
- If you supply the `node` key, you must provide a class that takes the block hash as the only argument for `initialize` and has a `render` function.
761
+ If you supply the `node` key, you must provide a class that takes the item hash as the only argument for `initialize` and has a `render` function.
728
762
 
729
763
  <details>
730
764
 
731
765
  For example:
732
766
 
733
767
  ```ruby
734
- # Let's say we have an (abbreviated) Photo block that has a dato cms image under ":image" and a ":caption" string
768
+ # Let's say we have an (abbreviated) Photo item that has a dato cms image under ":image" and a ":caption" string
735
769
  # {
736
770
  # :id=>"1",
737
771
  # :item_type=>"photo",
@@ -786,12 +820,12 @@ This node would render the following html:
786
820
 
787
821
  ### `render_value`
788
822
 
789
- `render_value` is a simpler form where you can supply a lamba that takes the hash object and it will render the lambda by calling it with the block hash.
823
+ `render_value` is a simpler form where you can supply a lamba that takes the hash object and it will render the lambda by calling it with the item hash.
790
824
 
791
825
  <details>
792
826
 
793
827
  ```ruby
794
- # Using the following block hash
828
+ # Using the following item hash
795
829
  # {
796
830
  # :id=>"1",
797
831
  # :item_type=>"photo",
@@ -803,9 +837,9 @@ This node would render the following html:
803
837
  # }
804
838
 
805
839
  DatoDast.configure do |config|
806
- config.blocks = {
840
+ config.inline_items = {
807
841
  "photo" => {
808
- "render_value" => ->(block) { "<img src='#{block[:url]}' />" },
842
+ "render_value" => ->(item) { "<img src='#{item[:url]}' />" },
809
843
  }
810
844
  }
811
845
  end
@@ -821,16 +855,16 @@ This would render the following html:
821
855
 
822
856
  ### `structure`
823
857
 
824
- The most powerful part of `DatoDast` is the `structure` tools for rendering blocks.
858
+ The most powerful part of `DatoDast` is the `structure` tools for rendering items.
825
859
 
826
- The `structure` configuration can be used on nested blocks or relationships to construct multiple tags.
860
+ The `structure` configuration can be used on nested items or relationships to construct multiple tags.
827
861
 
828
862
  The `structure` format is an array of hashes each with a "type" field. The "type" can be one of four values:
829
863
 
830
864
  - `"field"`
831
865
  - `"value"`
832
- - `"block"`
833
- - `"blocks"`
866
+ - `"item"`
867
+ - `"items"`
834
868
 
835
869
  ### `field`
836
870
 
@@ -881,7 +915,7 @@ Would render the following html:
881
915
 
882
916
  <details>
883
917
 
884
- When the type is `"value"`, then you also must provide a `"render_value"` function that takes the block hash as an argument and returns a string.
918
+ When the type is `"value"`, then you also must provide a `"render_value"` function that takes the item hash as an argument and returns a string.
885
919
 
886
920
  ```ruby
887
921
  # With the object
@@ -905,7 +939,7 @@ DatoDast.configure do |config|
905
939
  "type" => "value",
906
940
  "tag" => "span",
907
941
  "css_class" => "blue",
908
- "render_value" => ->(block) { block[:caption] },
942
+ "render_value" => ->(item) { item[:caption] },
909
943
  },
910
944
  ],
911
945
  },
@@ -922,14 +956,14 @@ Would render the following html:
922
956
  ```
923
957
  </details>
924
958
 
925
- ### `block`
959
+ ### `item`
926
960
 
927
961
  <details>
928
962
 
929
- When the type is `"block"`, then you also must provide a `"field"` value that specifies the field which contains another block. That block will the be rendered using the some block configuration.
963
+ When the type is `"item"`, then you also must provide a `"field"` value that specifies the field which contains another item. That item will the be rendered using the same item configuration.
930
964
 
931
965
  ```ruby
932
- # Let's imagine a card object with photo block relationship and caption
966
+ # Let's imagine a card object with photo item relationship and caption
933
967
  # {
934
968
  # :id=>"2",
935
969
  # :item_type => "card",
@@ -946,7 +980,7 @@ When the type is `"block"`, then you also must provide a `"field"` value that sp
946
980
  # :caption=>"My Logo",
947
981
  # }
948
982
  DatoDast.configure do |config|
949
- config.blocks = {
983
+ config.inline_items = {
950
984
  "card" => {
951
985
  "wrappers" => {
952
986
  "tag" => "div",
@@ -956,9 +990,9 @@ DatoDast.configure do |config|
956
990
  "value" => "1",
957
991
  }],
958
992
  },
959
- "structure" => []
993
+ "structure" => [
960
994
  {
961
- "type" => "block",
995
+ "type" => "item",
962
996
  "field" => "photo",
963
997
  },
964
998
  {
@@ -971,7 +1005,7 @@ DatoDast.configure do |config|
971
1005
  "photo" => {
972
1006
  "tag" => "div",
973
1007
  "css_class" => "img",
974
- "render_value" => ->(block) { "<img src='#{block[:url]}' />" }
1008
+ "render_value" => ->(item) { "<img src='#{item[:url]}' />" }
975
1009
  }
976
1010
  }
977
1011
  end
@@ -990,14 +1024,14 @@ Would render the following html:
990
1024
 
991
1025
  </details>
992
1026
 
993
- ### `blocks`
1027
+ ### `items`
994
1028
 
995
1029
  <details>
996
1030
 
997
- When the type is `"blocks"`, then you also must provide a `"field"` value that specifies the field which contains an array of blocks. That block will the be rendered using the some block configuration.
1031
+ When the type is `"items"`, then you also must provide a `"field"` value that specifies the field which contains an array of items. That item will the be rendered using the some item configuration.
998
1032
 
999
1033
  ```ruby
1000
- # Let's imagine a card object with photo block relationship and caption
1034
+ # Let's imagine a card object with photo item relationship and caption
1001
1035
  # {
1002
1036
  # :id=>"2",
1003
1037
  # :item_type => "card",
@@ -1038,7 +1072,7 @@ DatoDast.configure do |config|
1038
1072
  },
1039
1073
  "structure" => []
1040
1074
  {
1041
- "type" => "block",
1075
+ "type" => "items",
1042
1076
  "field" => "gallery",
1043
1077
  "tag" => "div",
1044
1078
  "css_class" => "gallery",
@@ -1053,7 +1087,7 @@ DatoDast.configure do |config|
1053
1087
  "photo" => {
1054
1088
  "tag" => "div",
1055
1089
  "css_class" => "img",
1056
- "render_value" => ->(block) { "<img src='#{block[:url]}' />" }
1090
+ "render_value" => ->(item) { "<img src='#{item[:url]}' />" }
1057
1091
  }
1058
1092
  }
1059
1093
  end
@@ -1079,4 +1113,4 @@ Would render the following html:
1079
1113
 
1080
1114
  ## Contributing
1081
1115
 
1082
- Bug reports and pull requests are welcome on GitHub at https://github.com/[dewyze]/dato_dast.
1116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dewyze/dato_dast.
@@ -8,6 +8,7 @@ module DatoDast
8
8
  Nodes::Code.type => { "tag" => "code", "node" => Nodes::Code, "wrappers" => ["pre"] },
9
9
  Nodes::Generic.type => { "node" => Nodes::Generic },
10
10
  Nodes::Heading.type => { "tag" => ->(node) { "h#{node.level}" }, "node" => Nodes::Heading },
11
+ Nodes::InlineItem.type => { "node" => Nodes::InlineItem },
11
12
  Nodes::ItemLink.type => { "tag" => "a", "node" => Nodes::ItemLink, "url_key" => :slug },
12
13
  Nodes::Link.type => { "tag" => "a", "node" => Nodes::Link },
13
14
  Nodes::List.type => { "tag" => ->(node) { node.style == "bulleted" ? "ul" : "ol" }, "node" => Nodes::List },
@@ -27,13 +28,14 @@ module DatoDast
27
28
  Marks::UNDERLINE => { "tag" => "u" },
28
29
  }.freeze
29
30
 
30
- BLOCK_RENDER_KEYS = ["node", "render_value", "structure"].freeze
31
+ ITEM_RENDER_KEYS = ["node", "render_value", "structure"].freeze
31
32
 
32
- attr_reader :blocks, :host, :marks, :types
33
+ attr_reader :blocks, :host, :inline_items, :marks, :types
33
34
  attr_accessor :highlight, :item_links, :smart_links
34
35
 
35
36
  def initialize
36
37
  @blocks = {}
38
+ @inline_items = {}
37
39
  @highlight = true
38
40
  @host = nil
39
41
  @item_links = {}
@@ -53,11 +55,17 @@ module DatoDast
53
55
  end
54
56
 
55
57
  def blocks=(new_blocks)
56
- validate_blocks_configuration(new_blocks)
58
+ validate_items_configuration(new_blocks, "blocks")
57
59
 
58
60
  @blocks = new_blocks
59
61
  end
60
62
 
63
+ def inline_items=(new_inline_items)
64
+ validate_items_configuration(new_inline_items, "inline_items")
65
+
66
+ @inline_items = new_inline_items
67
+ end
68
+
61
69
  def marks=(new_marks)
62
70
  validate_marks_configuration(new_marks)
63
71
 
@@ -78,17 +86,17 @@ module DatoDast
78
86
 
79
87
  private
80
88
 
81
- def validate_blocks_configuration(blocks_config)
82
- invalid_blocks = []
89
+ def validate_items_configuration(items_config, type)
90
+ invalid_items = []
83
91
 
84
- blocks_config.each do |block, block_config|
85
- next if block_config.is_a?(Proc)
92
+ items_config.each do |item, item_config|
93
+ next if item_config.is_a?(Proc)
86
94
 
87
- intersection = block_config.keys & BLOCK_RENDER_KEYS
88
- invalid_blocks << block unless intersection.length == 1
95
+ intersection = item_config.keys & ITEM_RENDER_KEYS
96
+ invalid_items << "#{type}->#{item}" unless intersection.length == 1
89
97
  end
90
98
 
91
- raise Errors::InvalidBlocksConfiguration.new(invalid_blocks) if invalid_blocks.present?
99
+ raise Errors::InvalidItemsConfiguration.new(invalid_items) if invalid_items.present?
92
100
  end
93
101
 
94
102
  def validate_types(types_config)
@@ -1,10 +1,10 @@
1
1
  module DatoDast
2
2
  module Errors
3
- class BlockFieldMissing < StandardError
3
+ class FieldMissing < StandardError
4
4
  MESSAGE = <<~MSG.strip
5
5
  A structure type of 'field' requires the block to have the specified field.
6
6
 
7
- The following block configuration is invalid:
7
+ The following configuration is invalid:
8
8
  MSG
9
9
 
10
10
  def initialize(item_type)
@@ -1,13 +1,13 @@
1
1
  module DatoDast
2
2
  module Errors
3
- class InvalidBlocksConfiguration < StandardError
3
+ class InvalidItemsConfiguration < StandardError
4
4
  MESSAGE = <<~MSG.strip
5
- A block configuration requires exactly one of the following keys:
5
+ An item configuration requires exactly one of the following keys:
6
6
  - "node"
7
7
  - "render_value"
8
8
  - "structure"
9
9
 
10
- The following block configurations are invalid:
10
+ The following configurations are invalid:
11
11
  MSG
12
12
 
13
13
  def initialize(keys)
@@ -0,0 +1,6 @@
1
+ module DatoDast
2
+ module Errors
3
+ class InvalidStructureType < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module DatoDast
2
+ module Errors
3
+ class MissingItemConfiguration < StandardError
4
+ MESSAGE = <<~MSG.strip
5
+ An configuration must be provided for every item type.
6
+
7
+ The configuration for the following item type is invalid:
8
+ MSG
9
+
10
+ def initialize(key)
11
+ super(MESSAGE + " " + key)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module DatoDast
2
+ module Errors
3
+ class MissingRenderFunction < StandardError
4
+ MESSAGE = <<~MSG.strip
5
+ A node class provided for an item must have a 'render' method.
6
+
7
+ The node object for the following item type is invalid:
8
+ MSG
9
+
10
+ def initialize(keys)
11
+ super(MESSAGE + " " + keys.join(", "))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,7 +4,7 @@ module DatoDast
4
4
  MESSAGE = <<~MSG.strip
5
5
  A structure type of 'value' requires a render value function.
6
6
 
7
- The following block configuration is invalid:
7
+ The following configuration is invalid:
8
8
  MSG
9
9
 
10
10
  def initialize(item_type)
@@ -1,8 +1,9 @@
1
- require "dato_dast/errors/block_field_missing"
2
- require "dato_dast/errors/block_node_missing_render_function"
3
- require "dato_dast/errors/invalid_block_structure_type"
4
- require "dato_dast/errors/invalid_blocks_configuration"
1
+ require "dato_dast/errors/field_missing"
2
+ require "dato_dast/errors/invalid_items_configuration"
5
3
  require "dato_dast/errors/invalid_marks_configuration"
6
4
  require "dato_dast/errors/invalid_nodes"
5
+ require "dato_dast/errors/invalid_structure_type"
7
6
  require "dato_dast/errors/invalid_types_configuration"
7
+ require "dato_dast/errors/missing_item_configuration"
8
+ require "dato_dast/errors/missing_render_function"
8
9
  require "dato_dast/errors/missing_render_value_function"
@@ -6,6 +6,7 @@ module DatoDast
6
6
  option :blocks, {}, "Configuration hash for blocks"
7
7
  option :host, nil, "Host for your site, used in conjunction with 'smart_links' option"
8
8
  option :highlight, true, "Toggle whether to attempt to higlight code blocks"
9
+ option :inline_items, {}, "Configuration hash for inlineItems"
9
10
  option :item_links, {}, "Configuration hash item links types and the url field"
10
11
  option :marks, {}, "Configuration hash for a given mark"
11
12
  option :smart_links, true, "Open Link items in new windows and ItemLinks in the same window"
@@ -16,6 +17,7 @@ module DatoDast
16
17
  config.blocks = options[:blocks]
17
18
  config.highlight = options[:highlight]
18
19
  config.host = options[:host]
20
+ config.inline_items = options[:inline_items]
19
21
  config.item_links = options[:item_links]
20
22
  config.marks = options[:marks]
21
23
  config.smart_links = options[:smart_links]
@@ -1,124 +1,13 @@
1
1
  module DatoDast
2
2
  module Nodes
3
- class Block < Base
4
- def block_id
5
- @node["item"]
6
- end
7
-
8
- def block
9
- return @block if @block
10
-
11
- @block ||= @blocks.find { |block| block[:id] == block_id }
12
- end
13
-
14
- def item_type
15
- block[:item_type]
16
- end
17
-
18
- def node
19
- node_config["node"]
3
+ class Block < Item
4
+ def item_repo
5
+ @blocks
20
6
  end
21
7
 
22
8
  def node_config
23
9
  config.blocks[item_type]
24
10
  end
25
-
26
- def children
27
- Array.wrap(structure).map do |child|
28
- case child["type"]
29
- when "field"
30
- build_field(child)
31
- when "value"
32
- build_value(child)
33
- when "block"
34
- build_block(block[child["field"].to_sym])
35
- when "blocks"
36
- build_blocks(child)
37
- else
38
- raise Errors::InvalidBlockStructureType
39
- end.merge(extract_tags(child)).compact
40
- end.flatten
41
- end
42
-
43
- def render_value
44
- if node_config["render_value"]
45
- node_config["render_value"].call(block)
46
- else
47
- render_children
48
- end
49
- end
50
-
51
- def render
52
- return super unless node
53
-
54
- begin
55
- node.new(block).render
56
- rescue NoMethodError => _e
57
- raise Errors::BlockNodeMissingRenderFunction.new(["block->#{item_type}"])
58
- end
59
- end
60
-
61
- private
62
-
63
- def structure
64
- node_config["structure"]
65
- end
66
-
67
- def extract_tags(child)
68
- {
69
- "tag" => child["tag"],
70
- "css_class" => child["css_class"],
71
- "meta" => child["meta"],
72
- "wrappers" => child["wrappers"],
73
- }.compact
74
- end
75
-
76
- def build_field(child)
77
- raise Errors::BlockFieldMissing.new(item_type) unless child["field"]
78
-
79
- field = child["field"].to_sym
80
- value = block[field]
81
-
82
- {
83
- "type" => "span",
84
- "value" => value,
85
- "marks" => Array.wrap(child["marks"]),
86
- }
87
- end
88
-
89
- def build_value(child)
90
- raise Errors::MissingRenderValueFunction.new(item_type) unless child["render_value"]
91
-
92
- value = child["render_value"].call(block)
93
-
94
- {
95
- "type" => "span",
96
- "value" => value,
97
- "marks" => Array.wrap(child["marks"]),
98
- }
99
- end
100
-
101
- def build_block(child)
102
- @blocks << child
103
-
104
- {
105
- "type" => "block",
106
- "item" => child[:id],
107
- }
108
- end
109
-
110
- def build_blocks(child)
111
- field = child["field"].to_sym
112
-
113
- parent = {
114
- "type" => "generic",
115
- "children" => Array.wrap(block[field]).map { |child_block| build_block(child_block) }
116
- }
117
- end
118
-
119
- def proc_object
120
- block
121
- end
122
11
  end
123
12
  end
124
13
  end
@@ -1,14 +1,13 @@
1
- # An inlineItem, similarly to itemLink, links the document to another record but does not specify any inner content (children).
2
- #
3
- # It can be used in situations where it is up to the frontend to decide how to present the record (ie. a widget, or an <a> tag pointing to the URL of the record with a text that is the title of the linked record).
4
- #
5
- # It does not allow children nodes.
6
- #
7
- # type "inlineItem" Required
8
- # item string Required
9
- # The DatoCMS record ID
10
- #
11
- # {
12
- # "type": "inlineItem",
13
- # "item": "74619345"
14
- # }
1
+ module DatoDast
2
+ module Nodes
3
+ class InlineItem < Item
4
+ def item_repo
5
+ @links
6
+ end
7
+
8
+ def node_config
9
+ config.inline_items[item_type]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,138 @@
1
+ module DatoDast
2
+ module Nodes
3
+ class Item < Base
4
+ def item_id
5
+ @node["item"]
6
+ end
7
+
8
+ def item
9
+ return @item if @item
10
+
11
+ @item ||= item_repo.find { |item| item[:id] == item_id }
12
+ end
13
+
14
+ def item_type
15
+ item[:item_type]
16
+ end
17
+
18
+ def node
19
+ raise DatoDast::Errors::MissingItemConfiguration.new(error_label) unless node_config
20
+
21
+ node_config["node"]
22
+ end
23
+
24
+ def children
25
+ Array.wrap(structure).map do |child|
26
+ case child["type"]
27
+ when "field"
28
+ build_field(child)
29
+ when "value"
30
+ build_value(child)
31
+ when "item"
32
+ build_item(item[child["field"].to_sym])
33
+ when "items"
34
+ build_items(child)
35
+ else
36
+ raise DatoDast::Errors::InvalidStructureType.new(error_label)
37
+ end.merge(extract_tags(child)).compact
38
+ end.flatten
39
+ end
40
+
41
+ def render_value
42
+ if node_config["render_value"]
43
+ node_config["render_value"].call(item)
44
+ else
45
+ render_children
46
+ end
47
+ end
48
+
49
+ def render
50
+ return super unless node
51
+
52
+ begin
53
+ node.new(item).render
54
+ rescue NoMethodError => _e
55
+ raise DatoDast::Errors::MissingRenderFunction.new([error_label])
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def label
62
+ @node["type"]
63
+ end
64
+
65
+ def error_label
66
+ "#{label}->#{item_type}"
67
+ end
68
+
69
+ def item_repo
70
+ raise NotImplementedError
71
+ end
72
+
73
+ def node_config
74
+ raise NotImplementedError
75
+ end
76
+
77
+ def structure
78
+ node_config["structure"]
79
+ end
80
+
81
+ def extract_tags(child)
82
+ {
83
+ "tag" => child["tag"],
84
+ "css_class" => child["css_class"],
85
+ "meta" => child["meta"],
86
+ "wrappers" => child["wrappers"],
87
+ }.compact
88
+ end
89
+
90
+ def build_field(child)
91
+ raise DatoDast::Errors::FieldMissing.new(error_label) unless child["field"]
92
+
93
+ field = child["field"].to_sym
94
+ value = item[field]
95
+
96
+ {
97
+ "type" => "span",
98
+ "value" => value,
99
+ "marks" => Array.wrap(child["marks"]),
100
+ }
101
+ end
102
+
103
+ def build_value(child)
104
+ raise Errors::MissingRenderValueFunction.new(error_label) unless child["render_value"]
105
+
106
+ value = child["render_value"].call(item)
107
+
108
+ {
109
+ "type" => "span",
110
+ "value" => value,
111
+ "marks" => Array.wrap(child["marks"]),
112
+ }
113
+ end
114
+
115
+ def build_item(child)
116
+ item_repo << child
117
+
118
+ {
119
+ "type" => label,
120
+ "item" => child[:id],
121
+ }
122
+ end
123
+
124
+ def build_items(child)
125
+ field = child["field"].to_sym
126
+
127
+ parent = {
128
+ "type" => "generic",
129
+ "children" => Array.wrap(item[field]).map { |child_item| build_item(child_item) }
130
+ }
131
+ end
132
+
133
+ def proc_object
134
+ item
135
+ end
136
+ end
137
+ end
138
+ end
@@ -1,4 +1,5 @@
1
1
  require "dato_dast/nodes/base"
2
+ require "dato_dast/nodes/item"
2
3
  require "dato_dast/nodes/span"
3
4
  require "dato_dast/nodes/generic"
4
5
  require "dato_dast/nodes/block"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatoDast
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dato_dast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John DeWyze
@@ -62,13 +62,14 @@ files:
62
62
  - lib/dato_dast.rb
63
63
  - lib/dato_dast/configuration.rb
64
64
  - lib/dato_dast/errors.rb
65
- - lib/dato_dast/errors/block_field_missing.rb
66
- - lib/dato_dast/errors/block_node_missing_render_function.rb
67
- - lib/dato_dast/errors/invalid_block_structure_type.rb
68
- - lib/dato_dast/errors/invalid_blocks_configuration.rb
65
+ - lib/dato_dast/errors/field_missing.rb
66
+ - lib/dato_dast/errors/invalid_items_configuration.rb
69
67
  - lib/dato_dast/errors/invalid_marks_configuration.rb
70
68
  - lib/dato_dast/errors/invalid_nodes.rb
69
+ - lib/dato_dast/errors/invalid_structure_type.rb
71
70
  - lib/dato_dast/errors/invalid_types_configuration.rb
71
+ - lib/dato_dast/errors/missing_item_configuration.rb
72
+ - lib/dato_dast/errors/missing_render_function.rb
72
73
  - lib/dato_dast/errors/missing_render_value_function.rb
73
74
  - lib/dato_dast/extensions/middleman.rb
74
75
  - lib/dato_dast/html_tag.rb
@@ -82,6 +83,7 @@ files:
82
83
  - lib/dato_dast/nodes/generic.rb
83
84
  - lib/dato_dast/nodes/heading.rb
84
85
  - lib/dato_dast/nodes/inline_item.rb
86
+ - lib/dato_dast/nodes/item.rb
85
87
  - lib/dato_dast/nodes/item_link.rb
86
88
  - lib/dato_dast/nodes/link.rb
87
89
  - lib/dato_dast/nodes/list.rb
@@ -1,15 +0,0 @@
1
- module DatoDast
2
- module Errors
3
- class BlockNodeMissingRenderFunction < StandardError
4
- MESSAGE = <<~MSG.strip
5
- A node class provided for a block must have a 'render' method.
6
-
7
- The node object for the following block item type is invalid:
8
- MSG
9
-
10
- def initialize(keys)
11
- super(MESSAGE + " " + keys.join(", "))
12
- end
13
- end
14
- end
15
- end
@@ -1,6 +0,0 @@
1
- module DatoDast
2
- module Errors
3
- class InvalidBlockStructureType < StandardError
4
- end
5
- end
6
- end