Skip to content

Commit 85b9e19

Browse files
committed
bluesky.to_as1: preserve whitespace by converting to HTML
eg for `app.bsky.actor.profile` and `app.bsky.feed.post`, convert newlines to `<br>`s in `text` and `description`
1 parent d8c402c commit 85b9e19

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ _Non-breaking changes:_
313313
* Add hashtag facet support.
314314
* Convert blobs in embeds to `getBlob` image URLs.
315315
* `app.bsky.actor.profile`: add HTML links for URLs in `summary` ([snarfed/bridgy-fed#1065](https://github.com/snarfed/bridgy-fed/issues/1065)).
316+
* `app.bsky.actor.profile` and `app.bsky.feed.post`: preserve whitespace in `text` and `description` by converting to HTML, eg newlines to `<br>`s.
316317
* `from_as1`:
317318
* Add hashtag, mention, block, and flag support. Interpret `tags` with missing `objectType` as hashtags.
318319
* Guess missing indices in facets based on content text. Otherwise, if we still don't know a facet's indices, discard it.

granary/bluesky.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,14 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
947947
'username': obj.get('handle') or repo_handle,
948948
}
949949

950+
summary = util.linkify((obj.get('description') or '').replace('\n', '<br>'),
951+
pretty=True)
952+
950953
ret.update({
951954
'url': util.dedupe_urls(urls),
952955
'displayName': obj.get('displayName'),
953956
# TODO: for app.bsky.feed.generator, use descriptionFacets
954-
'summary': util.linkify(obj.get('description') or '', pretty=True),
957+
'summary': summary,
955958
'image': images,
956959
'published': obj.get('createdAt'),
957960
})
@@ -1023,12 +1026,12 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
10231026
tags.append(tag)
10241027

10251028
in_reply_to = obj.get('reply', {}).get('parent', {}).get('uri')
1026-
1029+
content = text.replace('\n', '<br>')
10271030
ret = {
10281031
'objectType': 'comment' if in_reply_to else 'note',
10291032
'id': uri,
10301033
'url': at_uri_to_web_url(uri),
1031-
'content': text,
1034+
'content': content,
10321035
'inReplyTo': [{
10331036
'id': in_reply_to,
10341037
'url': at_uri_to_web_url(in_reply_to),

granary/tests/test_bluesky.py

+18
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,15 @@ def test_to_as1_profile_no_authenticated_label_to_unlisted(self):
15091509
},
15101510
}))
15111511

1512+
def test_to_as1_profile_newlines_to_brs(self):
1513+
self.assert_equals({
1514+
'objectType': 'person',
1515+
'summary': 'one<br>two<br><br>three',
1516+
}, to_as1({
1517+
'$type': 'app.bsky.actor.profile',
1518+
'description': 'one\ntwo\n\nthree',
1519+
}))
1520+
15121521
def test_to_as1_profileView_no_authenticated_label_to_unlisted(self):
15131522
got = to_as1({
15141523
**ACTOR_PROFILE_VIEW_BSKY,
@@ -1582,6 +1591,15 @@ def test_to_as1_post_with_image_blank_alt_text(self):
15821591
def test_to_as1_post_view_with_image(self):
15831592
self.assert_equals(POST_AS_IMAGES['object'], to_as1(POST_VIEW_BSKY_IMAGES))
15841593

1594+
def test_to_as1_post_newlines_to_brs(self):
1595+
self.assert_equals({
1596+
'objectType': 'note',
1597+
'content': 'one<br>two<br><br>three',
1598+
}, to_as1({
1599+
'$type': 'app.bsky.feed.post',
1600+
'text': 'one\ntwo\n\nthree',
1601+
}))
1602+
15851603
def test_to_as1_feedViewPost(self):
15861604
self.assert_equals(POST_AUTHOR_AS['object'], to_as1(POST_FEED_VIEW_BSKY))
15871605

0 commit comments

Comments
 (0)