Skip to content

Commit

Permalink
Render page notes differently than annotations (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov authored Dec 10, 2024
1 parent 8eb5d32 commit 97bc112
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 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 @@ -85,17 +85,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
4 changes: 1 addition & 3 deletions tests/functional/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,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
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 @@ -112,12 +112,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
33 changes: 30 additions & 3 deletions tests/unit/slack_annotations/format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_without_title(self):
"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) == {
Expand All @@ -62,7 +68,7 @@ def test_without_title(self):
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "(None)"},
{"type": "plain_text", "text": "Annotated text"},
{"type": "plain_text", "text": "(None)"},
],
}
Expand All @@ -83,12 +89,33 @@ def test_without_user_info(self):
"text": "`test_user_1` annotated <https://example.com/|Annotation title>:",
},
"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)"},
],
}

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 97bc112

Please sign in to comment.