|
| 1 | +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Website: https://www.blazor.zone or https://argozhang.github.io/ |
| 4 | + |
| 5 | +using ColorCode; |
| 6 | +using ColorCode.Compilation.Languages; |
| 7 | +using ColorCode.Styling; |
| 8 | +using Markdig; |
| 9 | +using Markdown.ColorCode; |
| 10 | +using Microsoft.AspNetCore.Components; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | + |
| 14 | +namespace BootstrapBlazor.Components; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// MarkdownContent component |
| 18 | +/// </summary> |
| 19 | +public partial class MarkdownContent |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Gets or sets the content. Default value is null. |
| 23 | + /// </summary> |
| 24 | + [Parameter] |
| 25 | + [EditorRequired] |
| 26 | + public string? Content { get; set; } |
| 27 | + |
| 28 | + [Inject, NotNull] |
| 29 | + private ILogger<MarkdownContent>? Logger { get; set; } |
| 30 | + |
| 31 | + private string? _markdown; |
| 32 | + private MarkdownPipeline? _markdownPipeline; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// <inheritdoc/> |
| 36 | + /// </summary> |
| 37 | + protected override void OnInitialized() |
| 38 | + { |
| 39 | + base.OnInitialized(); |
| 40 | + |
| 41 | + _markdownPipeline = new MarkdownPipelineBuilder() |
| 42 | + .UsePipeTables() |
| 43 | + .UseAdvancedExtensions() |
| 44 | + .UseColorCode(styleDictionary: StyleDictionary.DefaultLight, additionalLanguages: new List<ILanguage>() |
| 45 | + { |
| 46 | + new Json(), |
| 47 | + new CSharp(), |
| 48 | + new Cpp(), |
| 49 | + new Css(), |
| 50 | + new Html(), |
| 51 | + new JavaScript(), |
| 52 | + new Php(), |
| 53 | + }) |
| 54 | + .UseAutoLinks() |
| 55 | + .UseEmojiAndSmiley() |
| 56 | + .UseMediaLinks() |
| 57 | + .UseCitations() |
| 58 | + .UseMathematics() |
| 59 | + .UseDiagrams() |
| 60 | + .Build(); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// <inheritdoc/> |
| 65 | + /// </summary> |
| 66 | + protected override void OnParametersSet() |
| 67 | + { |
| 68 | + base.OnParametersSet(); |
| 69 | + |
| 70 | + _markdown = GetMarkdown(Content); |
| 71 | + } |
| 72 | + |
| 73 | + private string GetMarkdown(string? toHtml) |
| 74 | + { |
| 75 | + var html = ""; |
| 76 | + |
| 77 | + if (string.IsNullOrEmpty(toHtml)) |
| 78 | + { |
| 79 | + return html; |
| 80 | + } |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + // 处理未封闭的 think 标签 |
| 85 | + toHtml = HandleUnclosedThinkTags(toHtml); |
| 86 | + |
| 87 | + // 处理正常的 think 标签 |
| 88 | + var thinkPattern = @"<\s*think\b[^>]*>(.*?)<\s*/\s*think\s*>"; |
| 89 | + toHtml = Regex.Replace(toHtml, thinkPattern, @"<div class=""think"">$1</div>", RegexOptions.Singleline | RegexOptions.IgnoreCase); |
| 90 | + toHtml = RemoveEmbeddingsElement(toHtml); |
| 91 | + |
| 92 | + html = Markdig.Markdown.ToHtml(toHtml, _markdownPipeline); |
| 93 | + var pattern = "(<div style=\"color:#DADADA;background-color:#1E1E1E;\"><pre>(.*?)</pre></div>)"; |
| 94 | + var matches = Regex.Matches(html, pattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase); |
| 95 | + |
| 96 | + for (var i = matches.Count - 1; i >= 0; i--) |
| 97 | + { |
| 98 | + var match = matches[i].ToString(); |
| 99 | + var id = "copy" + i; |
| 100 | + var replacement = $"<button data-clipboard-target=\"#{id}\" class=\"float-end copyBtn mt-0\">Copy</button>" + match; |
| 101 | + html = html.Remove(matches[i].Index, matches[i].Length).Insert(matches[i].Index, replacement); |
| 102 | + } |
| 103 | + } |
| 104 | + catch (Exception ex) |
| 105 | + { |
| 106 | + Logger.LogError(ex, "{GetMakrDown} method throw exception", nameof(GetMarkdown)); |
| 107 | + } |
| 108 | + |
| 109 | + return html; |
| 110 | + } |
| 111 | + |
| 112 | + private static string HandleUnclosedThinkTags(string content) |
| 113 | + { |
| 114 | + if (string.IsNullOrEmpty(content)) |
| 115 | + return content; |
| 116 | + |
| 117 | + // 匹配开始标签 |
| 118 | + var openTagPattern = @"<\s*think\b[^>]*>"; |
| 119 | + var closeTagPattern = @"<\s*/\s*think\s*>"; |
| 120 | + |
| 121 | + var openTags = Regex.Matches(content, openTagPattern); |
| 122 | + var closeTags = Regex.Matches(content, closeTagPattern); |
| 123 | + |
| 124 | + // 如果开始标签数量等于结束标签数量,说明标签都是配对的 |
| 125 | + if (openTags.Count == closeTags.Count) |
| 126 | + return content; |
| 127 | + |
| 128 | + // 处理未封闭的标签 |
| 129 | + var parts = Regex.Split(content, openTagPattern); |
| 130 | + if (parts.Length <= 1) |
| 131 | + return content; |
| 132 | + |
| 133 | + var result = parts[0]; // 保留第一部分的内容 |
| 134 | + for (int i = 1; i < parts.Length; i++) |
| 135 | + { |
| 136 | + var part = parts[i]; |
| 137 | + // 检查这部分是否已经包含结束标签 |
| 138 | + if (!part.Contains("</think>", StringComparison.OrdinalIgnoreCase)) |
| 139 | + { |
| 140 | + // 没有结束标签,添加一个完整的 think 标签包装 |
| 141 | + result += $"<think>{part}</think>"; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + // 已经有结束标签,保持原样 |
| 146 | + result += $"<think>{part}"; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + private static string RemoveEmbeddingsElement(string data) |
| 154 | + { |
| 155 | + if (string.IsNullOrEmpty(data)) |
| 156 | + { |
| 157 | + return ""; |
| 158 | + } |
| 159 | + string pattern = @"\[EMBEDDINGS\](.*?)\[/EMBEDDINGS\]"; |
| 160 | + var matches = Regex.Matches(data, pattern, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); |
| 161 | + |
| 162 | + if (matches.Count == 0) |
| 163 | + { |
| 164 | + return data; |
| 165 | + } |
| 166 | + |
| 167 | + return Regex.Replace(data, pattern, "", RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); |
| 168 | + } |
| 169 | +} |
0 commit comments