useDeepLink option for Reader
This commit is contained in:
@@ -6,6 +6,8 @@ namespace ReadSharp.Ports.NReadability
|
||||
{
|
||||
public string AttributeValue { get; set; }
|
||||
|
||||
public string ArticleUrl { get; set; }
|
||||
|
||||
public XElement Element { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1656,7 +1656,9 @@ namespace ReadSharp.Ports.NReadability
|
||||
|
||||
if (attributeValueTransformer != null)
|
||||
{
|
||||
attributeTransformationResult = attributeValueTransformer.Invoke(new AttributeTransformationInput { AttributeValue = attributeValue, Element = element });
|
||||
attributeTransformationResult = attributeValueTransformer.Invoke(
|
||||
new AttributeTransformationInput { AttributeValue = attributeValue, Element = element, ArticleUrl = url }
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -75,6 +75,20 @@ namespace ReadSharp.Tests
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task DoesUseDeepLinksWork()
|
||||
{
|
||||
Article result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), true, false, true);
|
||||
|
||||
Assert.Contains("<a href=\"#Browser_compatibility\">", result.Content);
|
||||
|
||||
result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), true, false, false);
|
||||
|
||||
Assert.DoesNotContain("<a href=\"#Browser_compatibility\">", result.Content);
|
||||
Assert.Contains("<a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering#Browser_compatibility\">", result.Content);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task TestCzechCharsets()
|
||||
{
|
||||
|
||||
@@ -10,11 +10,17 @@ namespace ReadSharp
|
||||
/// </summary>
|
||||
/// <param name="uri">An URI to extract the content from.</param>
|
||||
/// <param name="bodyOnly">if set to <c>true</c> [only body is returned].</param>
|
||||
/// <param name="noHeadline">if set to <c>true</c> [no headline (h1) is included].</param>
|
||||
/// <param name="noHeadline">if set to <c>true</c> [no headline (h1) is included in generated HTML].</param>
|
||||
/// <param name="useDeepLinks">if set to <c>true</c> [deep links with hashes are not transformed to absolute URIs].</param>
|
||||
/// <returns>
|
||||
/// An article with extracted content and meta information.
|
||||
/// </returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
Task<Article> Read(Uri uri, bool bodyOnly = true, bool noHeadline = false);
|
||||
Task<Article> Read(
|
||||
Uri uri,
|
||||
bool bodyOnly = true,
|
||||
bool noHeadline = false,
|
||||
bool useDeepLinks = false
|
||||
);
|
||||
}
|
||||
}
|
||||
+49
-1
@@ -100,11 +100,16 @@ namespace ReadSharp
|
||||
/// <param name="uri">An URI to extract the content from.</param>
|
||||
/// <param name="bodyOnly">if set to <c>true</c> [only body is returned].</param>
|
||||
/// <param name="noHeadline">if set to <c>true</c> [no headline (h1) is included in generated HTML].</param>
|
||||
/// <param name="useDeepLinks">if set to <c>true</c> [deep links with hashes are not transformed to absolute URIs].</param>
|
||||
/// <returns>
|
||||
/// An article with extracted content and meta information.
|
||||
/// </returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<Article> Read(Uri uri, bool bodyOnly = true, bool noHeadline = false)
|
||||
public async Task<Article> Read(
|
||||
Uri uri,
|
||||
bool bodyOnly = true,
|
||||
bool noHeadline = false,
|
||||
bool useDeepLinks = false)
|
||||
{
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
|
||||
HttpResponseMessage respo = null;
|
||||
@@ -125,6 +130,16 @@ namespace ReadSharp
|
||||
// get HTML string from URI
|
||||
Response response = await Request(uri);
|
||||
|
||||
// handle deep links
|
||||
if (useDeepLinks)
|
||||
{
|
||||
_transcoder.AnchorHrefTranformer = ReverseDeepLinks;
|
||||
}
|
||||
else
|
||||
{
|
||||
_transcoder.AnchorHrefTranformer = null;
|
||||
}
|
||||
|
||||
// readability
|
||||
TranscodingResult transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline);
|
||||
|
||||
@@ -210,6 +225,39 @@ namespace ReadSharp
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reverses the deep links.
|
||||
/// </summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <returns></returns>
|
||||
private AttributeTransformationResult ReverseDeepLinks(AttributeTransformationInput input)
|
||||
{
|
||||
string articleUrl = input.ArticleUrl;
|
||||
string link = input.AttributeValue;
|
||||
|
||||
// remove deep-link if in article URI
|
||||
if (articleUrl.Contains("#"))
|
||||
{
|
||||
articleUrl = articleUrl.Split('#')[0];
|
||||
}
|
||||
|
||||
// anchor is a deep-link
|
||||
if (
|
||||
input.AttributeValue.Contains(articleUrl) &&
|
||||
input.AttributeValue.Contains("#") &&
|
||||
input.AttributeValue.Split('#')[1].Length > 0)
|
||||
{
|
||||
link = "#" + input.AttributeValue.Split('#')[1];
|
||||
}
|
||||
|
||||
return new AttributeTransformationResult()
|
||||
{
|
||||
TransformedValue = link
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a resource
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user