private static void Main(string[] args) { var path = @"C:\xguan\demo\hexo\source\_posts\";
var dir = new DirectoryInfo(path); var files = dir.GetFiles($"*.md"); foreach (var file in files) { StreamReader sr = new StreamReader(file.FullName, Encoding.UTF8); var source = sr.ReadToEnd();
Regex regex = new Regex(@"!\[\]\((.*?)\)", RegexOptions.IgnoreCase); MatchCollection matches = regex.Matches(source); foreach (var matche in matches) { var url = matche.ToString().Replace(".TrimEnd(')');
var fileName = Path.GetFileName(url); if (url.IndexOf("?") != -1) { fileName = Path.GetFileName(url.Substring(0, url.IndexOf("?"))); }
if (Path.GetExtension(fileName).Length == 0) { fileName = $"{fileName}.png"; } var newImgSrc = $"./images/{fileName}";
if (DownloadFile(url, newImgSrc)) { source = source.Replace(url, newImgSrc); Console.WriteLine($"处理前:{url}"); Console.WriteLine($"处理后:{newImgSrc}"); Console.WriteLine(); } } //copy 重新写文件 using (FileStream fs = new FileStream($"./copy/{file.Name}", FileMode.OpenOrCreate)) { byte[] data = System.Text.Encoding.UTF8.GetBytes(source); fs.Write(data, 0, data.Length); } System.Threading.Thread.Sleep(1000); } Console.WriteLine("处理完成"); Console.ReadLine(); }