Skip to content

Commit

Permalink
Render page notes differently than annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Dec 10, 2024
1 parent cd86af9 commit fb38e7b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
35 changes: 23 additions & 12 deletions src/slack_annotations/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


def _format_annotation(annotation: dict[str, Any]) -> dict[str, Any]:
fields = _build_annotation_fields(annotation)
summary = _build_annotation_summary(annotation)
fields = _build_annotation_fields(annotation)
return {
"type": "section",
"text": {"type": "mrkdwn", "text": summary},
Expand Down Expand Up @@ -82,17 +82,28 @@ def _build_annotation_summary(annotation: dict[str, Any]) -> str:


def _build_annotation_fields(annotation: dict[str, Any]) -> list[dict[str, Any]]:
quote = None
try:
quote = _get_quote(annotation)
except Exception: # pylint:disable=broad-exception-caught
quote = NONE_TEXT

return [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": f"*Annotation* (<{annotation['links']['incontext']}|in-context link>):",
},
{"type": "plain_text", "text": quote},
{"type": "plain_text", "text": _get_text(annotation)},
]
pass
incontext_link = annotation["links"]["incontext"]

if quote:
fields = [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": f"*Annotation* (<{incontext_link}|in-context link>):",
},
{"type": "plain_text", "text": quote},
]
else:
fields = [
{
"type": "mrkdwn",
"text": f"*Page Note* (<{incontext_link}|in-context link>):",
},
]
fields.append({"type": "plain_text", "text": _get_text(annotation)})
return fields
3 changes: 1 addition & 2 deletions tests/functional/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ def slack_annotations():
"text": "`test_user_1` (md............................) annotated <https://example.com/|Annotating the law | Hypothes.is>:",
},
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
"text": "*Page Note* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "(None)"},
{"type": "plain_text", "text": "test_user_1 reply"},
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/slack_annotations/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ def slack_annotations():
"text": "`test_user_1` (md............................) annotated <https://example.com/|Annotating the law | Hypothes.is>:",
},
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
"text": "*Page Note* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "(None)"},
{"type": "plain_text", "text": "test_user_1 reply"},
],
},
Expand Down
74 changes: 52 additions & 22 deletions tests/unit/slack_annotations/format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,57 @@ def test_format_empty_annotations():
assert not format_annotations([])


def test_format_annotation_without_title():
annotation = {
"user": "acct:[email protected]",
"uri": "https://example.com/",
"links": {"incontext": "https://hyp.is/test_annotation_id_1/example.com/"},
"user_info": {"display_name": "md............................"},
}

assert _format_annotation(annotation) == {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`test_user_1` (md............................) annotated https://example.com/:",
},
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
class TestFormatAnnotation:
def test_without_title(self):
annotation = {
"user": "acct:[email protected]",
"uri": "https://example.com/",
"links": {"incontext": "https://hyp.is/test_annotation_id_1/example.com/"},
"user_info": {"display_name": "md............................"},
"target": [
{
"source": "https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/",
"selector": [{"exact": "Annotated text"}],
}
],
}

assert _format_annotation(annotation) == {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
"text": "`test_user_1` (md............................) annotated https://example.com/:",
},
{"type": "plain_text", "text": "(None)"},
{"type": "plain_text", "text": "(None)"},
],
}
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "Annotated text"},
{"type": "plain_text", "text": "(None)"},
],
}

def test_page_note(self):
annotation = {
"user": "acct:[email protected]",
"uri": "https://example.com/",
"links": {"incontext": "https://hyp.is/test_annotation_id_1/example.com/"},
"user_info": {"display_name": "md............................"},
}

assert _format_annotation(annotation) == {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`test_user_1` (md............................) annotated https://example.com/:",
},
"fields": [
{
"type": "mrkdwn",
"text": "*Page Note* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "(None)"},
],
}

0 comments on commit fb38e7b

Please sign in to comment.