Skip to content

feat(ChatBot): add MarkdownContent component#399

Merged
ArgoZhang merged 1 commit intomasterfrom
refactor-markdown
Mar 31, 2025
Merged

feat(ChatBot): add MarkdownContent component#399
ArgoZhang merged 1 commit intomasterfrom
refactor-markdown

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Mar 31, 2025

Link issues

fixes #398

Summary By Copilot

This pull request introduces a new component, BootstrapBlazor.ChatBot, to the BootstrapBlazor project. The changes include updates to the solution file and the addition of new project files for the chatbot component.

Solution file updates:

  • Added BootstrapBlazor.ChatBot project to the solution (BootstrapBlazor.Extensions.sln).
  • Included configuration settings for the new project in the Global section. [1] [2]

New project files for BootstrapBlazor.ChatBot:

  • Created the project file BootstrapBlazor.ChatBot.csproj with necessary dependencies and metadata.
  • Added MarkdownContent.razor component to render markdown content and included necessary styles.
  • Implemented the logic for the MarkdownContent component in MarkdownContent.razor.cs, including markdown processing and custom tag handling.
  • Added _Imports.razor to include necessary namespaces for the new component.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Add a new MarkdownContent component to the BootstrapBlazor.ChatBot project for rendering and processing markdown content with advanced features

New Features:

  • Introduced a new MarkdownContent component that supports advanced markdown rendering with syntax highlighting, emoji support, and custom tag handling

Enhancements:

  • Implemented markdown processing with support for code syntax highlighting, auto-links, emojis, media links, citations, and mathematical notation
  • Added custom handling for special markdown tags like 'think' tags

@bb-auto bb-auto Bot added the enhancement New feature or request label Mar 31, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 31, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new MarkdownContent component within the BootstrapBlazor.ChatBot project. The component is designed to render markdown content, including handling custom tags and code block styling with a copy button. The pull request includes the component's implementation, styling, project configuration, and solution integration.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Added a new MarkdownContent component to render markdown content.
  • Created MarkdownContent.razor to define the component's UI.
  • Implemented MarkdownContent.razor.cs to handle markdown processing and custom tag handling.
  • Added a new _Imports.razor to include necessary namespaces for the new component.
  • Added styles for the think class within the MarkdownContent.razor file.
src/components/BootstrapBlazor.ChatBot/MarkdownContent.razor.cs
src/components/BootstrapBlazor.ChatBot/MarkdownContent.razor
src/components/BootstrapBlazor.ChatBot/_Imports.razor
Configured the new BootstrapBlazor.ChatBot project within the solution.
  • Added the BootstrapBlazor.ChatBot project to the solution file.
  • Included configuration settings for the new project in the Global section of the solution file.
BootstrapBlazor.Extensions.sln
Created a project file for the BootstrapBlazor.ChatBot component.
  • Created the BootstrapBlazor.ChatBot.csproj file.
  • Added necessary dependencies and metadata to the project file.
src/components/BootstrapBlazor.ChatBot/BootstrapBlazor.ChatBot.csproj

Assessment against linked issues

Issue Objective Addressed Explanation
#398 Create a new MarkdownContent component in the BootstrapBlazor project.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added this to the v9.2.0 milestone Mar 31, 2025
@ArgoZhang ArgoZhang merged commit 02a9b29 into master Mar 31, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the refactor-markdown branch March 31, 2025 06:12
Copy link
Copy Markdown

@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 @ArgoZhang - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a more specific exception type than Exception in the GetMarkdown method's catch block.
  • The HandleUnclosedThinkTags method could potentially be simplified using HtmlAgilityPack.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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.

}
catch (Exception ex)
{
Logger.LogError(ex, "{GetMakrDown} method throw exception", nameof(GetMarkdown));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (typo): Fix typo in error log placeholder.

The placeholder '{GetMakrDown}' seems to be a typo. It would be clearer and more consistent to use '{GetMarkdown}' in order to accurately reflect the method name.

Suggested change
Logger.LogError(ex, "{GetMakrDown} method throw exception", nameof(GetMarkdown));
Logger.LogError(ex, "{GetMarkdown} method throw exception", nameof(GetMarkdown));

return html;
}

private static string HandleUnclosedThinkTags(string content)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (complexity): Consider using an HTML parser library like HtmlAgilityPack to handle unclosed "think" tags instead of regex and string manipulation.

One way to reduce the “magic” of nested regex and manual string concatenation is to encapsulate the “think” tag repair logic into a small helper that uses a lightweight HTML parser rather than ad hoc regex-splitting. For example, you can replace your current implementation with one based on [HtmlAgilityPack](https://html-agility-pack.net/) to “repair” unclosed tags. This makes the intent explicit and the code easier to maintain.

For example, you might create a helper like this:

```csharp
using HtmlAgilityPack;

private static string FixThinkTags(string content)
{
    if (string.IsNullOrEmpty(content)) return content;

    // Load the content into an HTML document
    var doc = new HtmlDocument();
    doc.OptionFixNestedTags = true;
    doc.LoadHtml(content);

    // Select all nodes with the "think" tag.
    // (HtmlAgilityPack will auto-close unclosed tags on load)
    var thinkNodes = doc.DocumentNode.SelectNodes("//think");
    if (thinkNodes != null)
    {
        foreach (var node in thinkNodes)
        {
            // Optionally, further manipulate the node here if needed...
            // e.g. add a div wrapper if that was your intent when converting via regex
            var div = HtmlNode.CreateNode("<div class=\"think\"></div>");
            div.InnerHtml = node.InnerHtml;
            node.ParentNode.ReplaceChild(div, node);
        }
    }
    return doc.DocumentNode.OuterHtml;
}

Then in your GetMarkdown method replace the call to HandleUnclosedThinkTags with:

toHtml = FixThinkTags(toHtml);

This refactoring does not remove any functionality but delegates tag fixing to a dedicated parser which is easier to understand, unit test, and maintain. If you do not want to add a dependency now, consider extracting the regex/string-splitting logic into a separate method with clear inline documentation representing a “repair” step. Either way, encapsulating this complexity into a focused helper reduces the maintenance burden.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ChatBot): add MarkdownContent component

1 participant