]> uap-core.de Git - note.git/commitdiff
url encode/decode markdown image links
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 11:24:21 +0000 (13:24 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 20 Apr 2025 11:24:21 +0000 (13:24 +0200)
application/editor.c
application/gtk-text.c
libidav/utils.c
libidav/utils.h

index cb39004049a2aeb5d8174f2e86c4b4db68da03d7..7bfdec4c01d1001f665a63499cd70ff5d6dfa085 100644 (file)
@@ -33,6 +33,7 @@
 #include <cx/buffer.h>
 #include <cx/tree.h>
 
+#include <libidav/utils.h> // 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);
+            }
         }
     }
     
index 9817f579101c1b55398eec7c94e235fc9911797f..56fef7cbe828d6398026ad4bdfb07f52b4454690 100644 (file)
@@ -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) {
index 0f87de8fff8967f33e28f879c738d0c0283250d2..c6f1eb1134e55ce3ba03030c7acafb556bd8ad27 100644 (file)
@@ -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;
 }
 
index 8952c7613faf5472556946094c8fdc3d23c25360..62a203a32ec9426ccf083f0a7e70522894d3a022 100644 (file)
@@ -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);