Skip to content

Commit eac8a6d

Browse files
committed
Add back AngleSharp.Diffing
1 parent c2e4d62 commit eac8a6d

File tree

6 files changed

+114
-15
lines changed

6 files changed

+114
-15
lines changed

src/Elastic.Markdown/IO/DocumentationFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public record SnippetFile(IFileInfo SourceFile, IDirectoryInfo RootPath)
3838
{
3939
if (_parsed)
4040
return Anchors;
41-
var document = parser.ParseAsync(SourceFile, frontMatter, default).GetAwaiter().GetResult();
4241
if (!SourceFile.Exists)
4342
{
4443
_parsed = true;
4544
return null;
4645
}
4746

47+
var document = parser.MinimalParseAsync(SourceFile, default).GetAwaiter().GetResult();
4848
var toc = MarkdownFile.GetAnchors(set, parser, frontMatter, document, new Dictionary<string, string>(), out var anchors);
4949
Anchors = new SnippetAnchors(anchors, toc);
5050
_parsed = true;

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ private static void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block)
231231
var document = parser.ParseAsync(file, block.FrontMatter, default).GetAwaiter().GetResult();
232232
var html = document.ToHtml(MarkdownParser.Pipeline);
233233
_ = renderer.Write(html);
234-
//var slice = Include.Create(new IncludeViewModel { Html = html });
235-
//RenderRazorSlice(slice, renderer, block);
236234
}
237235

238236
private static void WriteSettingsBlock(HtmlRenderer renderer, SettingsBlock block)

tests/authoring/Directives/IncludeBlocks.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,26 @@ type ``include hoists anchors and table of contents`` () =
5555
let ``has no errors`` () = generator |> hasNoErrors
5656

5757

58+
type ``include can contain links to parent page's includes`` () =
59+
60+
static let generator = Setup.Generate [
61+
Index """
62+
# A Document that lives at the root
63+
64+
:::{include} _snippets/my-snippet.md
65+
:::
66+
67+
:::{include} _snippets/my-other-snippet.md
68+
:::
69+
"""
70+
Snippet "_snippets/my-snippet.md" """
71+
## header from snippet [aa]
72+
"""
73+
74+
Snippet "_snippets/my-other-snippet.md" """
75+
[link to root with included anchor](../index.md#aa)
76+
"""
77+
]
78+
79+
[<Fact>]
80+
let ``has no errors`` () = generator |> hasNoErrors

tests/authoring/Framework/HtmlAssertions.fs

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,81 @@ namespace authoring
77
open System
88
open System.Diagnostics
99
open System.IO
10+
open AngleSharp.Diffing
11+
open AngleSharp.Diffing.Core
1012
open AngleSharp.Html
1113
open AngleSharp.Html.Parser
14+
open DiffPlex.DiffBuilder
15+
open DiffPlex.DiffBuilder.Model
1216
open JetBrains.Annotations
17+
open Swensen.Unquote
1318
open Xunit.Sdk
1419

1520
[<AutoOpen>]
1621
module HtmlAssertions =
1722

23+
let htmlDiffString (diffs: seq<IDiff>) =
24+
let NodeName (source:ComparisonSource) = source.Node.NodeType.ToString().ToLowerInvariant();
25+
let htmlText (source:IDiff) =
26+
let formatter = PrettyMarkupFormatter();
27+
let nodeText (control: ComparisonSource) =
28+
use sw = new StringWriter()
29+
control.Node.ToHtml(sw, formatter)
30+
sw.ToString()
31+
let attrText (control: AttributeComparisonSource) =
32+
use sw = new StringWriter()
33+
control.Attribute.ToHtml(sw, formatter)
34+
sw.ToString()
35+
let nodeDiffText (control: ComparisonSource option) (test: ComparisonSource option) =
36+
let actual = match test with Some t -> nodeText t | None -> "missing"
37+
let expected = match control with Some t -> nodeText t | None -> "missing"
38+
$"""
39+
40+
expected: {expected}
41+
actual: {actual}
42+
"""
43+
let attrDiffText (control: AttributeComparisonSource option) (test: AttributeComparisonSource option) =
44+
let actual = match test with Some t -> attrText t | None -> "missing"
45+
let expected = match control with Some t -> attrText t | None -> "missing"
46+
$"""
47+
48+
expected: {expected}
49+
actual: {actual}
50+
"""
51+
52+
match source with
53+
| :? NodeDiff as diff -> nodeDiffText <| Some diff.Control <| Some diff.Test
54+
| :? AttrDiff as diff -> attrDiffText <| Some diff.Control <| Some diff.Test
55+
| :? MissingNodeDiff as diff -> nodeDiffText <| Some diff.Control <| None
56+
| :? MissingAttrDiff as diff -> attrDiffText <| Some diff.Control <| None
57+
| :? UnexpectedNodeDiff as diff -> nodeDiffText None <| Some diff.Test
58+
| :? UnexpectedAttrDiff as diff -> attrDiffText None <| Some diff.Test
59+
| _ -> failwith $"Unknown diff type detected: {source.GetType()}"
60+
61+
diffs
62+
|> Seq.map (fun diff ->
63+
64+
match diff with
65+
| :? NodeDiff as diff when diff.Target = DiffTarget.Text && diff.Control.Path.Equals(diff.Test.Path, StringComparison.Ordinal)
66+
-> $"The text in {diff.Control.Path} is different."
67+
| :? NodeDiff as diff when diff.Target = DiffTarget.Text
68+
-> $"The expected {NodeName(diff.Control)} at {diff.Control.Path} and the actual {NodeName(diff.Test)} at {diff.Test.Path} is different."
69+
| :? NodeDiff as diff when diff.Control.Path.Equals(diff.Test.Path, StringComparison.Ordinal)
70+
-> $"The {NodeName(diff.Control)}s at {diff.Control.Path} are different."
71+
| :? NodeDiff as diff -> $"The expected {NodeName(diff.Control)} at {diff.Control.Path} and the actual {NodeName(diff.Test)} at {diff.Test.Path} are different."
72+
| :? AttrDiff as diff when diff.Control.Path.Equals(diff.Test.Path, StringComparison.Ordinal)
73+
-> $"The values of the attributes at {diff.Control.Path} are different."
74+
| :? AttrDiff as diff -> $"The value of the attribute {diff.Control.Path} and actual attribute {diff.Test.Path} are different."
75+
| :? MissingNodeDiff as diff -> $"The {NodeName(diff.Control)} at {diff.Control.Path} is missing."
76+
| :? MissingAttrDiff as diff -> $"The attribute at {diff.Control.Path} is missing."
77+
| :? UnexpectedNodeDiff as diff -> $"The {NodeName(diff.Test)} at {diff.Test.Path} was not expected."
78+
| :? UnexpectedAttrDiff as diff -> $"The attribute at {diff.Test.Path} was not expected."
79+
| _ -> failwith $"Unknown diff type detected: {diff.GetType()}"
80+
+
81+
htmlText diff
82+
)
83+
|> String.concat "\n"
84+
1885
let private prettyHtml (html:string) (querySelector: string option) =
1986
let parser = HtmlParser()
2087
let document = parser.ParseDocument(html)
@@ -23,35 +90,46 @@ module HtmlAssertions =
2390
| Some q -> document.QuerySelector q
2491
| None -> document.Body
2592
use sw = new StringWriter()
93+
let formatter = PrettyMarkupFormatter()
2694
element.Children
27-
|> Seq.iter _.ToHtml(sw, PrettyMarkupFormatter())
95+
|> Seq.indexed
96+
|> Seq.filter (fun (i, c) -> (not <| (i = 0 && c.TagName = "H1")))
97+
|> Seq.map(fun (_, c) -> c)
98+
|> Seq.iter _.ToHtml(sw, formatter)
2899
sw.ToString().TrimStart('\n')
29100

