Skip to content

Commit

Permalink
[WCP]alt attribute for img tag (#9404)
Browse files Browse the repository at this point in the history
* add image alter
  • Loading branch information
lyndaidaii authored Feb 28, 2023
1 parent 9e2d728 commit 9fb61dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/NuGetGallery/Services/MarkdownService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class MarkdownService : IMarkdownService
private static readonly Regex EncodedBlockQuotePattern = new Regex("^ {0,3}>", RegexOptions.Multiline, RegexTimeout);
private static readonly Regex LinkPattern = new Regex("<a href=([\"\']).*?\\1", RegexOptions.None, RegexTimeout);
private static readonly Regex HtmlCommentPattern = new Regex("<!--.*?-->", RegexOptions.Singleline, RegexTimeout);
private static readonly Regex ImageTextPattern = new Regex("!\\[\\]\\(", RegexOptions.Singleline, RegexTimeout);
private static readonly string altTextForImage = "alternate text is missing from this package README image";

private readonly IFeatureFlagService _features;
private readonly IImageDomainValidator _imageDomainValidator;
Expand Down Expand Up @@ -198,7 +200,9 @@ private RenderedMarkdownResult GetHtmlFromMarkdownMarkdig(string markdownString,

var markdownWithoutComments = HtmlCommentPattern.Replace(markdownString, "");

var markdownWithoutBom = markdownWithoutComments.TrimStart('\ufeff');
var markdownWithImageAlt = ImageTextPattern.Replace(markdownWithoutComments, $"![{altTextForImage}](");

var markdownWithoutBom = markdownWithImageAlt.TrimStart('\ufeff');

var pipeline = new MarkdownPipelineBuilder()
.UseGridTables()
Expand Down Expand Up @@ -284,8 +288,8 @@ private RenderedMarkdownResult GetHtmlFromMarkdownMarkdig(string markdownString,

renderer.Render(document);
output.Content = htmlWriter.ToString().Trim();

output.IsMarkdigMdSyntaxHighlightEnabled = _features.IsMarkdigMdSyntaxHighlightEnabled();

return output;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/NuGetGallery.Facts/Services/MarkdownServiceFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public void EncodesHtmlInMarkdownWithAdaptiveHeader(string originalMd, string ex
[InlineData("![image](https://www.asp.net/fake.jpg)", "<p><img src=\"https://www.asp.net/fake.jpg\" alt=\"image\" /></p>", false, false)]
[InlineData("![image](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" class=\"img-fluid\" alt=\"image\" /></p>", true, true)]
[InlineData("![image](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" alt=\"image\" /></p>", true, false)]
[InlineData("![](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" alt=\"\" /></p>", true, false)]
[InlineData("![](http://www.otherurl.net/fake.jpg)", "<p><img src=\"https://www.otherurl.net/fake.jpg\" class=\"img-fluid\" alt=\"alternate text is missing from this package README image\" /></p>", true, true)]
[InlineData("## License\n\tLicensed under the Apache License, Version 2.0 (the \"License\");", "<h3 id=\"license\">License</h3>\n<pre><code>Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);\n</code></pre>", false, true)]
[InlineData("## License\n\tLicensed under the Apache License, Version 2.0 (the \"License\");", "<h3 id=\"license\">License</h3>\n<pre><code>Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);\n</code></pre>", false, true)]
public void ConvertsMarkdownToHtml(string originalMd, string expectedHtml, bool imageRewriteExpected, bool isMarkdigMdRenderingEnabled)
Expand Down

0 comments on commit 9fb61dc

Please sign in to comment.