"
+ in type: 'orderedList', content:
+ "#{children(content, tags, level + 1)}"
+ in type: 'listItem', content:
+ "
#{children(content, tags, level + 1)}
"
+ in type: 'text', text:, **rest
+ if rest[:marks].present?
+ apply_marks(text, rest[:marks])
else
- ""
+ text
end
+ in type: 'mention', attrs: { id: }, **rest
+ if rest[:marks].present?
+ apply_marks(tags[id], rest[:marks])
+ else
+ tags[id]
+ end
+ in { type: type } if ["paragraph", "title", "heading"].include?(type) && !node.key?(:content)
+ # noop
end
+ end
- def apply_marks(text, marks)
- marks.reduce(text) do |text, mark|
- case mark
- in type: 'bold'
- "#{text}"
- in type: 'italic'
- "#{text}"
- in type: 'underline'
- "#{text}"
- in type: 'strike'
- "#{text}"
- in type: 'highlight'
- "#{text}"
- end
+ def text_align(attrs)
+ if attrs.present? && attrs[:textAlign].present?
+ " style=\"text-align: #{attrs[:textAlign]}\""
+ else
+ ""
+ end
+ end
+
+ def apply_marks(text, marks)
+ marks.reduce(text) do |text, mark|
+ case mark
+ in type: 'bold'
+ "#{text}"
+ in type: 'italic'
+ "#{text}"
+ in type: 'underline'
+ "#{text}"
+ in type: 'strike'
+ "#{text}"
+ in type: 'highlight'
+ "#{text}"
end
end
end
diff --git a/spec/services/tiptap_service_spec.rb b/spec/services/tiptap_service_spec.rb
index bb3c1587a..37216b664 100644
--- a/spec/services/tiptap_service_spec.rb
+++ b/spec/services/tiptap_service_spec.rb
@@ -25,18 +25,28 @@ RSpec.describe TiptapService do
type: 'title' # remained empty in editor
},
{
- type: 'paragraph',
- attrs: { textAlign: 'right' },
- content: [
- {
- type: 'text',
- text: 'Hello world!'
- }
- ]
+ type: 'heading',
+ attrs: { level: 1 },
+ content: [{ type: 'text', text: 'Heading 1' }]
},
{
- type: 'paragraph'
- # no content, empty line
+ type: 'heading',
+ attrs: { level: 2, textAlign: 'center' },
+ content: [{ type: 'text', text: 'Heading 2' }]
+ },
+ {
+ type: 'heading',
+ attrs: { level: 3 },
+ content: [{ type: 'text', text: 'Heading 3' }]
+ },
+ {
+ type: 'heading',
+ attrs: { level: 3 } # remained empty in editor
+ },
+ {
+ type: 'paragraph',
+ attrs: { textAlign: 'right' },
+ content: [{ type: 'text', text: 'First paragraph' }]
},
{
type: 'paragraph',
@@ -63,23 +73,8 @@ RSpec.describe TiptapService do
]
},
{
- type: 'heading',
- attrs: { level: 1 },
- content: [{ type: 'text', text: 'Heading 1' }]
- },
- {
- type: 'heading',
- attrs: { level: 2, textAlign: 'center' },
- content: [{ type: 'text', text: 'Heading 2' }]
- },
- {
- type: 'heading',
- attrs: { level: 3 },
- content: [{ type: 'text', text: 'Heading 3' }]
- },
- {
- type: 'heading',
- attrs: { level: 3 } # remained empty in editor
+ type: 'paragraph'
+ # no content, empty line
},
{
type: 'bulletList',
@@ -157,13 +152,13 @@ RSpec.describe TiptapService do
let(:tags) { { 'name' => 'Paul' } }
let(:html) do
[
- '
Left
Right
',
+ '
Left
Right
',
'
Title
',
- '
Hello world!
',
- '
Bonjour Paul !
',
'
Heading 1
',
'
Heading 2
',
'
Heading 3
',
+ '
First paragraph
',
+ '
Bonjour Paul !
',
'
Item 1
Item 2
',
'
Item 1
Item 2
',
''
@@ -171,7 +166,7 @@ RSpec.describe TiptapService do
end
it 'returns html' do
- expect(described_class.to_html(json, tags)).to eq(html)
+ expect(described_class.new.to_html(json, tags)).to eq(html)
end
end
end