30101
let private createDiff expected actual =
31-
let expectedHtml = prettyHtml expected None
32-
let actualHtml = prettyHtml actual (Some "section#elastic-docs-v3")
33-
let textDiff = diff expectedHtml actualHtml
34-
match textDiff with
102+
let diffs =
103+
DiffBuilder
104+
.Compare(actual)
105+
.WithTest(expected)
106+
.Build()
107+
108+
let deepComparision = htmlDiffString diffs
109+
match deepComparision with
35110
| s when String.IsNullOrEmpty s -> ()
36111
| s ->
112+
let textDiff = diff expected actual
37113
let msg = $"""Html was not equal
38114
-- DIFF --
39115
{textDiff}
40116
41-
-- Actual HTML --
42-
{actualHtml}
117+
-- Comparison --
118+
{deepComparision}
43119
"""
44120
raise (XunitException(msg))
45121

46122
[<DebuggerStepThrough>]
47123
let toHtml ([<LanguageInjection("html")>]expected: string) (actual: MarkdownResult) =
48-
createDiff expected actual.Html
124+
let expectedHtml = prettyHtml expected None
125+
let actualHtml = prettyHtml actual.Html (Some "section#elastic-docs-v3")
126+
createDiff expectedHtml actualHtml
49127

50128
[<DebuggerStepThrough>]
51129
let convertsToHtml ([<LanguageInjection("html")>]expected: string) (actual: Lazy<GeneratorResults>) =
52130
let actual = actual.Value
53131

54-
let defaultFile = actual.MarkdownResults |> Seq.head
132+
let defaultFile = actual.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
55133
defaultFile |> toHtml expected
56134

57135
[<DebuggerStepThrough>]
@@ -75,5 +153,5 @@ But was not found in:
75153
let convertsToContainingHtml ([<LanguageInjection("html")>]expected: string) (actual: Lazy<GeneratorResults>) =
76154
let actual = actual.Value
77155

78-
let defaultFile = actual.MarkdownResults |> Seq.head
156+
let defaultFile = actual.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
79157
defaultFile |> containsHtml expected

tests/authoring/Framework/MarkdownDocumentAssertions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module MarkdownDocumentAssertions =
3030
[<DebuggerStepThrough>]
3131
let appliesTo (expectedAvailability: ApplicableTo) (actual: Lazy<GeneratorResults>) =
3232
let actual = actual.Value
33-
let result = actual.MarkdownResults |> Seq.head
33+
let result = actual.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
3434
let matter = result.File.YamlFrontMatter
3535
match matter with
3636
| NonNull m ->

tests/authoring/authoring.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
2222
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.29"/>
2323
<PackageReference Include="FsUnit.xUnit" Version="7.0.1"/>
24-
<PackageReference Include="AngleSharp" Version="1.2.0"/>
24+
<PackageReference Include="AngleSharp.Diffing" Version="1.0.0"/>
2525
<PackageReference Include="DiffPlex" Version="1.7.2"/>
2626
<PackageReference Include="Unquote" Version="7.0.1"/>
2727
</ItemGroup>

0 commit comments

Comments
 (0)