Skip to content

Commit

Permalink
Fix converting paragraphs to plain text (#19)
Browse files Browse the repository at this point in the history
* Fix converting paragraphs to plain text
  • Loading branch information
chadwilken authored May 22, 2024
1 parent d128458 commit b85adf0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/tip_tap/nodes/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions spec/tip_tap/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/tip_tap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b85adf0

Please sign in to comment.