[tiptap] convert tiptap json to path
This commit is contained in:
parent
b3c2b46bb1
commit
93ad0f4bda
2 changed files with 37 additions and 0 deletions
|
@ -5,6 +5,12 @@ class TiptapService
|
||||||
children(node[:content], substitutions, 0)
|
children(node[:content], substitutions, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_path(node, substitutions = {})
|
||||||
|
return '' if node.nil?
|
||||||
|
|
||||||
|
children_path(node[:content], substitutions)
|
||||||
|
end
|
||||||
|
|
||||||
# NOTE: node must be deep symbolized keys
|
# NOTE: node must be deep symbolized keys
|
||||||
def used_tags_and_libelle_for(node, tags = Set.new)
|
def used_tags_and_libelle_for(node, tags = Set.new)
|
||||||
case node
|
case node
|
||||||
|
@ -25,6 +31,21 @@ class TiptapService
|
||||||
@body_started = false
|
@body_started = false
|
||||||
end
|
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)
|
def children(content, substitutions, level)
|
||||||
content.map { node_to_html(_1, substitutions, level) }.join
|
content.map { node_to_html(_1, substitutions, level) }.join
|
||||||
end
|
end
|
||||||
|
|
|
@ -192,4 +192,20 @@ RSpec.describe TiptapService do
|
||||||
expect(described_class.new.used_tags_and_libelle_for(json)).to eq(Set.new([['name', 'Nom']]))
|
expect(described_class.new.used_tags_and_libelle_for(json)).to eq(Set.new([['name', 'Nom']]))
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue