Skip to content

Commit

Permalink
Wrap attachments in attachment-group AST
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Jan 30, 2025
1 parent c6f2297 commit 19f1327
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 97 deletions.
18 changes: 6 additions & 12 deletions frontend/src/style/misago/rich-text.less
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
}
}

.rich-text-attachment-group:last-child {
margin-bottom: @line-height-computed * -1;
}

.rich-text-image {
display: inline-block;
position: relative;
Expand All @@ -68,10 +72,6 @@
img {
border-radius: @border-radius-base;
}

&:last-child {
margin-bottom: 0;
}
}

.rich-text-video {
Expand All @@ -87,10 +87,6 @@
background-color: #1e1e1e;
border-radius: @border-radius-base;

&:last-child {
margin-bottom: 0;
}

video {
position: absolute;
top: 0;
Expand Down Expand Up @@ -140,11 +136,8 @@
}

.rich-text-file {
display: inline-block;
margin-bottom: @line-height-computed;

&:last-child {
margin-bottom: 0;
}
}

.rich-text-file-body {
Expand Down Expand Up @@ -223,6 +216,7 @@
}

.rich-text-attachment-error {
display: inline-block;
margin-bottom: @line-height-computed;

&:last-child {
Expand Down
4 changes: 4 additions & 0 deletions misago/parser/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def _render_ast_node_to_html_action(
if ast_type in ("thematic-break", "thematic-break-bbcode"):
return "<hr />"

if ast_type == "attachment-group":
children = render_ast_to_html(context, ast_node["children"], metadata)
return f'<div class="rich-text-attachment-group">{children}</div>'

if ast_type == "attachment":
return f"<attachment={ast_node['name']}:{ast_node['slug']}:{ast_node['id']}>"

Expand Down
5 changes: 5 additions & 0 deletions misago/parser/plaintext.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ def _render_ast_node_to_plaintext_action(

return href

if ast_type == "attachment-group":
return render_ast_to_plaintext(
context, ast_node["children"], metadata, text_format
)

if ast_type == "attachment":
return ast_node["name"]

Expand Down
5 changes: 4 additions & 1 deletion misago/parser/postprocessors/cleanast.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def process_invalid_parent(self, node: dict) -> list[dict]:
new_ast.append(node_copy)
new_children = []

new_ast.append(child)
if not new_ast or new_ast[-1]["type"] != "attachment-group":
new_ast.append({"type": "attachment-group", "children": []})

new_ast[-1]["children"].append(child)
else:
new_children += self.process_node(child)

Expand Down
2 changes: 1 addition & 1 deletion misago/parser/tests/__snapshots__/test_html.ambr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# serializer version: 1
# name: test_render_ast_to_html_attachment
'<p>See the site: </p><attachment=image.png:image-png:123>'
'<p>See the site: </p><div class="rich-text-attachment-group"><attachment=image.png:image-png:123></div>'
# ---
# name: test_render_ast_to_html_auto_link
'<p>See the file: <a href="https://misago-project.org" rel="external nofollow noopener" target="_blank">https://misago-project.org</a></p>'
Expand Down
3 changes: 3 additions & 0 deletions misago/parser/tests/__snapshots__/test_plaintext.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# name: test_render_ast_to_plaintext_attachment
'See the logo: file.png'
# ---
# name: test_render_ast_to_plaintext_attachment_group
'See the logo: file.png document.pdf'
# ---
# name: test_render_ast_to_plaintext_auto_link
'See the site: https://misago-project.org'
# ---
Expand Down
115 changes: 75 additions & 40 deletions misago/parser/tests/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ def test_attachment(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
},
],
},
{
"type": "paragraph",
Expand All @@ -32,10 +37,15 @@ def test_attachment_without_quotes_around_args(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
},
],
},
{
"type": "paragraph",
Expand All @@ -56,10 +66,15 @@ def test_attachment_with_spaces_in_args(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 1234,
},
],
},
{
"type": "paragraph",
Expand All @@ -80,16 +95,21 @@ def test_attachment_next_to_other_attachment(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
},
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
},
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
},
],
},
]

Expand All @@ -104,16 +124,21 @@ def test_attachments_with_spaces_between(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
},
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
},
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
},
],
},
]

Expand All @@ -130,10 +155,15 @@ def test_attachments_with_blank_lines_between(parse_markup):
],
},
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image.png",
"slug": "image-png",
"id": 6,
},
],
},
{
"type": "paragraph",
Expand All @@ -142,10 +172,15 @@ def test_attachments_with_blank_lines_between(parse_markup):
],
},
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "image2.png",
"slug": "image2-png",
"id": 7,
},
],
},
]

Expand Down
54 changes: 37 additions & 17 deletions misago/parser/tests/test_clean_ast_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ def test_clean_ast_post_processor_removes_attachment_from_paragraph(parse_markup
ast = parse_markup("<attachment=file.txt:123>")
assert ast == [
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
},
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
},
],
}
]


Expand All @@ -25,10 +30,15 @@ def test_clean_ast_post_processor_splits_paragraph_with_attachment(parse_markup)
],
},
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
},
],
},
{
"type": "paragraph",
Expand All @@ -51,10 +61,15 @@ def test_clean_ast_post_processor_splits_paragraph_with_two_attachments(parse_ma
],
},
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
},
],
},
{
"type": "paragraph",
Expand All @@ -63,10 +78,15 @@ def test_clean_ast_post_processor_splits_paragraph_with_two_attachments(parse_ma
],
},
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
"type": "attachment-group",
"children": [
{
"type": "attachment",
"name": "file.txt",
"slug": "file-txt",
"id": 123,
},
],
},
{
"type": "paragraph",
Expand Down
Loading

0 comments on commit 19f1327

Please sign in to comment.