]> uap-core.de Git - note.git/commitdiff
add support for markdown image nodes, prepare editor loading images from markdown
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 4 Apr 2025 20:05:10 +0000 (22:05 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 4 Apr 2025 20:05:10 +0000 (22:05 +0200)
application/editor.c
application/editor.h
application/gtk-text.c

index c3694121f9d03a1e0e6b1a80c571e724b46871a1..f8d8e09b86b2e12e16440a6d36790248f1b6393b 100644 (file)
@@ -158,6 +158,11 @@ static int md_enter_span(MD_SPANTYPE type, void* detail, void* userdata) {
         if(link && link->href.size > 0) {
             node->link = cx_strdup_a(data->a, cx_strn(link->href.text, link->href.size));
         }
+    } 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));
+        }
     }
     
     return 0;
@@ -197,7 +202,7 @@ static int md_text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* us
     // if the previous node is a text node, we can merge the nodes
     // there are 2 cases: the root node is a text node (current->text.ptr)
     // or the current node is not a text node, but has text children
-    if(current) {
+    if(current) {  
         MDNode *prev_text_node = NULL;
         if(current->text.ptr) {
             prev_text_node = current;
@@ -300,6 +305,13 @@ static void linearize_mdnodes(CxBuffer *buf, CxList *sections, MDNode *n, int de
     
     if(n->text.ptr) {
         cxBufferWrite(n->text.ptr, 1, n->text.length, buf);
+    } else if(n->type == MD_SPAN_IMG) {
+        MDDocStyleSection sec;
+        sec.pos = buf->pos;
+        sec.length = MDDocStyleSection_IMAGE;
+        sec.style = n->children_begin ? n->children_begin->text.ptr : NULL;
+        sec.link = n->link.ptr;
+        cxListAdd(sections, &sec);
     } else {
         size_t start_pos = buf->pos;
         
index bab1836fa05e963ca0b550155eb4381d1f8c797b..b5745d7d9e78e18cfd7621be8c5037c12b4dc967 100644 (file)
@@ -51,6 +51,8 @@ extern "C" {
 #define EDITOR_STYLE_STRONG     "strong"
 #define EDITOR_STYLE_CODE       "code"
 #define EDITOR_STYLE_LINK       "link"
+
+#define MDDocStyleSection_IMAGE -1
     
     
 #define MD_MAX_DEPTH 50
index 588773bd819489ccdf3f189f64f9757a48c17871..91d2c9e96c1ee548bb3777b3218bab572f63c031 100644 (file)
@@ -654,6 +654,11 @@ void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) {
     
     CxIterator i = cxListIterator(styles);
     cx_foreach(MDDocStyleSection*, sec, i) {
+        if(sec->length == MDDocStyleSection_IMAGE) {
+            // TODO: insert image
+            continue;
+        }
+        
         GtkTextIter begin, end;
         gtk_text_buffer_get_iter_at_offset(buffer, &begin, sec->pos);
         gtk_text_buffer_get_iter_at_offset(buffer, &end, sec->pos + sec->length);