From 5f6a42453c168b2fa63fdf15cfd65a5af7fc4680 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 19 Jul 2024 07:24:28 +0200 Subject: [PATCH] Added support for markdown-like ![alt text](image url). --- doc/snac.5 | 5 +++++ format.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/doc/snac.5 b/doc/snac.5 index 1b07155..fc991b2 100644 --- a/doc/snac.5 +++ b/doc/snac.5 @@ -43,6 +43,11 @@ int main(int argc, char *argv[]) Standalone URLs are converted to links. Also, from version 2.54, markdown-style links in the form of [link label](url) are also supported. +.It attached images +Standalone URLs for which the final extension is recognized as an +image (.jpg, .gif, .png, etc), are converted to ActivityPub image +attachments. Also, from version 2.57, markdown-style image links +in the form of ![alt text](image url) are also supported. .It line separators Horizonal rules can be inserted by typing three minus symbols alone in a line. diff --git a/format.c b/format.c index 170d28a..573b702 100644 --- a/format.c +++ b/format.c @@ -91,6 +91,7 @@ static xs_str *format_line(const char *line, xs_list **attach) "`[^`]+`" "|" "~~[^~]+~~" "|" "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*" "|" + "!\\[[^]]+\\]\\([^\\)]+\\)" "|" "\\[[^]]+\\]\\([^\\)]+\\)" "|" "https?:/" "/[^[:space:]]+" ")"); @@ -169,6 +170,36 @@ static xs_str *format_line(const char *line, xs_list **attach) else s = xs_str_cat(s, v); } + else + if (*v == '!') { + /* markdown-like images ![alt text](url to image) */ + xs *w = xs_strip_chars_i(xs_replace(v, "#", "#"), "![)"); + xs *l = xs_split_n(w, "](", 1); + + if (xs_list_len(l) == 2) { + const char *alt_text = xs_list_get(l, 0); + const char *img_url = xs_list_get(l, 1); + const char *mime = xs_mime_by_ext(img_url); + + if (attach != NULL && xs_startswith(mime, "image/")) { + xs *d = xs_dict_new(); + + d = xs_dict_append(d, "mediaType", mime); + d = xs_dict_append(d, "url", img_url); + d = xs_dict_append(d, "name", alt_text); + d = xs_dict_append(d, "type", "Image"); + + *attach = xs_list_append(*attach, d); + } + else { + xs *link = xs_fmt("%s", img_url, alt_text); + + s = xs_str_cat(s, link); + } + } + else + s = xs_str_cat(s, v); + } else s = xs_str_cat(s, v); }