Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WCP]alt attribute for img tag #9404

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/NuGetGallery/Services/MarkdownService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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);
lyndaidaii marked this conversation as resolved.
Show resolved Hide resolved

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

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

var markdownWithoutBom = markdownWithoutComments.TrimStart('\ufeff');
var markdownWithImageAlt = ImageTextPattern.Replace(markdownWithoutComments, "![image]");
lyndaidaii marked this conversation as resolved.
Show resolved Hide resolved

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

var pipeline = new MarkdownPipelineBuilder()
.UseGridTables()
Expand Down Expand Up @@ -284,8 +287,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=\"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