trek 0.1.14 → 0.1.15

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: 3cb0d4043c637f6146abea51141e76c51b0ebd6cc4dac523445d902473b95177
4
- data.tar.gz: b0fc30e95b128e3cd3728c81d24a6e35ee2300ed56b14f18027248e2c15a3030
3
+ metadata.gz: 6b09da98ecaf56b2d61965f064a51258bc84dced5267977541a810f33416ee2f
4
+ data.tar.gz: ad5b3dfc7aaaeef7c8d996fd7ba89ee3f5057c5b93d9c40bcba859c37035b2d0
5
5
  SHA512:
6
- metadata.gz: 174c1802c47d012db7287e22dd3f67873d5542f271d8d4caf539e7519cea9344feb1eb6c2bc6ba7b015e94fdc63fa2b992a6c3ca8207a97b76f7cd819a05dae4
7
- data.tar.gz: e7b8ddced93643490fb19c423c71c0996e6ed2782ac2e7e26a522b3d0a4026cc0bf84c8a5b0072e8f3e88d4a64461e218b723631a266a9a3a6214b53721fb04c
6
+ metadata.gz: ee62982045c3a349abfe75033fe60731cffb498408af1c6375afde95ef4477a7476f8781c4a206f72d4e04565e886165b652c9da488d0f8c3eb97c0a2c9b256b
7
+ data.tar.gz: dcb6b91baad0d783cefcf01c3afb15b54292d105844c291738e2a5972d96905c3c23e6d83435b35a78d0857eb7c12088d01ed21de3a595e8677002c26e214a29
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trek (0.1.14)
4
+ trek (0.1.15)
5
5
  action_policy (~> 0.6)
6
6
  actioncable
7
7
  acts_as_list (~> 1.1)
@@ -295,9 +295,9 @@ GEM
295
295
  net-smtp (0.5.1)
296
296
  net-protocol
297
297
  nio4r (2.7.5)
298
- nokogiri (1.19.0-arm64-darwin)
298
+ nokogiri (1.19.1-arm64-darwin)
299
299
  racc (~> 1.4)
300
- nokogiri (1.19.0-x86_64-darwin)
300
+ nokogiri (1.19.1-x86_64-darwin)
301
301
  racc (~> 1.4)
302
302
  parallel (1.27.0)
303
303
  parser (3.3.10.0)
@@ -313,7 +313,7 @@ GEM
313
313
  prettyprint
314
314
  prettyprint (0.2.0)
315
315
  prism (1.7.0)
316
- prosemirror_to_html (0.2.1)
316
+ prosemirror_to_html (0.3.0)
317
317
  nokogiri
318
318
  psych (5.3.1)
319
319
  date
@@ -7,6 +7,6 @@ class TrekTaggings < <%= migration_class_name %>
7
7
  t.timestamps
8
8
  end
9
9
 
10
- add_index :taggings, [:taggable_id, :tag_id], unique: true
10
+ add_index :taggings, %i[taggable_type taggable_id tag_id], unique: true
11
11
  end
12
12
  end
@@ -2,28 +2,60 @@ module Trek
2
2
  # `TypographyFormatter` formats strings to enforce typography rules.
3
3
  # For now, only French typography rules about non-breaking spaces
4
4
  # and ellipsis are implemented.
5
+ #
6
+ # Supports both plain strings and ProseMirror JSONB hashes. When given
7
+ # a Hash, it walks the node tree and formats only `text` node values
8
+ # in place, leaving the structure intact.
5
9
  class TypographyFormatter
6
10
  NBSP = [160].pack("U*").freeze
7
11
  NBSP_BEFORE_CHARS = %w[: ; % € \$ °C °F » ! \? –].freeze
8
12
  NBSP_AFTER_CHARS = %w[«].freeze
9
13
  NBSP_BEFORE_UNITS = %w[g h ha kg km Ko ko kW L l m min ml Mo mo s].freeze
10
14
 
11
- def initialize(str)
12
- @source = str
13
- @output = str.dup
15
+ def initialize(input)
16
+ @source = input
17
+ @prosemirror = input.is_a?(Hash)
18
+ @output = @prosemirror ? deep_dup_hash(input) : input.dup
14
19
  end
15
20
 
16
21
  def formatted
17
22
  return @output if @output.nil?
18
23
 
24
+ if @prosemirror
25
+ format_prosemirror_node(@output)
26
+ else
27
+ format_string!
28
+ end
29
+
30
+ @output
31
+ end
32
+
33
+ private
34
+
35
+ def format_string!
19
36
  remove_double_spaces
20
37
  add_nbsp
21
38
  replace_ellipsis
39
+ end
22
40
 
23
- @output
41
+ def format_prosemirror_node(node)
42
+ return unless node.is_a?(Hash)
43
+
44
+ if node["type"] == "text" && node["text"].is_a?(String)
45
+ formatter = self.class.new(node["text"])
46
+ node["text"] = formatter.formatted
47
+ end
48
+
49
+ Array(node["content"]).each { |child| format_prosemirror_node(child) }
24
50
  end
25
51
 
26
- private
52
+ def deep_dup_hash(obj)
53
+ case obj
54
+ when Hash then obj.transform_values { |v| deep_dup_hash(v) }
55
+ when Array then obj.map { |v| deep_dup_hash(v) }
56
+ else obj.dup rescue obj
57
+ end
58
+ end
27
59
 
28
60
  def remove_double_spaces
29
61
  @output.gsub!(/(\p{Zs}{2,})/, " ")
data/lib/trek/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trek
4
- VERSION = "0.1.14"
4
+ VERSION = "0.1.15"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etaminstudio/trek",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "A modern CMS for Ruby on Rails",
5
5
  "main": "app/javascript/trek.js",
6
6
  "repository": {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trek
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Bengrich