Skip to content

Commit

Permalink
bluesky.to_as1: preserve whitespace by converting to HTML
Browse files Browse the repository at this point in the history
eg for `app.bsky.actor.profile` and `app.bsky.feed.post`, convert newlines to `<br>`s in `text` and `description`
  • Loading branch information
snarfed committed Jun 7, 2024
1 parent d8c402c commit 85b9e19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ _Non-breaking changes:_
* Add hashtag facet support.
* Convert blobs in embeds to `getBlob` image URLs.
* `app.bsky.actor.profile`: add HTML links for URLs in `summary` ([snarfed/bridgy-fed#1065](https://github.com/snarfed/bridgy-fed/issues/1065)).
* `app.bsky.actor.profile` and `app.bsky.feed.post`: preserve whitespace in `text` and `description` by converting to HTML, eg newlines to `<br>`s.
* `from_as1`:
* Add hashtag, mention, block, and flag support. Interpret `tags` with missing `objectType` as hashtags.
* Guess missing indices in facets based on content text. Otherwise, if we still don't know a facet's indices, discard it.
Expand Down
9 changes: 6 additions & 3 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,14 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
'username': obj.get('handle') or repo_handle,
}

summary = util.linkify((obj.get('description') or '').replace('\n', '<br>'),
pretty=True)

ret.update({
'url': util.dedupe_urls(urls),
'displayName': obj.get('displayName'),
# TODO: for app.bsky.feed.generator, use descriptionFacets
'summary': util.linkify(obj.get('description') or '', pretty=True),
'summary': summary,
'image': images,
'published': obj.get('createdAt'),
})
Expand Down Expand Up @@ -1023,12 +1026,12 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
tags.append(tag)

in_reply_to = obj.get('reply', {}).get('parent', {}).get('uri')

content = text.replace('\n', '<br>')
ret = {
'objectType': 'comment' if in_reply_to else 'note',
'id': uri,
'url': at_uri_to_web_url(uri),
'content': text,
'content': content,
'inReplyTo': [{
'id': in_reply_to,
'url': at_uri_to_web_url(in_reply_to),
Expand Down
18 changes: 18 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,15 @@ def test_to_as1_profile_no_authenticated_label_to_unlisted(self):
},
}))

def test_to_as1_profile_newlines_to_brs(self):
self.assert_equals({
'objectType': 'person',
'summary': 'one<br>two<br><br>three',
}, to_as1({
'$type': 'app.bsky.actor.profile',
'description': 'one\ntwo\n\nthree',
}))

def test_to_as1_profileView_no_authenticated_label_to_unlisted(self):
got = to_as1({
**ACTOR_PROFILE_VIEW_BSKY,
Expand Down Expand Up @@ -1582,6 +1591,15 @@ def test_to_as1_post_with_image_blank_alt_text(self):
def test_to_as1_post_view_with_image(self):
self.assert_equals(POST_AS_IMAGES['object'], to_as1(POST_VIEW_BSKY_IMAGES))

def test_to_as1_post_newlines_to_brs(self):
self.assert_equals({
'objectType': 'note',
'content': 'one<br>two<br><br>three',
}, to_as1({
'$type': 'app.bsky.feed.post',
'text': 'one\ntwo\n\nthree',
}))

def test_to_as1_feedViewPost(self):
self.assert_equals(POST_AUTHOR_AS['object'], to_as1(POST_FEED_VIEW_BSKY))

Expand Down

0 comments on commit 85b9e19

Please sign in to comment.