运行并找到编辑控件
复用桌面应用工程,使用 --evergreen 启动。左侧笔记条目主要用于布局;本课检查的是当前笔记的编辑和预览。
.\build\Release\agent_studio.exe --evergreen跟踪当前编辑值
在 main.cpp 中找到 HTMLTextAreaElement 与输入监听。读取 value() 后传给预览函数,不读取 textarea 的初始 HTML 文本。输入变化和保存是两个不同动作。
预览函数的完整范围
以下是工程中的转换函数。它先转义文本,再处理标题、段落和无序列表;没有承诺完整 Markdown、链接解析或 HTML 直通。
#pragma once
#include <sstream>
#include <string>
// 演示使用的小型 Markdown 子集:标题、段落与无序条目。输入始终作为文本转义。
inline std::string renderMarkdown(const std::string& source) {
std::istringstream stream(source);
std::string line, result;
while (std::getline(stream, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
if (line.empty()) continue;
std::string tag = "p", attributes;
if (line.compare(0, 3, "## ") == 0) { tag = "h2"; line.erase(0, 3); }
else if (line.compare(0, 2, "# ") == 0) { tag = "h1"; line.erase(0, 2); }
else if (line.compare(0, 2, "- ") == 0) { attributes = " class=\"md-list\""; line = "• " + line.substr(2); }
result += "<" + tag + attributes + ">";
for (char ch : line) {
if (ch == '&') result += "&";
else if (ch == '<') result += "<";
else if (ch == '>') result += ">";
else result += ch;
}
result += "</" + tag + ">";
}
return result;
}
用四组输入检查预览
每次只改变一种语法,方便定位转换问题。
- 输入 # Title,检查标题。
- 输入普通段落与 - item,检查段落和列表。
- 输入 <script> 或 <img> 形式文字,确认显示文本,不成为可执行节点。
- 清空编辑器后新建、保存,再重启确认会话数据边界。
运行模型测试并扩展一项语法
CTest 的 markdown_preview 检查转换和转义。尝试添加一种语法时,先写输入与预期,再修改转换器;不要直接允许用户 HTML 绕过转义。
ctest --test-dir build -C Release --output-on-failure文档有误或遗漏?
反馈此页问题 →