网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Markdig;
using Markdig.Renderers.Html;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using Microsoft.IO;

namespace Dpz.Core.Infrastructure;

public static class ApplicationTools
{
    /// <summary>
    /// 起始时间
    /// </summary>
    public static readonly DateTime StartTime = new(2018, 2, 11, 13, 56, 24, DateTimeKind.Local);

    /// <summary>
    /// ChatGPT 默认用户ID
    /// </summary>
    public const string ChatGptUserId = "e0a82f97-d01d-43c0-a257-97998003e8b9";

    public static readonly RecyclableMemoryStreamManager MemoryStreamManager =
        new RecyclableMemoryStreamManager();

    /// <summary>
    /// Markdown转为Html
    /// </summary>
    /// <param name="markdown"></param>
    /// <param name="disableHtml">是否禁用html(默认禁用)</param>
    /// <returns></returns>
    public static string MarkdownToHtml(this string markdown, bool disableHtml = true)
    {
        var pipelineBuild = new MarkdownPipelineBuilder()
            .UseAutoLinks()
            .UsePipeTables()
            .UseTaskLists()
            .UseEmphasisExtras()
            .UseAutoIdentifiers();

        if (disableHtml)
        {
            pipelineBuild.DisableHtml();
        }

        var pipeline = pipelineBuild.Build();

        var document = Markdown.Parse(markdown, pipeline);
        foreach (var link in document.Descendants<LinkInline>())
        {
            link.GetAttributes().AddPropertyIfNotExist("target", "_blank");
        }

        foreach (var link in document.Descendants<AutolinkInline>())
        {
            link.GetAttributes().AddPropertyIfNotExist("target", "_blank");
        }

        return document.ToHtml(pipeline);
    }

    /// <summary>
    /// 失败重试
    /// </summary>
    /// <param name="func"></param>
    /// <param name="retryInterval">重试间隔</param>
    /// <param name="maxAttemptCount">最大重试次数</param>
    /// <param name="specificExceptionTypes">指定异常</param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    /// <exception cref="AggregateException"></exception>
    public static async Task<T> RetryAsync<T>(
        Func<Task<T>> func,
        TimeSpan retryInterval,
        int maxAttemptCount = 3,
        ICollection<Type>? specificExceptionTypes = null
    )
    {
        if (maxAttemptCount <= 0)
        {
            maxAttemptCount = 3;
        }

        if (maxAttemptCount >= 10)
        {
            maxAttemptCount = 3;
        }

        var exceptions = new List<Exception>();
        for (var attempted = 0; attempted < maxAttemptCount; attempted++)
        {
            try
            {
                if (attempted > 0)
                {
                    await Task.Delay(retryInterval);
                    retryInterval += TimeSpan.FromSeconds(attempted + 1);
                }

                return await func();
            }
            catch (Exception ex)
            {
                if (specificExceptionTypes != null && specificExceptionTypes.Contains(ex.GetType()))
                {
                    throw;
                }
                exceptions.Add(ex);
            }
        }

        throw new AggregateException(
            $"this task has been repeated {maxAttemptCount} times and has failed",
            exceptions
        );
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

这段代码定义了一个名为 ApplicationTools 的静态类,提供了一些实用工具方法,主要用于处理 Markdown 文本和实现失败重试机制。以下是代码的主要功能和组成部分的详细解释:

1. 常量和静态字段

  • StartTime: 记录了一个固定的起始时间(2018年2月11日13:56:24),可以用于计算应用程序的运行时间或其他时间相关的逻辑。

  • ChatGptUserId: 定义了一个常量字符串,表示 ChatGPT 的默认用户 ID。

  • MemoryStreamManager: 使用 RecyclableMemoryStreamManager 来管理内存流,优化内存使用,避免频繁的内存分配和释放。

2. Markdown 转 HTML

  • MarkdownToHtml: 这是一个扩展方法,用于将 Markdown 格式的字符串转换为 HTML。它接受两个参数:

    • markdown: 要转换的 Markdown 字符串。
    • disableHtml: 一个布尔值,指示是否禁用 HTML 标签(默认为禁用)。

    方法的实现步骤如下:

    • 创建一个 MarkdownPipelineBuilder,配置 Markdown 解析的选项(如自动链接、管道表、任务列表等)。
    • 如果 disableHtmltrue,则禁用 HTML 标签。
    • 解析 Markdown 文本并生成文档对象。
    • 为所有链接(LinkInlineAutolinkInline)添加 target="_blank" 属性,以便在新标签页中打开链接。
    • 最后,将文档转换为 HTML 字符串并返回。

3. 失败重试机制

  • RetryAsync: 这是一个异步方法,用于执行一个可能会失败的操作,并在失败时进行重试。它接受以下参数:

    • func: 一个返回 Task<T> 的函数,表示要执行的操作。
    • retryInterval: 重试之间的时间间隔。
    • maxAttemptCount: 最大重试次数(默认为 3 次)。
    • specificExceptionTypes: 可选的异常类型集合,指定在重试时需要处理的特定异常。

    方法的实现逻辑如下:

    • 如果 maxAttemptCount 小于等于 0 或大于等于 10,则将其重置为 3。
    • 使用一个循环来尝试执行 func,最多尝试 maxAttemptCount 次。
    • 如果尝试失败且 specificExceptionTypes 包含该异常类型,则抛出该异常。
    • 如果尝试失败但不在指定的异常类型中,则将异常添加到异常列表中。
    • 如果所有尝试都失败,则抛出一个 AggregateException,包含所有失败的异常。

总结

整体而言,这段代码提供了 Markdown 文本处理和异步操作的重试机制,适用于需要将 Markdown 转换为 HTML 的应用程序,以及需要处理可能失败的异步操作的场景。

loading