增加三方库的头文件
This commit is contained in:
parent
1113c5332a
commit
6890c4b71d
@ -66,8 +66,11 @@ find_package(cmark CONFIG REQUIRED)
|
||||
|
||||
#cmark-gfm
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cmark-gfm)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/src)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/extensions)
|
||||
#MicroTeX
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/MicroTeX)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/MicroTeX/lib)
|
||||
|
||||
#查找QT模块
|
||||
FIND_PACKAGE(Qt6 REQUIRED Core Gui Widgets)
|
||||
|
||||
@ -1,111 +0,0 @@
|
||||
#include "ctai_cmark-gfm.h"
|
||||
|
||||
namespace ctai_cmark_gfm {
|
||||
|
||||
MarkdownStreamProcessor::MarkdownStreamProcessor(QTextEdit* textEdit)
|
||||
: m_textEdit(textEdit)
|
||||
, m_buffer()
|
||||
, m_tempText() {
|
||||
}
|
||||
|
||||
MarkdownStreamProcessor::~MarkdownStreamProcessor() {
|
||||
// 确保在销毁时处理所有剩余的缓冲内容
|
||||
if (!m_buffer.isEmpty()) {
|
||||
handleBufferOverflow();
|
||||
}
|
||||
}
|
||||
|
||||
void MarkdownStreamProcessor::appendMarkdown(const QString& text) {
|
||||
// 将新内容添加到缓冲区
|
||||
QString newBuffer = m_buffer + text;
|
||||
|
||||
// 检查是否超出缓冲区大小
|
||||
if (newBuffer.size() > BUFFER_SIZE) {
|
||||
handleBufferOverflow();
|
||||
newBuffer = text;
|
||||
}
|
||||
|
||||
// 检查是否只包含开始标记
|
||||
if (hasMarkdownStart(text) && !hasMarkdownEnd(text)) {
|
||||
m_buffer = newBuffer;
|
||||
m_tempText = text;
|
||||
m_textEdit->append(text);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否包含结束标记
|
||||
if (hasMarkdownEnd(text)) {
|
||||
QString fullContent = m_buffer + text;
|
||||
QString rendered = renderMarkdown(fullContent);
|
||||
replaceTempText(rendered);
|
||||
m_buffer.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
m_buffer = newBuffer;
|
||||
}
|
||||
|
||||
bool MarkdownStreamProcessor::hasMarkdownStart(const QString& text) {
|
||||
// 检查常见的Markdown开始标记
|
||||
static const QStringList startMarkers = {
|
||||
"```", "~~~", "#", ">", "*", "_", "-", "+", "[", "|"
|
||||
};
|
||||
|
||||
for (const QString& marker : startMarkers) {
|
||||
if (text.contains(marker)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MarkdownStreamProcessor::hasMarkdownEnd(const QString& text) {
|
||||
// 检查常见的Markdown结束标记
|
||||
static const QStringList endMarkers = {
|
||||
"```", "~~~", "\n\n", "]", "*", "_"
|
||||
};
|
||||
|
||||
for (const QString& marker : endMarkers) {
|
||||
if (text.contains(marker)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QString MarkdownStreamProcessor::renderMarkdown(const QString& text) {
|
||||
QByteArray textUtf8 = text.toUtf8();
|
||||
char* rendered = cmark_markdown_to_html(
|
||||
textUtf8.constData(),
|
||||
textUtf8.size(),
|
||||
CMARK_OPT_DEFAULT
|
||||
);
|
||||
|
||||
QString result = QString::fromUtf8(rendered);
|
||||
free(rendered);
|
||||
return result;
|
||||
}
|
||||
|
||||
void MarkdownStreamProcessor::handleBufferOverflow() {
|
||||
if (!m_buffer.isEmpty()) {
|
||||
QString rendered = renderMarkdown(m_buffer);
|
||||
replaceTempText(rendered);
|
||||
m_buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void MarkdownStreamProcessor::replaceTempText(const QString& newContent) {
|
||||
if (!m_tempText.isEmpty()) {
|
||||
// 获取当前文本
|
||||
QString currentText = m_textEdit->toPlainText();
|
||||
// 替换临时文本
|
||||
currentText.replace(m_tempText, newContent);
|
||||
m_textEdit->setHtml(currentText);
|
||||
m_tempText.clear();
|
||||
} else {
|
||||
m_textEdit->append(newContent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
#ifndef CTAI_CMARK_GFM_H
|
||||
#define CTAI_CMARK_GFM_H
|
||||
|
||||
#include <cmark-gfm.h>
|
||||
#include <QString>
|
||||
#include <QTextEdit>
|
||||
#include <memory>
|
||||
|
||||
namespace ctai_cmark_gfm {
|
||||
|
||||
class MarkdownStreamProcessor {
|
||||
public:
|
||||
explicit MarkdownStreamProcessor(QTextEdit* textEdit);
|
||||
~MarkdownStreamProcessor();
|
||||
|
||||
// 追加内容并处理Markdown
|
||||
void appendMarkdown(const QString& text);
|
||||
|
||||
private:
|
||||
static constexpr size_t BUFFER_SIZE = 4096;
|
||||
|
||||
QTextEdit* m_textEdit;
|
||||
QString m_buffer;
|
||||
QString m_tempText;
|
||||
|
||||
// 检查是否包含Markdown开始标记
|
||||
bool hasMarkdownStart(const QString& text);
|
||||
|
||||
// 检查是否包含Markdown结束标记
|
||||
bool hasMarkdownEnd(const QString& text);
|
||||
|
||||
// 渲染Markdown内容
|
||||
QString renderMarkdown(const QString& text);
|
||||
|
||||
// 处理缓冲区溢出
|
||||
void handleBufferOverflow();
|
||||
|
||||
// 替换临时文本
|
||||
void replaceTempText(const QString& newContent);
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user