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

Add tests for nested and indented inline anchors #359

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public MarkdownFile[] YieldParents()
return parents.ToArray();
}

public async Task<MarkdownDocument> MinimalParse(Cancel ctx)
public async Task<MarkdownDocument> MinimalParseAsync(Cancel ctx)
{
var document = await MarkdownParser.MinimalParseAsync(SourceFile, ctx);
ReadDocumentInstructions(document);
Expand All @@ -99,7 +99,7 @@ public async Task<MarkdownDocument> MinimalParse(Cancel ctx)
public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
{
if (!_instructionsParsed)
await MinimalParse(ctx);
await MinimalParseAsync(ctx);

var document = await MarkdownParser.ParseAsync(SourceFile, YamlFrontMatter, ctx);
return document;
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public async Task Resolve(Cancel ctx = default)
if (_resolved)
return;

await Parallel.ForEachAsync(FilesInOrder, ctx, async (file, token) => await file.MinimalParse(token));
await Parallel.ForEachAsync(FilesInOrder, ctx, async (file, token) => await file.MinimalParseAsync(token));
await Parallel.ForEachAsync(GroupsInOrder, ctx, async (group, token) => await group.Resolve(token));

await (Index?.MinimalParse(ctx) ?? Task.CompletedTask);
await (Index?.MinimalParseAsync(ctx) ?? Task.CompletedTask);

_resolved = true;
}
Expand Down
80 changes: 53 additions & 27 deletions src/Elastic.Markdown/Myst/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Elastic.Markdown.Myst.Substitution;
using Markdig;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Parsers;
using Markdig.Syntax;

namespace Elastic.Markdown.Myst;
Expand All @@ -28,34 +29,59 @@ public class MarkdownParser(

private BuildContext Context { get; } = context;

public static MarkdownPipeline MinimalPipeline { get; } =
new MarkdownPipelineBuilder()
.UseYamlFrontMatter()
.UseInlineAnchors()
.UseHeadingsWithSlugs()
.UseDirectives()
.Build();
// ReSharper disable once InconsistentNaming
private static MarkdownPipeline? _minimalPipeline;
public static MarkdownPipeline MinimalPipeline
{
get
{
if (_minimalPipeline is not null)
return _minimalPipeline;
var builder = new MarkdownPipelineBuilder()
.UseYamlFrontMatter()
.UseInlineAnchors()
.UseHeadingsWithSlugs()
.UseDirectives();

builder.BlockParsers.TryRemove<IndentedCodeBlockParser>();
_minimalPipeline = builder.Build();
return _minimalPipeline;

}
}

public static MarkdownPipeline Pipeline { get; } =
new MarkdownPipelineBuilder()
.EnableTrackTrivia()
.UseInlineAnchors()
.UsePreciseSourceLocation()
.UseDiagnosticLinks()
.UseHeadingsWithSlugs()
.UseEmphasisExtras(EmphasisExtraOptions.Default)
.UseSoftlineBreakAsHardlineBreak()
.UseSubstitution()
.UseComments()
.UseYamlFrontMatter()
.UseGridTables()
.UsePipeTables()
.UseDirectives()
.UseDefinitionLists()
.UseEnhancedCodeBlocks()
.DisableHtml()
.UseHardBreaks()
.Build();
// ReSharper disable once InconsistentNaming
private static MarkdownPipeline? _pipeline;
public static MarkdownPipeline Pipeline
{
get
{
if (_pipeline is not null)
return _pipeline;

var builder = new MarkdownPipelineBuilder()
.EnableTrackTrivia()
.UseInlineAnchors()
.UsePreciseSourceLocation()
.UseDiagnosticLinks()
.UseHeadingsWithSlugs()
.UseEmphasisExtras(EmphasisExtraOptions.Default)
.UseSoftlineBreakAsHardlineBreak()
.UseSubstitution()
.UseComments()
.UseYamlFrontMatter()
.UseGridTables()
.UsePipeTables()
.UseDirectives()
.UseDefinitionLists()
.UseEnhancedCodeBlocks()
.DisableHtml()
.UseHardBreaks();
builder.BlockParsers.TryRemove<IndentedCodeBlockParser>();
_pipeline = builder.Build();
return _pipeline;
}
}

public ConfigurationFile Configuration { get; } = configuration;

Expand Down
3 changes: 2 additions & 1 deletion tests/authoring/Framework/ErrorCollectorAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module DiagnosticsCollectorAssertions =
[<DebuggerStepThrough>]
let hasNoErrors (actual: Lazy<GeneratorResults>) =
let actual = actual.Value
test <@ actual.Context.Collector.Errors = 0 @>
let errors = actual.Context.Collector.Errors
test <@ errors = 0 @>

[<DebuggerStepThrough>]
let hasError (expected: string) (actual: Lazy<GeneratorResults>) =
Expand Down
31 changes: 27 additions & 4 deletions tests/authoring/Framework/HtmlAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open AngleSharp.Html.Parser
open DiffPlex.DiffBuilder
open DiffPlex.DiffBuilder.Model
open JetBrains.Annotations
open Swensen.Unquote
open Xunit.Sdk

[<AutoOpen>]
Expand Down Expand Up @@ -87,7 +88,7 @@ actual: {actual}
use sw = new StringWriter()
document.Body.Children
|> Seq.iter _.ToHtml(sw, PrettyMarkupFormatter())
sw.ToString()
sw.ToString().TrimStart('\n')

let private createDiff expected actual =
let diffs =
Expand All @@ -110,15 +111,37 @@ actual: {actual}
"""
raise (XunitException(msg))

[<DebuggerStepThrough>]
let toHtml ([<LanguageInjection("html")>]expected: string) (actual: MarkdownResult) =
createDiff expected actual.Html

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

let defaultFile = actual.MarkdownResults |> Seq.head
createDiff expected defaultFile.Html
defaultFile |> toHtml expected

[<DebuggerStepThrough>]
let toHtml ([<LanguageInjection("html")>]expected: string) (actual: MarkdownResult) =
createDiff expected actual.Html
let containsHtml ([<LanguageInjection("html")>]expected: string) (actual: MarkdownResult) =

let prettyExpected = prettyHtml expected
let prettyActual = prettyHtml actual.Html

if not <| prettyActual.Contains prettyExpected then
let msg = $"""Expected html to contain:
{prettyExpected}

But was not found in:

{prettyActual}
"""
raise (XunitException(msg))


[<DebuggerStepThrough>]
let convertsToContainingHtml ([<LanguageInjection("html")>]expected: string) (actual: Lazy<GeneratorResults>) =
let actual = actual.Value

let defaultFile = actual.MarkdownResults |> Seq.head
defaultFile |> containsHtml expected
25 changes: 25 additions & 0 deletions tests/authoring/Framework/MarkdownDocumentAssertions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

namespace authoring

open System.Diagnostics
open Markdig.Syntax
open Xunit.Sdk

module MarkdownDocumentAssertions =

[<DebuggerStepThrough>]
let parses<'element when 'element :> MarkdownObject> (actual: MarkdownResult) =
let unsupportedBlocks = actual.Document.Descendants<'element>() |> Array.ofSeq
if unsupportedBlocks.Length = 0 then
raise (XunitException($"Could not find {typedefof<'element>.Name} in fully parsed document"))
unsupportedBlocks;

[<DebuggerStepThrough>]
let parsesMinimal<'element when 'element :> MarkdownObject> (actual: MarkdownResult) =
let unsupportedBlocks = actual.MinimalParse.Descendants<'element>() |> Array.ofSeq
if unsupportedBlocks.Length = 0 then
raise (XunitException($"Could not find {typedefof<'element>.Name} in minimally parsed document"))
unsupportedBlocks;
4 changes: 3 additions & 1 deletion tests/authoring/Framework/TestValues.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type TestLoggerFactory () =

type MarkdownResult = {
File: MarkdownFile
MinimalParse: MarkdownDocument
Document: MarkdownDocument
Html: string
Context: MarkdownTestContext
Expand Down Expand Up @@ -89,8 +90,9 @@ and MarkdownTestContext =
|> Seq.map (fun (f: MarkdownFile) -> task {
// technically we do this work twice since generate all also does it
let! document = f.ParseFullAsync(ctx)
let! minimal = f.MinimalParseAsync(ctx)
let html = f.CreateHtml(document)
return { File = f; Document = document; Html = html; Context = this }
return { File = f; Document = document; MinimalParse = minimal; Html = html; Context = this }
})
// this is not great code, refactor or depend on FSharp.Control.TaskSeq
// for now this runs without issue
Expand Down
85 changes: 85 additions & 0 deletions tests/authoring/Inline/InlineAnchors.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

module ``inline elements``.``anchors DEPRECATED``

open Elastic.Markdown.Myst.InlineParsers
open Markdig.Syntax
open Swensen.Unquote
open System.Linq
open Xunit
open authoring
open authoring.MarkdownDocumentAssertions

type ``inline anchor in the middle`` () =

Expand All @@ -22,3 +27,83 @@ this is *regular* text and this $$$is-an-inline-anchor$$$ and this continues to
"""
[<Fact>]
let ``has no errors`` () = markdown |> hasNoErrors

type ``inline anchors embedded in definition lists`` () =

static let markdown = Setup.Generate [
Index """# Testing nested inline anchors

$$$search-type$$$

`search_type`
: (Optional, string) How distributed term frequencies are calculated for relevance scoring.

::::{dropdown} Valid values for `search_type`
`query_then_fetch`
: (Default) Distributed term frequencies are calculated locally for each shard running the search. We recommend this option for faster searches with potentially less accurate scoring.

$$$dfs-query-then-fetch$$$

`dfs_query_then_fetch`
: Distributed term frequencies are calculated globally, using information gathered from all shards running the search.

::::
"""
Markdown "file.md" """
[Link to first](index.md#search-type)
[Link to second](index.md#dfs-query-then-fetch)
"""
]

[<Fact>]
let ``emits nested inline anchor`` () =
markdown |> convertsToContainingHtml """<a id="dfs-query-then-fetch"></a>"""

[<Fact>]
let ``emits definition list block anchor`` () =
markdown |> convertsToContainingHtml """<a id="search-type"></a>"""

[<Fact>]
let ``has no errors`` () = markdown |> hasNoErrors

[<Fact>]
let ``minimal parse sees two inline anchors`` () =
let inlineAnchors = markdown |> converts "index.md" |> parsesMinimal<InlineAnchor>
test <@ inlineAnchors.Length = 2 @>



type ``inline anchors embedded in indented code`` () =

static let markdown = Setup.Generate [
Index """# Testing nested inline anchors

$$$search-type$$$

indented codeblock

$$$dfs-query-then-fetch$$$

block
"""
Markdown "file.md" """
[Link to first](index.md#search-type)
[Link to second](index.md#dfs-query-then-fetch)
"""
]

[<Fact>]
let ``emits nested inline anchor`` () =
markdown |> convertsToContainingHtml """<a id="dfs-query-then-fetch"></a>"""

[<Fact>]
let ``emits definition list block anchor`` () =
markdown |> convertsToContainingHtml """<a id="search-type"></a>"""

[<Fact>]
let ``has no errors`` () = markdown |> hasNoErrors

[<Fact>]
let ``minimal parse sees two inline anchors`` () =
let inlineAnchors = markdown |> converts "index.md" |> parsesMinimal<InlineAnchor>
test <@ inlineAnchors.Length = 2 @>
1 change: 1 addition & 0 deletions tests/authoring/authoring.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Compile Include="Framework\MarkdownResultsAssertions.fs" />
<Compile Include="Framework\HtmlAssertions.fs" />
<Compile Include="Framework\ErrorCollectorAssertions.fs" />
<Compile Include="Framework\MarkdownDocumentAssertions.fs" />
<Compile Include="Inline\Substitutions.fs" />
</ItemGroup>

Expand Down
Loading