commit 49362f5

default  ·  2023-05-21 18:32:23 +0000 UTC
parent 3cd7331
Convert image links in notes to attachments.
5 files changed,  +28, -11
M html.c
M main.c
M snac.h
+2, -2
 1@@ -646,7 +646,7 @@ d_char *msg_actor(snac *snac)
 2     msg = xs_dict_set(msg, "preferredUsername", snac->uid);
 3     msg = xs_dict_set(msg, "published",         xs_dict_get(snac->config, "published"));
 4 
 5-    f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"));
 6+    f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
 7     msg = xs_dict_set(msg, "summary", f_bio);
 8 
 9     char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
10@@ -789,7 +789,7 @@ xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
11     }
12 
13     /* format the content */
14-    fc2 = not_really_markdown(content);
15+    fc2 = not_really_markdown(content, &atls);
16 
17     if (in_reply_to != NULL && *in_reply_to) {
18         xs *p_msg = NULL;
+22, -5
 1@@ -3,6 +3,7 @@
 2 
 3 #include "xs.h"
 4 #include "xs_regex.h"
 5+#include "xs_mime.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -38,7 +39,7 @@ struct {
10 };
11 
12 
13-static xs_str *format_line(const char *line)
14+static xs_str *format_line(const char *line, xs_list **attach)
15 /* formats a line */
16 {
17     xs_str *s = xs_str_new(NULL);
18@@ -73,8 +74,24 @@ static xs_str *format_line(const char *line)
19             else
20             if (xs_startswith(v, "http")) {
21                 xs *v2 = xs_strip_chars_i(xs_dup(v), ".");
22-                xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v);
23-                s = xs_str_cat(s, s1);
24+
25+                const char *mime = xs_mime_by_ext(v2);
26+
27+                if (attach != NULL && xs_startswith(mime, "image/")) {
28+                    /* if it's a link to an image, insert it as an attachment */
29+                    xs *d = xs_dict_new();
30+
31+                    d = xs_dict_append(d, "mediaType", mime);
32+                    d = xs_dict_append(d, "url",       v2);
33+                    d = xs_dict_append(d, "name",      "");
34+                    d = xs_dict_append(d, "type",      "Image");
35+
36+                    *attach = xs_list_append(*attach, d);
37+                }
38+                else {
39+                    xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v);
40+                    s = xs_str_cat(s, s1);
41+                }
42             }
43             else
44                 s = xs_str_cat(s, v);
45@@ -90,7 +107,7 @@ static xs_str *format_line(const char *line)
46 }
47 
48 
49-xs_str *not_really_markdown(const char *content)
50+xs_str *not_really_markdown(const char *content, xs_list **attach)
51 /* formats a content using some Markdown rules */
52 {
53     xs_str *s  = xs_str_new(NULL);
54@@ -119,7 +136,7 @@ xs_str *not_really_markdown(const char *content)
55         if (in_pre)
56             ss = xs_dup(v);
57         else
58-            ss = xs_strip_i(format_line(v));
59+            ss = xs_strip_i(format_line(v, attach));
60 
61         if (xs_startswith(ss, ">")) {
62             /* delete the > and subsequent spaces */
M html.c
+2, -2
 1@@ -322,7 +322,7 @@ d_char *html_user_header(snac *snac, d_char *s, int local)
 2         s = xs_str_cat(s, s1);
 3 
 4         if (local) {
 5-            xs *bio = not_really_markdown(xs_dict_get(snac->config, "bio"));
 6+            xs *bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
 7             xs *s1  = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio);
 8 
 9             s = xs_str_cat(s, s1);
10@@ -1467,7 +1467,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
11     if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
12         d_char *rss;
13         xs *elems = timeline_simple_list(&snac, "public", 0, 20);
14-        xs *bio   = not_really_markdown(xs_dict_get(snac.config, "bio"));
15+        xs *bio   = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
16         char *p, *v;
17 
18         /* escape tags */
M main.c
+1, -1
1@@ -87,7 +87,7 @@ int main(int argc, char *argv[])
2     if (strcmp(cmd, "markdown") == 0) {
3         /* undocumented, for testing only */
4         xs *c = xs_readall(stdin);
5-        xs *fc = not_really_markdown(c);
6+        xs *fc = not_really_markdown(c, NULL);
7 
8         printf("<html>\n%s\n</html>\n", fc);
9         return 0;
M snac.h
+1, -1
1@@ -228,7 +228,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2                              char *payload, int p_size,
3                              char **body, int *b_size, char **ctype);
4 
5-xs_str *not_really_markdown(const char *content);
6+xs_str *not_really_markdown(const char *content, xs_list **attach);
7 xs_str *sanitize(const char *content);
8 
9 int html_get_handler(const xs_dict *req, const char *q_path,