From 34e06214c3fe8d8b873b616624b4196986738a0e Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 20 Apr 2025 13:24:21 +0200 Subject: [PATCH] url encode/decode markdown image links --- application/editor.c | 10 +++++- application/gtk-text.c | 5 ++- libidav/utils.c | 75 ++++++++++++++++++++++++++++++++++++++++-- libidav/utils.h | 3 ++ 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/application/editor.c b/application/editor.c index cb39004..7bfdec4 100644 --- a/application/editor.c +++ b/application/editor.c @@ -33,6 +33,7 @@ #include #include +#include // url encoding/decoding void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown) { @@ -162,7 +163,14 @@ static int md_enter_span(MD_SPANTYPE type, void* detail, void* userdata) { } else if(type == MD_SPAN_IMG) { MD_SPAN_IMG_DETAIL *img = detail; if(img) { - node->link = cx_strdup_a(data->a, cx_strn(img->src.text, img->src.size)); + cxstring imgsrc = cx_strn(img->src.text, img->src.size); + if(cx_strprefix(imgsrc, CX_STR("http://")) || cx_strprefix(imgsrc, CX_STR("https://"))) { + // copy url in case it is http/https + node->link = cx_strdup_a(data->a, imgsrc); + } else { + // in case it is a path, urldecode the link + node->link = util_url_decode_s(data->a, imgsrc); + } } } diff --git a/application/gtk-text.c b/application/gtk-text.c index 9817f57..56fef7c 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -294,7 +294,10 @@ static gboolean editor_attach_file(NoteEditor *editor, const char *path) { void md_serialize_image(EmbeddedWidget *em, CxBuffer *out) { Attachment *attachment = em->data1; - cx_bprintf(out, "![image](%s)", attachment->name); + // TODO: relative path to parent resource + char *path = util_url_encode(NULL, attachment->name); + cx_bprintf(out, "![image](%s)", path); + free(path); } static void editor_insert_image(NoteEditor *editor, Attachment *attachment, GtkTextIter *iter) { diff --git a/libidav/utils.c b/libidav/utils.c index 0f87de8..c6f1eb1 100644 --- a/libidav/utils.c +++ b/libidav/utils.c @@ -348,10 +348,81 @@ cxstring util_url_path_s(cxstring url) { return path; } +char* util_url_encode(DavSession *sn, const char *url) { + CURL *handle = sn ? sn->handle : NULL; +#if LIBCURL_VERSION_NUM < 0x075200 + int cleanup_handle = 0; + if(!handle) { + handle = curl_easy_init(); + cleanup_handle = 1; + } +#endif + + char *esc = curl_easy_escape(handle, url, strlen(url)); + char *ret = esc ? strdup(esc) : NULL; + curl_free(esc); + +#if LIBCURL_VERSION_NUM < 0x075200 + if(cleanup_handle) { + curl_easy_cleanup(handle); + } +#endif + + return ret; +} + char* util_url_decode(DavSession *sn, const char *url) { - char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL); - char *ret = strdup(unesc); + CURL *handle = sn ? sn->handle : NULL; +#if LIBCURL_VERSION_NUM < 0x075200 + int cleanup_handle = 0; + if(!handle) { + handle = curl_easy_init(); + cleanup_handle = 1; + } +#endif + + char *unesc = curl_easy_unescape(handle, url, strlen(url), NULL); + char *ret = unesc ? strdup(unesc) : NULL; + curl_free(unesc); + +#if LIBCURL_VERSION_NUM < 0x075200 + if(cleanup_handle) { + curl_easy_cleanup(handle); + } +#endif + + return ret; +} + +cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url) { + CURL *handle = NULL; +#if LIBCURL_VERSION_NUM < 0x075200 + handle = curl_easy_init(); +#endif + + char *esc = curl_easy_escape(handle, url.ptr, url.length); + cxmutstr ret = esc ? cx_strdup_a(a, cx_str(esc)) : (cxmutstr){NULL, 0}; + curl_free(esc); + +#if LIBCURL_VERSION_NUM < 0x075200 + curl_easy_cleanup(handle); +#endif + return ret; +} + +cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url) { + CURL *handle = NULL; +#if LIBCURL_VERSION_NUM < 0x075200 + handle = curl_easy_init(); +#endif + + char *unesc = curl_easy_unescape(handle, url.ptr, url.length, NULL); + cxmutstr ret = unesc ? cx_strdup_a(a, cx_str(unesc)) : (cxmutstr){NULL, 0}; curl_free(unesc); + +#if LIBCURL_VERSION_NUM < 0x075200 + curl_easy_cleanup(handle); +#endif return ret; } diff --git a/libidav/utils.h b/libidav/utils.h index 8952c76..62a203a 100644 --- a/libidav/utils.h +++ b/libidav/utils.h @@ -76,7 +76,10 @@ char* util_url_base(const char *url); cxstring util_url_base_s(cxstring url); const char* util_url_path(const char *url); cxstring util_url_path_s(cxstring url); +char* util_url_encode(DavSession *sn, const char *url); char* util_url_decode(DavSession *sn, const char *url); +cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url); +cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url); const char* util_resource_name(const char *url); const char* util_resource_name_c(const char *url, char pathseparator); const char* util_path_file_name(const char *url); -- 2.43.5