|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System.Text; |
|
|
|
using System; |
|
|
|
using System.Text; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using CommonMark; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
@ -11,6 +12,8 @@ namespace Volo.Docs.Formatting |
|
|
|
|
|
|
|
private const string NewLinkFormat = "[{0}](/documents/{1}/{2}/{3}/{4})"; |
|
|
|
|
|
|
|
private const string MarkdownLinkRegExp = @"\[([^)]+)\]\(([^)]+." + Type + @")\)"; |
|
|
|
|
|
|
|
public string Convert(string content) |
|
|
|
{ |
|
|
|
return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); |
|
|
|
@ -19,14 +22,45 @@ namespace Volo.Docs.Formatting |
|
|
|
public string NormalizeLinks(string content, string projectShortName, string version, |
|
|
|
string documentLocalDirectory) |
|
|
|
{ |
|
|
|
return Regex.Replace(content, @"\[([^)]+)\]\(([^)]+.md)\)", delegate (Match match) |
|
|
|
{ |
|
|
|
var displayText = match.Groups[1].Value; |
|
|
|
var documentName = match.Groups[2].Value; |
|
|
|
var newLink = string.Format(NewLinkFormat, displayText, projectShortName, version, documentLocalDirectory.TrimStart('/').TrimEnd('/'), documentName); |
|
|
|
return newLink; |
|
|
|
}); |
|
|
|
return Regex.Replace(content, MarkdownLinkRegExp, delegate (Match match) |
|
|
|
{ |
|
|
|
var displayText = match.Groups[1].Value; |
|
|
|
var documentName = RemoveFileExtensionIfLocalUrl(match.Groups[2].Value); |
|
|
|
return string.Format(NewLinkFormat, displayText, projectShortName, version, |
|
|
|
documentLocalDirectory.TrimStart('/').TrimEnd('/'), documentName); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private static string RemoveFileExtensionIfLocalUrl(string documentName) |
|
|
|
{ |
|
|
|
if (string.IsNullOrWhiteSpace(documentName)) |
|
|
|
{ |
|
|
|
return documentName; |
|
|
|
} |
|
|
|
|
|
|
|
if (!documentName.EndsWith(Type, StringComparison.OrdinalIgnoreCase)) |
|
|
|
{ |
|
|
|
return documentName; |
|
|
|
} |
|
|
|
|
|
|
|
if (IsRemoteUrl(documentName)) |
|
|
|
{ |
|
|
|
return documentName; |
|
|
|
} |
|
|
|
|
|
|
|
return documentName.Left(documentName.Length - Type.Length - 1); |
|
|
|
} |
|
|
|
|
|
|
|
private static bool IsRemoteUrl(string url) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
return Regex.IsMatch(url, @"\A(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?\z"); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|