diff --git a/PortablePorts/NReadability/AttributeTransformationInput.cs b/PortablePorts/NReadability/AttributeTransformationInput.cs index e9097c5..613a4fa 100644 --- a/PortablePorts/NReadability/AttributeTransformationInput.cs +++ b/PortablePorts/NReadability/AttributeTransformationInput.cs @@ -6,6 +6,8 @@ namespace ReadSharp.Ports.NReadability { public string AttributeValue { get; set; } + public string ArticleUrl { get; set; } + public XElement Element { get; set; } } } diff --git a/PortablePorts/NReadability/NReadabilityTranscoder.cs b/PortablePorts/NReadability/NReadabilityTranscoder.cs index 1180aee..0296921 100644 --- a/PortablePorts/NReadability/NReadabilityTranscoder.cs +++ b/PortablePorts/NReadability/NReadabilityTranscoder.cs @@ -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 { diff --git a/ReadSharp.Tests/ReadTests.cs b/ReadSharp.Tests/ReadTests.cs index 8273480..c0806d1 100644 --- a/ReadSharp.Tests/ReadTests.cs +++ b/ReadSharp.Tests/ReadTests.cs @@ -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("", result.Content); + + result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), true, false, false); + + Assert.DoesNotContain("", result.Content); + Assert.Contains("", result.Content); + } + + [Fact] public async Task TestCzechCharsets() { diff --git a/ReadSharp/IReader.cs b/ReadSharp/IReader.cs index 9246fc1..e12266d 100644 --- a/ReadSharp/IReader.cs +++ b/ReadSharp/IReader.cs @@ -10,11 +10,17 @@ namespace ReadSharp /// /// An URI to extract the content from. /// if set to true [only body is returned]. - /// if set to true [no headline (h1) is included]. + /// if set to true [no headline (h1) is included in generated HTML]. + /// if set to true [deep links with hashes are not transformed to absolute URIs]. /// /// An article with extracted content and meta information. /// /// - Task
Read(Uri uri, bool bodyOnly = true, bool noHeadline = false); + Task
Read( + Uri uri, + bool bodyOnly = true, + bool noHeadline = false, + bool useDeepLinks = false + ); } } \ No newline at end of file diff --git a/ReadSharp/Reader.cs b/ReadSharp/Reader.cs index c85097c..281ef7d 100644 --- a/ReadSharp/Reader.cs +++ b/ReadSharp/Reader.cs @@ -100,11 +100,16 @@ namespace ReadSharp /// An URI to extract the content from. /// if set to true [only body is returned]. /// if set to true [no headline (h1) is included in generated HTML]. + /// if set to true [deep links with hashes are not transformed to absolute URIs]. /// /// An article with extracted content and meta information. /// /// - public async Task
Read(Uri uri, bool bodyOnly = true, bool noHeadline = false) + public async Task
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 + /// + /// Reverses the deep links. + /// + /// The input. + /// + 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 + }; + } + + + /// /// Fetches a resource ///