diff --git a/app/MindWork AI Studio/MindWork AI Studio.csproj b/app/MindWork AI Studio/MindWork AI Studio.csproj index 41fa3a9d9..338c5e951 100644 --- a/app/MindWork AI Studio/MindWork AI Studio.csproj +++ b/app/MindWork AI Studio/MindWork AI Studio.csproj @@ -115,7 +115,7 @@ - + diff --git a/app/MindWork AI Studio/Tools/HTMLParser.cs b/app/MindWork AI Studio/Tools/HTMLParser.cs index 6bba9fd6b..f2fbbd4a7 100644 --- a/app/MindWork AI Studio/Tools/HTMLParser.cs +++ b/app/MindWork AI Studio/Tools/HTMLParser.cs @@ -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; - /// - /// The elements which ReverseMarkdown reads as inline elements around their content, such as a link. - /// - private static readonly FrozenSet INLINE_ELEMENTS = new[] { "a", "abbr", "b", "cite", "del", "em", "i", "s", "span", "strike", "strong", "sub", "sup" }.ToFrozenSet(StringComparer.OrdinalIgnoreCase); - - /// - /// 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. - /// - private static readonly FrozenSet 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); - /// /// The one converter every HTML to Markdown conversion runs through. /// @@ -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 } }, }); /// @@ -271,74 +253,4 @@ public sealed class HTMLParser /// The HTML content to parse. /// The converted Markdown content. public static string ParseToMarkdown(string html) => CONVERTER.Convert(html); - - /// - /// Replaces every block inside an inline element by its content, with a space before and after it. - /// - /// The document element of the HTML, as ReverseMarkdown parsed it. - /// - /// 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.

- /// 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.

- /// 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. - ///
- 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; - } - - /// - /// Whether the node next to a block runs into the block without whitespace in between. - /// - /// The node right before or after the block, if any. - /// True when the neighbor comes before the block, so its last character touches the block. - 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, - }; } \ No newline at end of file diff --git a/app/MindWork AI Studio/packages.lock.json b/app/MindWork AI Studio/packages.lock.json index 94cc07785..53ec20113 100644 --- a/app/MindWork AI Studio/packages.lock.json +++ b/app/MindWork AI Studio/packages.lock.json @@ -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" } diff --git a/app/Tests/Tools/HTMLParserConversionTests.cs b/app/Tests/Tools/HTMLParserConversionTests.cs index 07e9f7af7..4a0ae29b9 100644 --- a/app/Tests/Tools/HTMLParserConversionTests.cs +++ b/app/Tests/Tools/HTMLParserConversionTests.cs @@ -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. ///
/// - /// 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. /// [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 diff --git a/app/TrimSmokeTest/MarkdownChecks.cs b/app/TrimSmokeTest/MarkdownChecks.cs index c25c839ca..188901353 100644 --- a/app/TrimSmokeTest/MarkdownChecks.cs +++ b/app/TrimSmokeTest/MarkdownChecks.cs @@ -8,8 +8,8 @@ namespace AIStudio.TrimSmokeTest; /// /// 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. /// 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 diff --git a/documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md b/documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md index a5362f14c..fcc8bef56 100644 --- a/documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md +++ b/documentation/compatibility-shims/2026-11-reversemarkdown-blocks-in-inline-elements.md @@ -1,7 +1,8 @@ # ReverseMarkdown Blocks in Inline Elements -- Status: Active +- Status: Removed - Introduced: 2026-11-26 +- Removed: 2026-11-28, with ReverseMarkdown 6.3.1, which puts a space at the edges of a block which it flattens into the text of an inline element, unless whitespace is there already ([mysticmind/reversemarkdown-net#441](https://github.com/mysticmind/reversemarkdown-net/pull/441)). A figure with a caption inside a link now becomes one image with the caption as its alt text, as everywhere else, instead of every image of the figure. - Remove after: when a ReverseMarkdown release keeps the words of the blocks apart which it writes into the text of an inline element, such as a link, see [mysticmind/reversemarkdown-net#440](https://github.com/mysticmind/reversemarkdown-net/issues/440) - Code references: - `app/MindWork AI Studio/Tools/HTMLParser.cs` (`UnwrapBlocksInsideInlineElements`, `IsBlockInsideInlineElement`, `NeedsSeparator`, `INLINE_ELEMENTS`, `BLOCK_ELEMENTS`)