Upgraded ReverseMarkdown to v6.3.1 (#1244)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-29 10:34:48 +01:00
1 parent dbe609a9a7
commit ddfc632b63
6 files changed
+14 -99

No files matched your search

@@ -115,7 +115,7 @@
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="10.0.15" />
<PackageReference Include="MudBlazor" Version="9.14.0" />
<PackageReference Include="MudBlazor.Markdown" Version="9.12.0" />
<PackageReference Include="ReverseMarkdown" Version="6.3.0" />
<PackageReference Include="ReverseMarkdown" Version="6.3.1" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="3.0.5" />
<PackageReference Include="LuaCSharp" Version="0.5.8" />
</ItemGroup>
@@ -1,9 +1,7 @@
using System.Collections.Frozen;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;
using AIStudio.Tools.Web;
using AngleSharp.Dom;
using HtmlAgilityPack;
using ReverseMarkdown;
@@ -15,21 +13,6 @@ public sealed class HTMLParser
private const int MAX_REDIRECTS = 10;
private const int DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
/// <summary>
/// The elements which ReverseMarkdown reads as inline elements around their content, such as a link.
/// </summary>
private static readonly FrozenSet<string> INLINE_ELEMENTS = new[] { "a", "abbr", "b", "cite", "del", "em", "i", "s", "span", "strike", "strong", "sub", "sup" }.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The elements which ReverseMarkdown reads as blocks of their own, or as containers of blocks. Tables and pre are
/// left out: their content keeps its own structure inside an inline element as well.
/// </summary>
private static readonly FrozenSet<string> BLOCK_ELEMENTS = new[]
{
"article", "aside", "blockquote", "dd", "div", "dl", "dt", "figcaption", "figure", "footer", "h1", "h2", "h3", "h4", "h5", "h6",
"header", "hr", "li", "main", "nav", "ol", "p", "section", "ul",
}.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The one converter every HTML to Markdown conversion runs through.
/// </summary>
@@ -47,7 +30,6 @@ public sealed class HTMLParser
Tags = { Unknown = Config.UnknownTagsOption.Bypass },
Formatting = { RemoveComments = true },
Links = { SmartHref = true },
Preprocess = { { "Unwrap the blocks inside inline elements", UnwrapBlocksInsideInlineElements } },
});
/// <summary>
@@ -271,74 +253,4 @@ public sealed class HTMLParser
/// <param name="html">The HTML content to parse.</param>
/// <returns>The converted Markdown content.</returns>
public static string ParseToMarkdown(string html) => CONVERTER.Convert(html);
/// <summary>
/// Replaces every block inside an inline element by its content, with a space before and after it.
/// </summary>
/// <param name="root">The document element of the HTML, as ReverseMarkdown parsed it.</param>
/// <remarks>
/// Compatibility shim, see documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md;
/// remove it once ReverseMarkdown separates such blocks itself, see
/// https://github.com/mysticmind/reversemarkdown-net/issues/440.<br/><br/>
/// Many pages wrap a whole teaser in a link: a heading, a line of metadata, and a summary, each a block of its own.
/// A link holds no blocks in Markdown, so ReverseMarkdown 6 writes their texts one after another into the text of
/// the link, without anything in between, e.g., "Java77.721 Abrufeheise security". Spaces next to such blocks alone
/// were not enough: a first attempt which only added them still left most teasers on the start page of a news site
/// glued together. Hence, each block gives way to its content, which the library then reads as part of the inline
/// element, spaces included. The result is what the library makes of such a block anyway, only with the words
/// apart. Where whitespace is there already, nothing is added, and inside pre the text stays exactly as it is.<br/><br/>
/// The blocks are taken from the outside in, so an inner block sees the neighbors it got when its outer block gave
/// way. Every conversion parses a document of its own, so this step changes nothing which another thread could see.
/// </remarks>
private static void UnwrapBlocksInsideInlineElements(IElement root)
{
var blocks = root.QuerySelectorAll("*").Where(IsBlockInsideInlineElement).ToList();
foreach (var block in blocks)
{
if (block.Parent is not { } parent)
continue;
if (NeedsSeparator(block.PreviousSibling, atItsEnd: true))
parent.InsertBefore(block.Owner!.CreateTextNode(" "), block);
if (NeedsSeparator(block.NextSibling, atItsEnd: false))
parent.InsertBefore(block.Owner!.CreateTextNode(" "), block.NextSibling);
foreach (var child in block.ChildNodes.ToArray())
parent.InsertBefore(child, block);
block.Remove();
}
}
private static bool IsBlockInsideInlineElement(IElement element)
{
if (!BLOCK_ELEMENTS.Contains(element.LocalName))
return false;
var insideInlineElement = false;
for (var ancestor = element.ParentElement; ancestor is not null; ancestor = ancestor.ParentElement)
{
if (ancestor.LocalName.Equals("pre", StringComparison.OrdinalIgnoreCase))
return false;
if (INLINE_ELEMENTS.Contains(ancestor.LocalName))
insideInlineElement = true;
}
return insideInlineElement;
}
/// <summary>
/// Whether the node next to a block runs into the block without whitespace in between.
/// </summary>
/// <param name="neighbor">The node right before or after the block, if any.</param>
/// <param name="atItsEnd">True when the neighbor comes before the block, so its last character touches the block.</param>
private static bool NeedsSeparator(INode? neighbor, bool atItsEnd) => neighbor switch
{
null => false,
IText { Data.Length: 0 } => false,
IText text => !char.IsWhiteSpace(atItsEnd ? text.Data[^1] : text.Data[0]),
_ => true,
};
}
+3 -3
View File
@@ -114,9 +114,9 @@
},
"ReverseMarkdown": {
"type": "Direct",
"requested": "[6.3.0, )",
"resolved": "6.3.0",
"contentHash": "h2GwfOpLAkONJa3ARH4VnhrJro7XcqTmI1z0d6LzXVto8Qm/8cmUO3cBm7nkN48rQfSPi2NpEPBKuPzSZr1LMg==",
"requested": "[6.3.1, )",
"resolved": "6.3.1",
"contentHash": "aJsUf35UDzzjMUG92WBsMskkx9GQL34TVg1UifBeqKT77hi1H+O05XiSOs13ZIpPlgdQASmWFDCP8nqohSw8PA==",
"dependencies": {
"AngleSharp": "1.5.2"
}
+5 -3
View File
@@ -16,8 +16,10 @@ public sealed class HTMLParserConversionTests
/// A teaser wrapped in a link keeps the words of its blocks apart, while the text of pre stays as it is.
/// </summary>
/// <remarks>
/// Without the compatibility shim in HTMLParser, the first item reads "Probe lands on the asteroid22 minutes
/// agoExample News", see documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md.
/// ReverseMarkdown separates such blocks itself since version 6.3.1, see
/// https://github.com/mysticmind/reversemarkdown-net/issues/440. Up to version 6.3.0, the first item read "Probe
/// lands on the asteroid22 minutes agoExample News". Since version 6.3.1 as well, it drops the empty link which
/// the HTML parser leaves behind when the pre closes the paragraph.
/// </remarks>
[Test]
public void BlocksInsideALinkKeepTheirWordsApart()
@@ -39,7 +41,7 @@ public sealed class HTMLParserConversionTests
- [Second headline A summary of it.](https://example.org/news/2)
- [Plain link text](https://example.org/news/3)
Code in a link: [](https://example.org/code)
Code in a link:
```
line one
+3 -3
View File
@@ -8,8 +8,8 @@ namespace AIStudio.TrimSmokeTest;
/// <remarks>
/// The trimmer therefore removes every part of ReverseMarkdown which nothing but reflection reaches, and
/// Trimming.props suppresses the warnings which would name such a part. The converter of HTMLParser takes only the
/// built-in readers and writers, which need no reflection, and a preprocessing step of our own. The HTML and the
/// expected Markdown are those of HTMLParserConversionTests in app/Tests/Tools, which run untrimmed.
/// built-in readers and writers, which need no reflection. The HTML and the expected Markdown are those of
/// HTMLParserConversionTests in app/Tests/Tools, which run untrimmed.
/// </remarks>
public static class MarkdownChecks
{
@@ -30,7 +30,7 @@ public static class MarkdownChecks
- [Second headline A summary of it.](https://example.org/news/2)
- [Plain link text](https://example.org/news/3)
Code in a link: [](https://example.org/code)
Code in a link:
```
line one