From: Olaf Wintermann Date: Tue, 18 Mar 2025 20:50:48 +0000 (+0100) Subject: convert textview links to markdown X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=c5f2b46d357e73a677a9a52af4204c627d1a8888;p=note.git convert textview links to markdown --- diff --git a/application/gtk-text.c b/application/gtk-text.c index 1393e78..f1cd7c1 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -439,6 +439,8 @@ void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) { */ static CxMap* tags2map(GSList *tags) { CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS); + int anon_tag = 0; + char buf[32]; while(tags) { GtkTextTag *t = tags->data; GValue name_value = G_VALUE_INIT; @@ -446,6 +448,9 @@ static CxMap* tags2map(GSList *tags) { const char *name = g_value_get_string(&name_value); if(name) { cxMapPut(map, name, t); + } else { + snprintf(buf, 32, "_%d", anon_tag++); + cxMapPut(map, buf, t); } tags = tags->next; } @@ -504,13 +509,22 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) { cxMapFree(prev_tags); prev_tags = begin_tags; + TextLink *link = NULL; // check all new tags and add tag prefix code CxMapIterator i = cxMapIterator(new_tags); cx_foreach(CxMapEntry *, entry, i) { + const char *name = entry->key->data; MDTag *t = cxMapGet(markdown_tags, *entry->key); if(t && t->begin) { cxBufferPutString(&out, t->begin); } + if(name[0] == '_') { + GtkTextTag *tag = entry->value; + link = g_object_get_data(G_OBJECT(tag), "link"); + if(link) { + cxBufferPut(&out, '['); + } + } } cxMapFree(new_tags); @@ -532,6 +546,11 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) { cxBufferPutString(&out, t->end); } } + if(link) { + cxBufferPutString(&out, "]("); + cxBufferPutString(&out, link->link); + cxBufferPut(&out, ')'); + } cxMapFree(begin_tags2); cxMapFree(end_tags);