commit 5f6a424

default  ·  2024-07-19 05:24:28 +0000 UTC
parent 6e3b621
Added support for markdown-like ![alt text](image url).
2 files changed,  +36, -0
+5, -0
 1@@ -43,6 +43,11 @@ int main(int argc, char *argv[])
 2 Standalone URLs are converted to links. Also, from version 2.54,
 3 markdown-style links in the form of [link label](url) are also
 4 supported.
 5+.It attached images
 6+Standalone URLs for which the final extension is recognized as an
 7+image (.jpg, .gif, .png, etc), are converted to ActivityPub image
 8+attachments. Also, from version 2.57, markdown-style image links
 9+in the form of ![alt text](image url) are also supported.
10 .It line separators
11 Horizonal rules can be inserted by typing three minus symbols
12 alone in a line.
+31, -0
 1@@ -91,6 +91,7 @@ static xs_str *format_line(const char *line, xs_list **attach)
 2             "`[^`]+`"                           "|"
 3             "~~[^~]+~~"                         "|"
 4             "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*"     "|"
 5+            "!\\[[^]]+\\]\\([^\\)]+\\)"         "|"
 6             "\\[[^]]+\\]\\([^\\)]+\\)"          "|"
 7             "https?:/" "/[^[:space:]]+"
 8         ")");
 9@@ -169,6 +170,36 @@ static xs_str *format_line(const char *line, xs_list **attach)
10                 else
11                     s = xs_str_cat(s, v);
12             }
13+            else
14+            if (*v == '!') {
15+                /* markdown-like images ![alt text](url to image) */
16+                xs *w = xs_strip_chars_i(xs_replace(v, "#", "#"), "![)");
17+                xs *l = xs_split_n(w, "](", 1);
18+
19+                if (xs_list_len(l) == 2) {
20+                    const char *alt_text = xs_list_get(l, 0);
21+                    const char *img_url  = xs_list_get(l, 1);
22+                    const char *mime     = xs_mime_by_ext(img_url);
23+
24+                    if (attach != NULL && xs_startswith(mime, "image/")) {
25+                        xs *d = xs_dict_new();
26+
27+                        d = xs_dict_append(d, "mediaType", mime);
28+                        d = xs_dict_append(d, "url",       img_url);
29+                        d = xs_dict_append(d, "name",      alt_text);
30+                        d = xs_dict_append(d, "type",      "Image");
31+
32+                        *attach = xs_list_append(*attach, d);
33+                    }
34+                    else {
35+                        xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
36+
37+                        s = xs_str_cat(s, link);
38+                    }
39+                }
40+                else
41+                    s = xs_str_cat(s, v);
42+            }
43             else
44                 s = xs_str_cat(s, v);
45         }