Skip to content

Commit 14eeb30

Browse files
committed
Support camelcased Node types
1 parent f71e589 commit 14eeb30

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/prosemirror_to_html/nodes/node.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(node)
1313
end
1414

1515
def matching
16-
return @node.type == self.class.node_type if @node.type
16+
return underscore(@node.type) == self.class.node_type if @node.type
1717

1818
false
1919
end
@@ -29,6 +29,20 @@ def tag
2929
def text
3030
nil
3131
end
32+
33+
private
34+
35+
# from https://github.com/rails/rails/blob/83217025a171593547d1268651b446d3533e2019/activesupport/lib/active_support/inflector/methods.rb#L92
36+
def underscore(camel_cased_word)
37+
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
38+
39+
word = camel_cased_word.to_s.gsub("::", "/")
40+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
41+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
42+
word.tr!("-", "_")
43+
word.downcase!
44+
word
45+
end
3246
end
3347
end
3448
end

spec/nodes/horizontal_rule_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,38 @@
3232
renderer = ProsemirrorToHtml::Renderer.new
3333
expect(renderer.render(json)).to eq html
3434
end
35+
36+
it 'renders correctly with camelCase' do
37+
json = {
38+
type: 'doc',
39+
content: [
40+
{
41+
type: 'paragraph',
42+
content: [
43+
{
44+
type: 'text',
45+
text: 'some text'
46+
},
47+
]
48+
},
49+
{
50+
type: 'horizontalRule'
51+
},
52+
{
53+
type: 'paragraph',
54+
content: [
55+
{
56+
type: 'text',
57+
text: 'some more text'
58+
}
59+
]
60+
}
61+
]
62+
}
63+
64+
html = '<p>some text</p><hr><p>some more text</p>'
65+
66+
renderer = ProsemirrorToHtml::Renderer.new
67+
expect(renderer.render(json)).to eq html
68+
end
3569
end

0 commit comments

Comments
 (0)