using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace QuestionSwapDoc.Help
{
    public static class HttpHelp
    {
        /// <summary>
        /// 为html中的连接添加左边的路径为绝对路径
        /// </summary>
        /// <param name="htmlText"></param>
        /// <returns></returns>
        public static string AddLeftLinkPath(string htmlText, string imgLinkLeftPath)
        {
            //html解析器
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(htmlText);
            var imgs = htmlDocument.DocumentNode.SelectNodes("//img");
            if (imgs != null)
            {
                foreach (var item in imgs)
                {
                    var imgSrc = item.Attributes["src"];
                    var newPath = Path.Join(imgLinkLeftPath, imgSrc.Value);
                    if (File.Exists(newPath))
                    {
                        imgSrc.Value = newPath;
                    }
                }
            }

            return htmlDocument.DocumentNode.OuterHtml;
        }
    }
}
