Skip to content

Commit

Permalink
Add support for markdown > alerts via markdig
Browse files Browse the repository at this point in the history
Markdig supports alert blocks as documented at https://github.com/xoofx/markdig/blob/master/src/Markdig.Tests/Specs/AlertBlockSpecs.md.

Bumping markdig and adding `.UseAlertBlocks()` now renders the expected html.

Fixes NuGet#10125
  • Loading branch information
kzu committed Sep 13, 2024
1 parent 08cf2a8 commit e782501
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<PackageVersion Include="LibGit2Sharp" Version="0.26.0" />
<PackageVersion Include="Lucene.Net.Contrib" Version="3.0.3" />
<PackageVersion Include="Lucene.Net" Version="3.0.3" />
<PackageVersion Include="Markdig.Signed" Version="0.30.2" />
<PackageVersion Include="Markdig.Signed" Version="0.37.0" />
<PackageVersion Include="MicroBuild.Core" Version="0.3.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.TraceListener" Version="2.21.0" />
Expand Down
5 changes: 3 additions & 2 deletions src/NuGetGallery/Services/MarkdownService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -208,6 +208,7 @@ private RenderedMarkdownResult GetHtmlFromMarkdownMarkdig(string markdownString,
.UseTaskLists()
.UseEmojiAndSmiley()
.UseAutoLinks()
.UseAlertBlocks()
.UseReferralLinks("noopener noreferrer nofollow")
.UseAutoIdentifiers()
.UseEmphasisExtras(EmphasisExtraOptions.Strikethrough)
Expand Down Expand Up @@ -292,4 +293,4 @@ private RenderedMarkdownResult GetHtmlFromMarkdownMarkdig(string markdownString,
}
}
}
}
}
15 changes: 13 additions & 2 deletions tests/NuGetGallery.Facts/Services/MarkdownServiceFacts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -105,12 +105,23 @@ public void EncodesHtmlInMarkdownWithAdaptiveHeader(string originalMd, string ex
[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(
"""
> [!NOTE]
> This is a note
""",
"""
<div class="markdown-alert markdown-alert-note alert alert-primary" role="alert">
<p class="mb-0">This is a note</p>
</div>
""",
false, true)]
public void ConvertsMarkdownToHtml(string originalMd, string expectedHtml, bool imageRewriteExpected, bool isMarkdigMdRenderingEnabled)
{
_featureFlagService.Setup(x => x.IsMarkdigMdRenderingEnabled()).Returns(isMarkdigMdRenderingEnabled);
_featureFlagService.Setup(x => x.IsImageAllowlistEnabled()).Returns(false);
var readMeResult = _markdownService.GetHtmlFromMarkdown(originalMd);
Assert.Equal(expectedHtml, readMeResult.Content);
Assert.Equal(expectedHtml.Replace("\r\n", "\n"), readMeResult.Content.Replace("\r\n", "\n"));
Assert.Equal(imageRewriteExpected, readMeResult.ImagesRewritten);
}

Expand Down

0 comments on commit e782501

Please sign in to comment.