From 3c3d360b55199fe6cf2acf77eb676806f8ccc31a Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Thu, 6 Mar 2025 15:38:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=9E=84=E5=BB=BAcmark-gfm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 ++-- src/ctai_cmark_gfm.cpp | 16 ++++++++++++++ src/ctai_cmark_gfm.h | 24 +++++++++++++++++++++ src/ctai_history_textedit.cpp | 16 ++++++++------ src/ctai_history_textedit.h | 7 +++--- src/ctai_markdown.cpp | 40 ----------------------------------- src/ctai_markdown.h | 14 ------------ 7 files changed, 55 insertions(+), 66 deletions(-) create mode 100644 src/ctai_cmark_gfm.cpp create mode 100644 src/ctai_cmark_gfm.h delete mode 100644 src/ctai_markdown.cpp delete mode 100644 src/ctai_markdown.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fe1584..bae9e19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ include_directories(${JSON}/include) find_package(CURL REQUIRED) #cmark -find_package(cmark CONFIG REQUIRED) +#find_package(cmark CONFIG REQUIRED) #cmark-gfm add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cmark-gfm) @@ -151,7 +151,7 @@ target_link_libraries( Qt6::Gui Qt6::Widgets CURL::libcurl - cmark::cmark + #cmark::cmark libcmark-gfm-extensions_static libcmark-gfm_static microtex diff --git a/src/ctai_cmark_gfm.cpp b/src/ctai_cmark_gfm.cpp new file mode 100644 index 0000000..5311620 --- /dev/null +++ b/src/ctai_cmark_gfm.cpp @@ -0,0 +1,16 @@ +#include "ctai_cmark_gfm.h" + +ctai_cmark_gfm::ctai_cmark_gfm(QTextEdit *textEdit) + : m_textEdit(textEdit), m_buffer() +{ +} + +ctai_cmark_gfm::~ctai_cmark_gfm() +{ + +} + +void ctai_cmark_gfm::appendMarkdown(const QString &markdown) +{ + +} diff --git a/src/ctai_cmark_gfm.h b/src/ctai_cmark_gfm.h new file mode 100644 index 0000000..9a0d2fb --- /dev/null +++ b/src/ctai_cmark_gfm.h @@ -0,0 +1,24 @@ +#ifndef CTAI_CMARK_GFM_H +#define CTAI_CMARK_GFM_H + +#include "cmark-gfm.h" +#include "cmark-gfm-extension_api.h" +#include "cmark-gfm-core-extensions.h" +#include +#include + +class ctai_cmark_gfm { +public: + explicit ctai_cmark_gfm(QTextEdit* textEdit); + ~ctai_cmark_gfm(); + + // 追加内容并处理Markdown + void appendMarkdown(const QString& text); + +private: + static constexpr size_t BUFFER_SIZE = 4096; + QTextEdit* m_textEdit; + QString m_buffer; +}; + +#endif \ No newline at end of file diff --git a/src/ctai_history_textedit.cpp b/src/ctai_history_textedit.cpp index 7faee6a..4d2ef2c 100644 --- a/src/ctai_history_textedit.cpp +++ b/src/ctai_history_textedit.cpp @@ -91,6 +91,7 @@ void ctai_history_textedit::init_layout(msg_type msg_type_mode) // 5. 历史信息QTextEdit historyLayout = new QVBoxLayout(); m_msg_history = new QTextEdit(); + m_cmark_gfm=new ctai_cmark_gfm(m_msg_history); m_msg_history->setAcceptRichText(true); m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_msg_history->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); @@ -126,7 +127,7 @@ void ctai_history_textedit::connect_signals(msg_type msg_type_mode) { connect(m_msg_user_del, SIGNAL(clicked()), this, SLOT(on_delete_clicked())); } - connect(m_msg_history->document(), SIGNAL(contentsChanged()), this, SLOT(on_text_height())); + connect(m_msg_history, SIGNAL(textChanged()), this, SLOT(on_sync_text_height())); } // tokens按钮实现功能的槽函数 @@ -196,6 +197,7 @@ void ctai_history_textedit::on_save_clicked() m_msg_save_menu->addAction(m_msg_save_pdf); m_msg_save_menu->exec(QCursor::pos()); } + // 实现折叠功能的槽函数 void ctai_history_textedit::on_fold_clicked() { @@ -206,7 +208,7 @@ void ctai_history_textedit::on_fold_clicked() updateGeometry(); emit row_height_changed(m_is_folded); } -void ctai_history_textedit::on_text_height() +void ctai_history_textedit::on_sync_text_height() { // 防抖动处理:如果上次更新在100ms内,则延迟处理 static QTimer debounceTimer; @@ -243,8 +245,8 @@ void ctai_history_textedit::on_text_height() { QTimer::singleShot(100, [parent]() { - QEvent e(QEvent::LayoutRequest); - QApplication::sendEvent(parent, &e); }); + QEvent e(QEvent::LayoutRequest); + QApplication::sendEvent(parent, &e); }); } } } @@ -254,7 +256,7 @@ void ctai_history_textedit::on_text_height() void ctai_history_textedit::add_user_message(const model_data &message) { QString disp_data; - disp_data = ctai_markdown::md_to_html(QSL(message.send_user_data)); + disp_data = QSL(message.send_user_data); m_msg_sned_id = QSL(message.send_user_id); m_msg_history->setHtml(disp_data); } @@ -269,12 +271,12 @@ void ctai_history_textedit::add_system_message(const model_data &message) { // 流式模式下追加内容 m_current_content += message.postback_model_data; - m_msg_history->setHtml(ctai_markdown::md_to_html(m_current_content)); + m_cmark_gfm->appendMarkdown(QSL(message.postback_model_data)); } else { // 非流式模式直接设置全部内容 - disp_data = ctai_markdown::md_to_html(QSL(message.postback_model_data)); + disp_data = QSL(message.postback_model_data); m_msg_history->setHtml(disp_data); m_current_content = QSL(message.postback_model_data); } diff --git a/src/ctai_history_textedit.h b/src/ctai_history_textedit.h index ff7e762..4dca3ec 100644 --- a/src/ctai_history_textedit.h +++ b/src/ctai_history_textedit.h @@ -29,7 +29,7 @@ #include // 项目相关头文件 #include "ctai_base.h" -#include "ctai_markdown.h" +#include "ctai_cmark_gfm.h" typedef struct tokens_args{ QString prompt_tokens={}; @@ -63,13 +63,14 @@ private slots: void on_save_html(); // 保存html void on_save_markdown(); // 保存markdown void on_save_pdf(); // 保存pdf - void on_text_height(); + void on_sync_text_height(); signals: void delete_requested(QString); // 请求删除此消息 void row_height_changed(bool); private: - void connect_signals(msg_type msg_type_mode); // 连接信号和槽 + void connect_signals(msg_type msg_type_mode);// 连接信号和槽 private: + ctai_cmark_gfm* m_cmark_gfm; QFrame *hLine; QSpacerItem *sparcer_item = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed); QVBoxLayout *mainLayout={}; diff --git a/src/ctai_markdown.cpp b/src/ctai_markdown.cpp deleted file mode 100644 index f72eb46..0000000 --- a/src/ctai_markdown.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "ctai_markdown.h" - -namespace ctai_markdown { - -QString md_to_html(const QString& text) { - // 转换为UTF-8编码的字符串 - QByteArray markdown = text.toUtf8(); - // 使用cmark解析Markdown - char *html = cmark_markdown_to_html( - markdown.constData(), - markdown.size(), - CMARK_OPT_DEFAULT - ); - - // 转换回QString并处理代码块 - QString result = QString::fromUtf8(html); - free(html); - //qDebug()<<"处理的块:"<", - "
"
-    );
-    
-    // 添加内联代码样式
-    html.replace(
-        "",
-        ""
-    );
-    
-    return html;
-}
-
-} // namespace ctai_parse
\ No newline at end of file
diff --git a/src/ctai_markdown.h b/src/ctai_markdown.h
deleted file mode 100644
index d70068e..0000000
--- a/src/ctai_markdown.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef CTAI_MARKDOWN_H
-#define CTAI_MARKDOWN_H
-
-#include 
-#include 
-#include 
-
-namespace ctai_markdown {
-    // Markdown 转 HTML
-    QString md_to_html(const QString& text);
-    // 处理代码块样式
-    QString process_code_blocks(QString html);
-}
-#endif // CTAI_PARSESTRING_H
\ No newline at end of file