From 93ad0f4bda3ab72b0ef5c8e8b334b376e0753275 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Sat, 2 Mar 2024 22:41:05 +0100 Subject: [PATCH] [tiptap] convert tiptap json to path --- app/services/tiptap_service.rb | 21 +++++++++++++++++++++ spec/services/tiptap_service_spec.rb | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/services/tiptap_service.rb b/app/services/tiptap_service.rb index ce372d39f..5d0cb4325 100644 --- a/app/services/tiptap_service.rb +++ b/app/services/tiptap_service.rb @@ -5,6 +5,12 @@ class TiptapService children(node[:content], substitutions, 0) end + def to_path(node, substitutions = {}) + return '' if node.nil? + + children_path(node[:content], substitutions) + end + # NOTE: node must be deep symbolized keys def used_tags_and_libelle_for(node, tags = Set.new) case node @@ -25,6 +31,21 @@ class TiptapService @body_started = false end + def children_path(content, substitutions) + content.map { node_to_path(_1, substitutions) }.join + end + + def node_to_path(node, substitutions) + case node + in type: 'paragraph', content: + children_path(content, substitutions) + in type: 'text', text:, **rest + text.strip + in type: 'mention', attrs: { id: }, **rest + substitutions.fetch(id) { "--#{id}--" } + end + end + def children(content, substitutions, level) content.map { node_to_html(_1, substitutions, level) }.join end diff --git a/spec/services/tiptap_service_spec.rb b/spec/services/tiptap_service_spec.rb index 14196fb53..da47220f2 100644 --- a/spec/services/tiptap_service_spec.rb +++ b/spec/services/tiptap_service_spec.rb @@ -192,4 +192,20 @@ RSpec.describe TiptapService do expect(described_class.new.used_tags_and_libelle_for(json)).to eq(Set.new([['name', 'Nom']])) 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" }] } + ] + + }.deep_symbolize_keys + end + + it 'returns path' do + expect(described_class.new.to_path(json, substitutions)).to eq("export_42.pdf") + end + end end