Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
normalize autolinks
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Feb 24, 2020
1 parent b66a4a3 commit 8ed35d4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
71 changes: 58 additions & 13 deletions EhTagClient/MarkdigExt/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace EhTagClient.MarkdigExt
{
Expand Down Expand Up @@ -87,26 +88,70 @@ public static MarkdownDocument Normalize(MarkdownDocument doc)
{
foreach (var link in doc.Descendants().OfType<LinkInline>())
{
var url = link.GetDynamicUrl?.Invoke() ?? link.Url;
var title = link.Title;
var (furl, nsfw) = _FormatUrl(url);
if (link.IsImage && nsfw)
_NormalizeLink(link);
}

return doc;
}

private static readonly Dictionary<string, string> _KnownHosts = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["moegirl.org"] = "萌娘百科",
["wikipedia.org"] = "维基百科",
["pixiv.net"] = "pixiv",
["instagram.com"] = "Instagram",
["facebook.com"] = "脸书",
["twitter.com"] = "Twitter",
["weibo.com"] = "微博",
};

private static void _NormalizeLink(LinkInline link)
{
var url = link.GetDynamicUrl?.Invoke() ?? link.Url;
var title = link.Title;
var nsfwmark = default(string);

if (url != null && url.StartsWith("#") && !string.IsNullOrWhiteSpace(title))
{
// nsfw link
nsfwmark = url;
url = title;
}

var (furl, nsfw) = _FormatUrl(url);
if (nsfw && nsfwmark == null)
{
nsfwmark = "#";
}

if (link.IsImage)
{
if (nsfwmark == null)
{
link.Title = furl;
link.Url = "#";
link.Url = furl;
}
else if (link.IsImage && url == "#" && !string.IsNullOrEmpty(title))
else
{
(link.Title, _) = _FormatUrl(title);
link.Url = "#";
link.Title = furl;
link.Url = nsfwmark;
}
else
}
else
{
if (link.IsAutoLink
&& link.FirstChild == link.LastChild && link.FirstChild is LiteralInline content
&& Uri.TryCreate(url, UriKind.Absolute, out var purl))
{
link.Url = furl;
foreach (var item in _KnownHosts)
{
if (purl.Host.EndsWith(item.Key))
{
content.Content = new Markdig.Helpers.StringSlice(item.Value);
}
}
}
link.Url = furl;
}

return doc;
}

private static readonly Regex _ThumbUriRegex = new Regex(@"^(http|https)://(ehgt\.org(/t|)|exhentai\.org/t|ul\.ehgt\.org(/t|))/(.+)$", RegexOptions.Compiled | RegexOptions.Singleline);
Expand Down
1 change: 1 addition & 0 deletions EhTagClient/MarkdigExt/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static Renderer()
var builder = new MarkdownPipelineBuilder();
builder.DisableHtml();
builder.BlockParsers.RemoveAll(bp => !(bp is Markdig.Parsers.ParagraphBlockParser));
builder.UseAutoLinks();
_Pipeline = builder.Build();
}

Expand Down

0 comments on commit 8ed35d4

Please sign in to comment.