From b85adf0ef95be645fc2859bbf858a4deb86caf0d Mon Sep 17 00:00:00 2001 From: Chad Wilken Date: Wed, 22 May 2024 09:12:14 -0500 Subject: [PATCH] Fix converting paragraphs to plain text (#19) * Fix converting paragraphs to plain text --- lib/tip_tap/nodes/paragraph.rb | 7 +++++ spec/tip_tap/node_spec.rb | 48 ++++++++++++++++++++++++++++++++++ spec/tip_tap_spec.rb | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/tip_tap/nodes/paragraph.rb b/lib/tip_tap/nodes/paragraph.rb index ee51775..4fa456e 100644 --- a/lib/tip_tap/nodes/paragraph.rb +++ b/lib/tip_tap/nodes/paragraph.rb @@ -11,6 +11,13 @@ class Paragraph < Node def text(text, marks: []) add_content(Text.new(text, marks: marks)) end + + # Override the default to_plain_text method to account for the nested Text nodes + # we don't want to use the separator when joining the text nodes since it could + # be a newline or some other character that we don't want to include in the plain text + def to_plain_text(separator: " ") + content.map { |node| node.to_plain_text(separator: separator) }.join("") + end end end end diff --git a/spec/tip_tap/node_spec.rb b/spec/tip_tap/node_spec.rb index 445e0d7..b8d2820 100644 --- a/spec/tip_tap/node_spec.rb +++ b/spec/tip_tap/node_spec.rb @@ -89,5 +89,53 @@ expect(text).to be_a(String) expect(text).to eq("Hello World!") end + + context "when the node has children" do + it "breaks up paragraphs with separator" do + node = TipTap::Node.from_json({ + content: [ + {type: "paragraph", content: [type: "text", text: "Hello World!"]}, + {type: "paragraph", content: [type: "text", text: "How are you?"]} + ] + }) + text = node.to_plain_text(separator: "\n\n") + expect(text).to be_a(String) + expect(text).to eq("Hello World!\n\nHow are you?") + end + + it "does not break up links with separator" do + node = TipTap::Node.from_json({ + content: [ + { + type: "paragraph", content: [ + {type: "text", text: "Hello "}, + {type: "text", text: "World!", marks: [{type: "link", attrs: {href: "https://example.com"}}]} + ] + }, + {type: "paragraph", content: [type: "text", text: "How are you?"]} + ] + }) + text = node.to_plain_text(separator: "\n\n") + expect(text).to be_a(String) + expect(text).to eq("Hello World!\n\nHow are you?") + end + + it "does not break up bold marks with separator" do + node = TipTap::Node.from_json({ + content: [ + { + type: "paragraph", content: [ + {type: "text", text: "Hello "}, + {type: "text", text: "World!", marks: [{type: "bold"}]} + ] + }, + {type: "paragraph", content: [type: "text", text: "How are you?"]} + ] + }) + text = node.to_plain_text(separator: "\n\n") + expect(text).to be_a(String) + expect(text).to eq("Hello World!\n\nHow are you?") + end + end end end diff --git a/spec/tip_tap_spec.rb b/spec/tip_tap_spec.rb index fa0e714..eda94bd 100644 --- a/spec/tip_tap_spec.rb +++ b/spec/tip_tap_spec.rb @@ -20,7 +20,7 @@ it "parses the json and returns the plain text" do expect(document.to_plain_text).to eq( - "Site Summary Overview - May 2nd 2023 This is a site visit summary that is being synthesized by Chad Wilken. Todo 1 Todo 2 Todo 3 This is a heading 2 This is a heading 3 This is a bullet item This is another item Final paragraph." + "Site Summary Overview - May 2nd 2023 This is a site visit summary that is being synthesized by Chad Wilken. Todo 1 Todo 2 Todo 3 This is a heading 2 This is a heading 3 This is a bullet item This is another item Final paragraph." ) end