diff --git a/app/models/export_item.rb b/app/models/export_item.rb index 8ee6e4323..294946753 100644 --- a/app/models/export_item.rb +++ b/app/models/export_item.rb @@ -21,14 +21,14 @@ class ExportItem def template_json = template.to_json - def template_string = TiptapService.new.to_path(template) + def template_string = TiptapService.new.to_texts_and_tags(template) def path(dossier, attachment: nil, row_index: nil, index: nil) used_tags = TiptapService.used_tags_and_libelle_for(template) substitutions = tags_substitutions(used_tags, dossier, escape: false, memoize: true) substitutions['original-filename'] = attachment.filename.base if attachment - TiptapService.new.to_path(template, substitutions) + suffix(attachment, row_index, index) + TiptapService.new.to_texts_and_tags(template, substitutions) + suffix(attachment, row_index, index) end def ==(other) diff --git a/app/services/tiptap_service.rb b/app/services/tiptap_service.rb index 3bbee3494..3b6e61d19 100644 --- a/app/services/tiptap_service.rb +++ b/app/services/tiptap_service.rb @@ -19,10 +19,10 @@ class TiptapService children(node[:content], substitutions, 0) end - def to_path(node, substitutions = {}) + def to_texts_and_tags(node, substitutions = {}) return '' if node.nil? - children_path(node[:content], substitutions) + children_texts_and_tags(node[:content], substitutions) end private @@ -31,18 +31,24 @@ class TiptapService @body_started = false end - def children_path(content, substitutions) - content.map { node_to_path(_1, substitutions) }.join + def children_texts_and_tags(content, substitutions) + content.map { node_to_texts_and_tags(_1, substitutions) }.join end - def node_to_path(node, substitutions) + def node_to_texts_and_tags(node, substitutions) case node in type: 'paragraph', content: - children_path(content, substitutions) - in type: 'text', text:, **rest + children_texts_and_tags(content, substitutions) + in type: 'paragraph' # empty paragraph + '' + in type: 'text', text: text.strip - in type: 'mention', attrs: { id: }, **rest - substitutions.fetch(id) { "--#{id}--" } + in type: 'mention', attrs: { id:, label: } + if substitutions.present? + substitutions.fetch(id) { "--#{id}--" } + else + "#{label}" + end end end diff --git a/spec/services/tiptap_service_spec.rb b/spec/services/tiptap_service_spec.rb index 1ec0468b2..365fcae3b 100644 --- a/spec/services/tiptap_service_spec.rb +++ b/spec/services/tiptap_service_spec.rb @@ -193,19 +193,40 @@ RSpec.describe TiptapService do end end - describe '.to_path' do - let(:substitutions) { { "dossier_number" => "42" } } - let(:json) do - { - "content" => [ - { "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " .pdf", "type" => "text" }] } - ] + describe '.to_texts_and_tags' do + subject { described_class.new.to_texts_and_tags(json, substitutions) } - }.deep_symbolize_keys + context 'nominal' do + let(:json) do + { + "content" => [ + { "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " .pdf", "type" => "text" }] } + ] + + }.deep_symbolize_keys + end + + context 'with substitutions' do + let(:substitutions) { { "dossier_number" => "42" } } + it 'returns texts_and_tags' do + is_expected.to eq("export_42.pdf") + end + end + + context 'without substitutions' do + let(:substitutions) { nil } + + it 'returns texts_and_tags' do + is_expected.to eq("export_numéro du dossier.pdf") + end + end end - it 'returns path' do - expect(described_class.new.to_path(json, substitutions)).to eq("export_42.pdf") + context 'empty paragraph' do + let(:json) { { content: [{ type: 'paragraph' }] } } + let(:substitutions) { {} } + + it { is_expected.to eq('') } end end end