bsky descriptions with newlines

This commit is contained in:
Dylan Knutson
2025-08-12 21:31:59 +00:00
parent 171ddd430b
commit 2de7f85a99
3 changed files with 170 additions and 6 deletions

View File

@@ -34,6 +34,151 @@ RSpec.describe Domain::BlueskyPostHelper, type: :helper do
end
end
context "with newlines in text" do
let(:text_with_newlines) { "Line 1\nLine 2\nLine 3" }
let(:facets) do
[
{
"$type" => "app.bsky.richtext.facet",
"index" => {
"byteStart" => 0,
"byteEnd" => 6,
},
"features" => [
{
"$type" => "app.bsky.richtext.facet#mention",
"did" => "did:plc:alice123",
},
],
},
]
end
it "converts newlines to <br /> tags in text parts" do
expect(helper).to receive(:render).with(
partial: "domain/has_description_html/external_link",
locals:
hash_including(
link_text: "Line 1",
url: "https://bsky.app/profile/did:plc:alice123",
),
).and_return("[external user link]")
result = helper.render_bsky_post_facets(text_with_newlines, facets)
expect(result).to eq("[external user link]<br />Line 2<br />Line 3")
end
context "with newlines in text without facets" do
it "keeps newlines unchanged when no facets are present" do
result = helper.render_bsky_post_facets(text_with_newlines, [])
expect(result).to eq("Line 1\nLine 2\nLine 3")
end
end
context "with newlines around facets" do
let(:text_with_newlines_around_facets) do
"Before\n@alice.bsky.social\nAfter"
end
let(:facets) do
[
{
"$type" => "app.bsky.richtext.facet",
"index" => {
"byteStart" => 7,
"byteEnd" => 25,
},
"features" => [
{
"$type" => "app.bsky.richtext.facet#mention",
"did" => "did:plc:alice123",
},
],
},
]
end
it "converts newlines to <br /> tags while preserving facets" do
expect(helper).to receive(:render).with(
partial: "domain/has_description_html/external_link",
locals:
hash_including(
link_text: "@alice.bsky.social",
url: "https://bsky.app/profile/did:plc:alice123",
),
).and_return("[external user link]")
result =
helper.render_bsky_post_facets(
text_with_newlines_around_facets,
facets,
)
expect(result).to eq("Before<br />[external user link]<br />After")
end
end
context "with multiple text parts containing newlines" do
let(:text_with_multiple_newlines) do
"Start\n@alice.bsky.social\nMiddle\n@bob.bsky.social\nEnd"
end
let(:facets) do
[
{
"$type" => "app.bsky.richtext.facet",
"index" => {
"byteStart" => 6,
"byteEnd" => 24,
},
"features" => [
{
"$type" => "app.bsky.richtext.facet#mention",
"did" => "did:plc:alice123",
},
],
},
{
"$type" => "app.bsky.richtext.facet",
"index" => {
"byteStart" => 32,
"byteEnd" => 48,
},
"features" => [
{
"$type" => "app.bsky.richtext.facet#mention",
"did" => "did:plc:bob123",
},
],
},
]
end
it "converts newlines in all text parts to <br /> tags" do
expect(helper).to receive(:render).with(
partial: "domain/has_description_html/external_link",
locals:
hash_including(
link_text: "@alice.bsky.social",
url: "https://bsky.app/profile/did:plc:alice123",
),
).and_return("[alice link]")
expect(helper).to receive(:render).with(
partial: "domain/has_description_html/external_link",
locals:
hash_including(
link_text: "@bob.bsky.social",
url: "https://bsky.app/profile/did:plc:bob123",
),
).and_return("[bob link]")
result =
helper.render_bsky_post_facets(text_with_multiple_newlines, facets)
expect(result).to eq(
"Start<br />[alice link]<br />Middle<br />[bob link]<br />End",
)
end
end
end
context "with mention facets" do
let(:mentioned_user) do
create(