summaryrefslogtreecommitdiff
path: root/format.c
diff options
context:
space:
mode:
authorGravatar default2023-05-21 20:32:23 +0200
committerGravatar default2023-05-21 20:32:23 +0200
commit49362f54049a357e52cd6c57d2aa20d33add3307 (patch)
tree0417bac6b8157ce7be07be86b7eb6915bb24cdf7 /format.c
parentUpdated TODO. (diff)
downloadsnac2-49362f54049a357e52cd6c57d2aa20d33add3307.tar.gz
snac2-49362f54049a357e52cd6c57d2aa20d33add3307.tar.xz
snac2-49362f54049a357e52cd6c57d2aa20d33add3307.zip
Convert image links in notes to attachments.
Diffstat (limited to 'format.c')
-rw-r--r--format.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/format.c b/format.c
index 14508b9..5eec404 100644
--- a/format.c
+++ b/format.c
@@ -3,6 +3,7 @@
3 3
4#include "xs.h" 4#include "xs.h"
5#include "xs_regex.h" 5#include "xs_regex.h"
6#include "xs_mime.h"
6 7
7#include "snac.h" 8#include "snac.h"
8 9
@@ -38,7 +39,7 @@ struct {
38}; 39};
39 40
40 41
41static xs_str *format_line(const char *line) 42static xs_str *format_line(const char *line, xs_list **attach)
42/* formats a line */ 43/* formats a line */
43{ 44{
44 xs_str *s = xs_str_new(NULL); 45 xs_str *s = xs_str_new(NULL);
@@ -73,8 +74,24 @@ static xs_str *format_line(const char *line)
73 else 74 else
74 if (xs_startswith(v, "http")) { 75 if (xs_startswith(v, "http")) {
75 xs *v2 = xs_strip_chars_i(xs_dup(v), "."); 76 xs *v2 = xs_strip_chars_i(xs_dup(v), ".");
76 xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v); 77
77 s = xs_str_cat(s, s1); 78 const char *mime = xs_mime_by_ext(v2);
79
80 if (attach != NULL && xs_startswith(mime, "image/")) {
81 /* if it's a link to an image, insert it as an attachment */
82 xs *d = xs_dict_new();
83
84 d = xs_dict_append(d, "mediaType", mime);
85 d = xs_dict_append(d, "url", v2);
86 d = xs_dict_append(d, "name", "");
87 d = xs_dict_append(d, "type", "Image");
88
89 *attach = xs_list_append(*attach, d);
90 }
91 else {
92 xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v);
93 s = xs_str_cat(s, s1);
94 }
78 } 95 }
79 else 96 else
80 s = xs_str_cat(s, v); 97 s = xs_str_cat(s, v);
@@ -90,7 +107,7 @@ static xs_str *format_line(const char *line)
90} 107}
91 108
92 109
93xs_str *not_really_markdown(const char *content) 110xs_str *not_really_markdown(const char *content, xs_list **attach)
94/* formats a content using some Markdown rules */ 111/* formats a content using some Markdown rules */
95{ 112{
96 xs_str *s = xs_str_new(NULL); 113 xs_str *s = xs_str_new(NULL);
@@ -119,7 +136,7 @@ xs_str *not_really_markdown(const char *content)
119 if (in_pre) 136 if (in_pre)
120 ss = xs_dup(v); 137 ss = xs_dup(v);
121 else 138 else
122 ss = xs_strip_i(format_line(v)); 139 ss = xs_strip_i(format_line(v, attach));
123 140
124 if (xs_startswith(ss, ">")) { 141 if (xs_startswith(ss, ">")) {
125 /* delete the > and subsequent spaces */ 142 /* delete the > and subsequent spaces */