Skip to content

fix(webchat): render standalone HTML replies as code blocks#6074

Open
stablegenius49 wants to merge 1 commit intoAstrBotDevs:masterfrom
stablegenius49:pr-factory/issue-5988-html-code-render
Open

fix(webchat): render standalone HTML replies as code blocks#6074
stablegenius49 wants to merge 1 commit intoAstrBotDevs:masterfrom
stablegenius49:pr-factory/issue-5988-html-code-render

Conversation

@stablegenius49
Copy link
Contributor

@stablegenius49 stablegenius49 commented Mar 11, 2026

Summary

  • detect plain-text replies that look like standalone HTML documents
  • wrap those replies in an explicit html code fence before passing them to the markdown renderer
  • keep existing markdown/code-fence behavior unchanged for normal replies

Verification

  • corepack pnpm exec vue-tsc --noEmit
  • corepack pnpm exec vite build

Closes #5988

Summary by Sourcery

规范对包含完整 HTML 文档的纯文本聊天回复的渲染方式,使其以代码形式显示,而不是被当作可解释的 HTML。

Bug Fixes:

  • 检测独立的、类似 HTML 的纯文本回复,并将其渲染为 HTML 代码块,而不是原始的 Markdown 内容。

Enhancements:

  • 增加 Markdown 内容规范化处理,在保留现有的围栏代码块和 Markdown 行为的同时,将独立的 HTML 文档包裹在显式的 HTML 代码围栏中。
Original summary in English

Summary by Sourcery

Normalize rendering of plain-text chat replies that contain full HTML documents so they are displayed as code rather than interpreted HTML.

Bug Fixes:

  • Detect standalone HTML-like plain-text replies and render them as HTML code blocks instead of raw markdown content.

Enhancements:

  • Add markdown content normalization that preserves existing fenced-code and markdown behavior while wrapping standalone HTML documents in explicit HTML code fences.

@auto-assign auto-assign bot requested review from Soulter and anka-afk March 11, 2026 18:50
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 11, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the chat message rendering by intelligently handling plain-text replies that contain full HTML structures. Previously, such content might have been misinterpreted or rendered incorrectly by the markdown parser. The changes introduce a preprocessing step to identify these HTML documents and explicitly format them as HTML code blocks, ensuring they are displayed as intended without affecting the rendering of standard markdown or existing code fences.

Highlights

  • HTML Reply Detection: Implemented logic to detect plain-text replies that resemble standalone HTML documents.
  • HTML Code Block Wrapping: Automatically wraps detected standalone HTML replies in an explicit HTML code fence before they are passed to the markdown renderer.
  • Preserved Existing Behavior: Ensured that the existing markdown and code-fence rendering behavior remains unchanged for normal replies that are not standalone HTML.
Changelog
  • dashboard/src/components/chat/message_list_comps/MessagePartsRenderer.vue
    • Modified the MarkdownRender component to use a new content normalization function.
    • Added isMarkdownCodeFence helper function to check for existing markdown code fences.
    • Introduced looksLikeStandaloneHtml helper function to identify text that appears to be a standalone HTML document.
    • Implemented normalizeMarkdownContent function to wrap standalone HTML text in an HTML code block if not already a code fence.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the feature:chatui The bug / feature is about astrbot's chatui, webchat label Mar 11, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出一些整体性的反馈:

  • normalizeMarkdownContent 中,生成的代码块使用了四个反引号(```````),而不是通常的三个;除非你的 Markdown 渲染器明确需要使用四反引号代码块,否则这很可能会导致渲染不正确,应当与 MarkdownRender` 期望的代码块格式保持一致。
  • isMarkdownCodeFence 只检查(去除首尾空白后)字符串最开头位置的代码块标记,因此如果在代码块标记前存在任何前导字符(例如注释或 BOM),内容就会被当作 HTML 重新包装;如果在实际使用中可能出现这种情况,建议让这段检测逻辑更健壮一些。
面向 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- In `normalizeMarkdownContent`, the generated code fence uses four backticks (```\`\`\`\``) rather than the usual three; unless your markdown renderer specifically expects 4-backtick fences, this is likely to render incorrectly and should be aligned with whatever fence style `MarkdownRender` expects.
- `isMarkdownCodeFence` only checks for fences at the very beginning of the (trimmed) string, so any leading characters (e.g., comments or BOM) before a fence will cause the content to be rewrapped as HTML; consider making the detection a bit more robust if such cases are possible in practice.

Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • In normalizeMarkdownContent, the generated code fence uses four backticks (```````) rather than the usual three; unless your markdown renderer specifically expects 4-backtick fences, this is likely to render incorrectly and should be aligned with whatever fence style MarkdownRender` expects.
  • isMarkdownCodeFence only checks for fences at the very beginning of the (trimmed) string, so any leading characters (e.g., comments or BOM) before a fence will cause the content to be rewrapped as HTML; consider making the detection a bit more robust if such cases are possible in practice.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `normalizeMarkdownContent`, the generated code fence uses four backticks (```\`\`\`\``) rather than the usual three; unless your markdown renderer specifically expects 4-backtick fences, this is likely to render incorrectly and should be aligned with whatever fence style `MarkdownRender` expects.
- `isMarkdownCodeFence` only checks for fences at the very beginning of the (trimmed) string, so any leading characters (e.g., comments or BOM) before a fence will cause the content to be rewrapped as HTML; consider making the detection a bit more robust if such cases are possible in practice.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to detect and render standalone HTML documents in chat replies as code blocks, preventing them from being interpreted by the browser. The implementation adds helper functions to identify potential HTML content and wraps it in a markdown code fence. My review includes a suggestion to improve the accuracy of the HTML detection logic by ensuring it only matches content that starts with HTML tags, thus avoiding false positives.

const looksLikeStandaloneHtml = (text) => {
const normalized = text.trim();
if (!normalized) return false;
if (!/(<!doctype\s+html|<html\b|<head\b|<body\b)/i.test(normalized)) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To more accurately detect standalone HTML documents, it's better to anchor the regex to the start of the string. This ensures that we only match text that begins with an HTML tag, preventing false positives on content that merely contains HTML tags somewhere in the middle.

    if (!/^(<!doctype\s+html|<html\b|<head\b|<body\b)/i.test(normalized)) return false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature:chatui The bug / feature is about astrbot's chatui, webchat size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] WebChat 界面中输出一个完整的 HTML 页面时,未能正确识别该内容

1 participant