From: Olaf Wintermann Date: Fri, 4 Apr 2025 20:05:10 +0000 (+0200) Subject: add support for markdown image nodes, prepare editor loading images from markdown X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=a2feffc6a73f8f80bba267b06cf5207251d69bb8;p=note.git add support for markdown image nodes, prepare editor loading images from markdown --- diff --git a/application/editor.c b/application/editor.c index c369412..f8d8e09 100644 --- a/application/editor.c +++ b/application/editor.c @@ -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; diff --git a/application/editor.h b/application/editor.h index bab1836..b5745d7 100644 --- a/application/editor.h +++ b/application/editor.h @@ -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 diff --git a/application/gtk-text.c b/application/gtk-text.c index 588773b..91d2c9e 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -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);