From: Olaf Wintermann Date: Mon, 16 Mar 2026 18:42:42 +0000 (+0100) Subject: fix markdown code block generation X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=HEAD;p=note.git fix markdown code block generation --- diff --git a/application/gtk-text.c b/application/gtk-text.c index 003822c..21e9d17 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -55,6 +55,7 @@ #define g_object_get_data g_object_get_data static CxMap *markdown_tags; +static MDTag *code_block_tag; static void editor_set_cursor_cb( GtkTextBuffer *buffer, @@ -135,6 +136,8 @@ void editor_global_init() { cxMapPut(markdown_tags, EDITOR_STYLE_EMPHASIS, &((MDTag){"*", "*"})); cxMapPut(markdown_tags, EDITOR_STYLE_UNDERLINE, &((MDTag){"_", "_"})); cxMapPut(markdown_tags, EDITOR_STYLE_CODE, &((MDTag){"`", "`"})); + + code_block_tag = cxMapGet(markdown_tags, EDITOR_STYLE_CODE_BLOCK); } void editor_init_textview(UiObject *obj, UIWIDGET textview) { @@ -1240,6 +1243,7 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) { } gchar *text = gtk_text_buffer_get_text(buffer, &start, &iter, TRUE); + gboolean is_code_block = false; GSList *tags = gtk_text_iter_get_tags(&start); CxMap *begin_tags = tags2map(tags); @@ -1258,6 +1262,10 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) { cx_foreach(CxMapEntry *, entry, i) { const char *name = entry->key->data; MDTag *t = cxMapGet(markdown_tags, *entry->key); + if(t == code_block_tag) { + is_code_block = true; + break; + } if(t && t->begin) { cxBufferPutString(&out, t->begin); } @@ -1274,7 +1282,19 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a) { // add content printf("range: {%s}\n\n", text); - cxBufferPutString(&out, text); + if(!is_code_block) { + cxBufferPutString(&out, text); + } else { + int code_block_start = 0; + size_t text_len = strlen(text); + for(int i=0;i\n" + " \n" + " int main() {" + " }\n" + "\nend\n")); + } +} + + CX_TEST(test_editor_load_markdown_link) { CX_TEST_DO { CX_TEST_CALL_SUBROUTINE(test_editor_markdown_load_get, cx_mutstr("Link [link1](https://example.com/link1)\n[link2][1]\n\n[1]: https://example.com/link2")); diff --git a/application/tests/test-editor.h b/application/tests/test-editor.h index c983f9a..e042b85 100644 --- a/application/tests/test-editor.h +++ b/application/tests/test-editor.h @@ -48,6 +48,7 @@ CX_TEST(test_editor_load_markdown_list1); CX_TEST(test_editor_load_markdown_list2); CX_TEST(test_editor_load_markdown_span1); CX_TEST(test_editor_load_markdown_code1); +CX_TEST(test_editor_load_markdown_code2); CX_TEST(test_editor_load_markdown_link); #ifdef __cplusplus diff --git a/application/tests/testmain.c b/application/tests/testmain.c index 8cacb25..748909e 100644 --- a/application/tests/testmain.c +++ b/application/tests/testmain.c @@ -89,6 +89,7 @@ int main(int argc, char **argv) { cx_test_register(suite, test_editor_load_markdown_list2); cx_test_register(suite, test_editor_load_markdown_span1); cx_test_register(suite, test_editor_load_markdown_code1); + cx_test_register(suite, test_editor_load_markdown_code2); cx_test_register(suite, test_editor_load_markdown_link); cx_test_register(suite, test_text_search_strcasestr);