ctai/src/ctai_markdown.cpp
2025-03-04 21:02:39 +08:00

40 lines
1.1 KiB
C++

#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()<<"处理的块:"<<result;
return process_code_blocks(result.toUtf8());
}
QString process_code_blocks(QString html) {
// 添加代码块样式
html.replace(
"<pre><code>",
"<pre><code style='display:block; padding:1em; "
"background-color:#f6f8fa; border-radius:4px; "
"font-family:Consolas,monospace; font-size:13px;'>"
);
// 添加内联代码样式
html.replace(
"<code>",
"<code style='padding:0.2em 0.4em; background-color:#f6f8fa; "
"border-radius:3px; font-family:Consolas,monospace;'>"
);
return html;
}
} // namespace ctai_parse