Browse Source

Update MarkdownDocumentToHtmlConverter.cs

pull/625/head
Halil ibrahim Kalkan 8 years ago
parent
commit
9f02727ac2
  1. 77
      modules/docs/src/Volo.Docs.Web/Markdown/MarkdownDocumentToHtmlConverter.cs

77
modules/docs/src/Volo.Docs.Web/Markdown/MarkdownDocumentToHtmlConverter.cs

@ -14,11 +14,11 @@ namespace Volo.Docs.Markdown
{ {
public const string Type = "md"; public const string Type = "md";
private const string NewLinkFormat = "[{0}](/documents/{1}/{2}{3}/{4})"; private const string MdLinkFormat = "[{0}](/documents/{1}/{2}{3}/{4})";
private const string MarkdownLinkRegExp = @"\[([^)]+)\]\(([^)]+." + Type + @")\)"; private const string MarkdownLinkRegExp = @"\[([^)]+)\]\(([^)]+." + Type + @")\)";
private const string AnchorLinkRegExp = @"<a[^>]+href=\""(.*?)\""[^>]*>(.*)?</a>"; private const string AnchorLinkRegExp = @"<a[^>]+href=\""(.*?)\""[^>]*>(.*)?</a>";
public string Convert(ProjectDto project, DocumentWithDetailsDto document, string version) public virtual string Convert(ProjectDto project, DocumentWithDetailsDto document, string version)
{ {
if (document.Content.IsNullOrEmpty()) if (document.Content.IsNullOrEmpty())
{ {
@ -35,41 +35,43 @@ namespace Volo.Docs.Markdown
return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content)));
} }
private string NormalizeLinks( protected virtual string NormalizeLinks(
string content, string content,
string projectShortName, string projectShortName,
string version, string version,
string documentLocalDirectory) string documentLocalDirectory)
{ {
if (content == null) var normalized = Regex.Replace(content, MarkdownLinkRegExp, delegate(Match match)
{ {
return null; var link = match.Groups[2].Value;
} if (UrlHelper.IsExternalLink(link))
{
var normalized = Regex.Replace(content, MarkdownLinkRegExp, delegate (Match match) return match.Value;
{ }
var link = match.Groups[2].Value;
if (UrlHelper.IsExternalLink(link))
{
return match.Value;
}
var displayText = match.Groups[1].Value; var displayText = match.Groups[1].Value;
var documentName = RemoveFileExtension(link); var documentName = RemoveFileExtension(link);
var documentLocalDirectoryNormalized = documentLocalDirectory.TrimStart('/').TrimEnd('/'); var documentLocalDirectoryNormalized = documentLocalDirectory.TrimStart('/').TrimEnd('/');
if (!string.IsNullOrWhiteSpace(documentLocalDirectoryNormalized)) if (!string.IsNullOrWhiteSpace(documentLocalDirectoryNormalized))
{ {
documentLocalDirectoryNormalized = "/" + documentLocalDirectoryNormalized; documentLocalDirectoryNormalized = "/" + documentLocalDirectoryNormalized;
} }
return string.Format(NewLinkFormat, displayText, projectShortName, version, documentLocalDirectoryNormalized, documentName); return string.Format(
}); MdLinkFormat,
displayText,
projectShortName,
version,
documentLocalDirectoryNormalized,
documentName
);
});
normalized = Regex.Replace(normalized, AnchorLinkRegExp, delegate (Match match) normalized = Regex.Replace(normalized, AnchorLinkRegExp, delegate (Match match)
{ {
var link = match.Groups[1].Value; var link = match.Groups[1].Value;
if (IsRemoteUrl(link)) if (UrlHelper.IsExternalLink(link))
{ {
return match.Value; return match.Value;
} }
@ -82,7 +84,14 @@ namespace Volo.Docs.Markdown
documentLocalDirectoryNormalized = "/" + documentLocalDirectoryNormalized; documentLocalDirectoryNormalized = "/" + documentLocalDirectoryNormalized;
} }
return string.Format(NewLinkFormat, displayText, projectShortName, version, documentLocalDirectoryNormalized, documentName); return string.Format(
MdLinkFormat,
displayText,
projectShortName,
version,
documentLocalDirectoryNormalized,
documentName
);
}); });
return normalized; return normalized;
@ -107,23 +116,5 @@ namespace Volo.Docs.Markdown
return documentName.Left(documentName.Length - Type.Length - 1); return documentName.Left(documentName.Length - Type.Length - 1);
} }
//TODO: Merge with UrlHelper.IsExternalLink
private static bool IsRemoteUrl(string url)
{
if (url == null)
{
return true;
}
try
{
return Regex.IsMatch(url, @"\A(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?\z");
}
catch (Exception)
{
return true;
}
}
} }
} }

Loading…
Cancel
Save