/// <summary> /// 替换图片的源路径 /// </summary> /// <param name="content"></param> /// <param name="documentRawRootUrl"></param> /// <param name="localDirectory"></param> /// <returns></returns> public static string ReplaceImageSources(string content, string documentRawRootUrl, string localDirectory) { if (content == null) { return(null); } content = Regex.Replace(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", delegate(Match match) { if (WebUrlHelper.IsExternalLink(match.Groups[2].Value)) { return(match.Value); } //本地的图片,需要上传到图床中去 var newImageSource = documentRawRootUrl.EnsureEndsWith('/') + (localDirectory.IsNullOrEmpty() ? "" : localDirectory.TrimStart('/').EnsureEndsWith('/')) + match.Groups[2].Value.TrimStart('/'); var url = match.Groups[1] + " src=\"" + newImageSource + "\" " + match.Groups[3]; return(url); }, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline); return(content); }
/// <summary> /// 对URL路径进行替换和重组 /// </summary> /// <param name="content"></param> /// <returns></returns> protected virtual string NormalizeLinks( string content ) { var normalized = Regex.Replace(content, MarkdownLinkRegExp, delegate(Match match) { var link = match.Groups[2].Value; if (WebUrlHelper.IsExternalLink(link)) { return(match.Value); } var displayText = match.Groups[1].Value; var documentName = RemoveFileExtension(link); var result = string.Format(MdLinkFormat, displayText, documentName ); return(result); }); normalized = Regex.Replace(normalized, AnchorLinkRegExp, delegate(Match match) { var link = match.Groups[1].Value; if (WebUrlHelper.IsExternalLink(link)) { return(match.Value); } var displayText = match.Groups[2].Value; var documentName = RemoveFileExtension(link); return(string.Format( MdLinkFormat, displayText, documentName )); }); return(normalized); }
/// <summary> /// 上传图片到图床地址中 /// </summary> /// <param name="content"></param> /// <param name="input"></param> /// <param name="postConfig"></param> /// <returns></returns> private async Task <string> UploadpicturesToPictureBedAsync(string content, GitlabPostsNavInput input, PostItems postConfig) { if (content == null) { return(null); } //获取上传图片列表 var toDoImgBedList = Regex.Matches(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline); var newSourceImagesList = new List <string>(); foreach (Match itemPic in toDoImgBedList) { //检查是否为外联,如果是外联按不处理 if (WebUrlHelper.IsExternalLink(itemPic.Groups[2].Value)) { newSourceImagesList.Add(itemPic.Groups[2].Value); //检查图片是否为外部链接 } else { input.FileName = itemPic.Groups[2].Value; var file = await _gitlabClientAppService.GetGitlabFileInfo(input); if (file == null) { continue; } //获取当前图片的base64,然后上传到图床中。 var tags = postConfig.tags.Split(','); var directoryPath = $"{postConfig.blogShortName}/{tags[0]}"; //存放的图床仓库和地址 var pictureToBed = new UploadPictureToBed { PathWithNamespace = "52abp/picturebed", file = file, DirectoryPath = directoryPath }; //上传图片到指定图床 var imgRawUrl = await _gitlabClientAppService.UploadPictureToImgBed(pictureToBed); newSourceImagesList.Add(imgRawUrl); //install-ubuntu-1.png //http://code.52abp.com/52abp/picturebed/raw/master/2020/04/28/install-ubuntu-1.png } } //将本地图源,替换为远程图床的图源 content = Regex.Replace(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", (Match match) => { var oldImageSource = match.Groups[2].Value; if (WebUrlHelper.IsExternalLink(oldImageSource)) { //检查图片是否为外部链接 var newurl = match.Groups[1] + " class=\"img-fluid\" src =\"" + oldImageSource + "\" " + match.Groups[3]; return(newurl); // return match.Value; } oldImageSource = Path.GetFileName(oldImageSource); var newImageSource = newSourceImagesList .FirstOrDefault(a => a.EndsWith(oldImageSource)); var url = match.Groups[1] + " class=\"img-fluid\" src =\"" + newImageSource + "\" " + match.Groups[3]; return(url); }, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline); return(content); }