Skip to content

limit admonitions to attention, caution, note & tip #64

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

Merged
merged 1 commit into from
Nov 13, 2024
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
5 changes: 4 additions & 1 deletion src/Elastic.Markdown/Myst/Directives/AdmonitionBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class AdmonitionBlock(DirectiveBlockParser parser, string admonition, Dic
: DirectiveBlock(parser, properties)
{
public string Admonition => admonition == "admonition" ? Classes?.Trim() ?? "note" : admonition;

public override string Directive => Admonition;

public string? Classes { get; protected set; }
public string? CrossReferenceName { get; private set; }
public bool? DropdownOpen { get; private set; }
Expand All @@ -15,7 +18,7 @@ public string Title
{
get
{
var t = Admonition == "seealso" ? "see also" : Admonition;
var t = Admonition;
var title = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t);
if (admonition is "admonition" && !string.IsNullOrEmpty(Arguments))
title = Arguments;
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/Myst/Directives/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Elastic.Markdown.Myst.Directives;
public class CodeBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public string Directive => directive;
public override string Directive => directive;
public string? Caption { get; private set; }
public string? CrossReferenceName { get; private set; }

Expand Down
7 changes: 6 additions & 1 deletion src/Elastic.Markdown/Myst/Directives/DirectiveBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Elastic.Markdown.Diagnostics;
using Markdig.Helpers;
using Markdig.Syntax;

Expand All @@ -24,7 +25,6 @@ namespace Elastic.Markdown.Myst.Directives;
public abstract class DirectiveBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
: ContainerBlock(parser), IFencedBlock
{

public IReadOnlyDictionary<string, string> Properties { get; } = properties;

/// <inheritdoc />
Expand Down Expand Up @@ -89,5 +89,10 @@ protected bool PropBool(params string[] keys)
return default;
}

public abstract string Directive { get; }

protected void EmitError(ParserContext context, string message) =>
context.EmitError(Line + 1, 1, Directive.Length + 4 , message);


}
12 changes: 8 additions & 4 deletions src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public DirectiveBlockParser()

private Dictionary<string, string> _admonitionData = new();

private readonly string[] _admonitions = [
"admonition", "attention", "caution", "danger", "error", "hint", "important", "note", "tip", "seealso"
];
private readonly string[] _admonitions = [ "attention", "caution", "note", "tip" ];

private readonly string[] _versionBlocks = [ "versionadded", "versionchanged", "versionremoved", "deprecated" ];

Expand All @@ -59,7 +57,13 @@ public DirectiveBlockParser()
{ "sidebar", 4 },
{ "code-cell", 8 },


{ "admonition", 3 },
{ "attention", 3 },
{ "danger", 3 },
{ "error", 3 },
{ "hint", 3 },
{ "important", 3 },
{ "seealso", 3 }
}.ToFrozenDictionary();

protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
Expand Down
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Myst/Directives/ImageBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Elastic.Markdown.Myst.Directives;
public class ImageBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
: DirectiveBlock(parser, properties)
{
public override string Directive => "image";

public BuildContext Build { get; } = context.Build;

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions src/Elastic.Markdown/Myst/Directives/IncludeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Elastic.Markdown.Myst.Directives;
public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
: DirectiveBlock(parser, properties)
{
public override string Directive => "include";

public BuildContext Build { get; } = context.Build;

public IFileSystem FileSystem { get; } = context.Build.ReadFileSystem;
Expand All @@ -21,8 +23,6 @@ public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string

public bool Found { get; private set; }

protected virtual string Directive { get; } = "include";

public bool Literal { get; protected set; }
public string? Language { get; private set; }
public string? Caption { get; private set; }
Expand Down Expand Up @@ -54,7 +54,7 @@ public override void FinalizeAndValidate(ParserContext context)
if (FileSystem.File.Exists(IncludePath))
Found = true;
else
context.EmitError(Line, Column, "```{include}".Length , $"`{IncludePath}` does not exist.");
EmitError(context, $"`{IncludePath}` does not exist.");


}
Expand All @@ -66,5 +66,6 @@ public class LiteralIncludeBlock : IncludeBlock
public LiteralIncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
: base(parser, properties, context) => Literal = true;

protected override string Directive { get; } = "literalinclude";
public override string Directive => "literalinclude";

}
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Myst/Directives/MermaidBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Elastic.Markdown.Myst.Directives;
public class MermaidBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public override string Directive => "mermaid";

public override void FinalizeAndValidate(ParserContext context)
{
}
Expand Down
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/Myst/Directives/TabSetBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Elastic.Markdown.Myst.Directives;
public class TabSetBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public override string Directive => "tab-set";

public int Index { get; set; }
public override void FinalizeAndValidate(ParserContext context) => Index = FindIndex();

Expand All @@ -24,6 +26,8 @@ public int FindIndex()
public class TabItemBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public override string Directive => "tab-set-item";

public string Title { get; set; } = default!;
public int Index { get; set; }
public int TabSetIndex { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Elastic.Markdown.Myst.Directives;
public class UnknownDirectiveBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public string Directive => directive;
public override string Directive => directive;

public override void FinalizeAndValidate(ParserContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elastic.Markdown.Myst.Directives;
public class UnsupportedDirectiveBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties, int issueId)
: DirectiveBlock(parser, properties)
{
public string Directive => directive;
public override string Directive => directive;

public string IssueUrl => $"https://github.com/elastic/docs-builder/issues/{issueId}";

Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/Myst/Directives/VersionBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Elastic.Markdown.Myst.Directives;
public class VersionBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
: DirectiveBlock(parser, properties)
{
public string Directive => directive;
public override string Directive => directive;
public string Class => directive.Replace("version", "");

public string Title
Expand Down
49 changes: 2 additions & 47 deletions tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,18 @@ A regular paragraph.
public void SetsCorrectAdmonitionType() => Block!.Admonition.Should().Be(directive);
}

public class AttentionTests(ITestOutputHelper output) : AdmonitionTests(output, "attention")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Attention");
}
public class CautionTests(ITestOutputHelper output) : AdmonitionTests(output, "caution")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Caution");
}
public class DangerTests(ITestOutputHelper output) : AdmonitionTests(output, "danger")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Danger");
}
public class ErrorTests(ITestOutputHelper output) : AdmonitionTests(output, "error")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Error");
}
public class HintTests(ITestOutputHelper output) : AdmonitionTests(output, "hint")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Hint");
}
public class ImportantTests(ITestOutputHelper output) : AdmonitionTests(output, "important")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Important");
}

public class NoteTests(ITestOutputHelper output) : AdmonitionTests(output, "note")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Note");
}
public class SeeAlsoTests(ITestOutputHelper output) : AdmonitionTests(output, "seealso")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("See Also");
}

public class TipTests(ITestOutputHelper output) : AdmonitionTests(output, "tip")
{
[Fact]
Expand All @@ -85,23 +57,6 @@ A regular paragraph.
public void SetsCustomTitle() => Block!.Title.Should().Be("Note This is my custom note");
}

public class AdmonitionTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
"""
```{admonition} This is my custom note
This is an attention block
```
A regular paragraph.
"""
)
{
[Fact]
public void SetsCorrectAdmonitionType() => Block!.Admonition.Should().Be("note");

[Fact]
public void SetsCustomTitle() => Block!.Title.Should().Be("This is my custom note");
}


public class DropdownTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
"""
```{dropdown} This is my custom dropdown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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

using Elastic.Markdown.Myst.Directives;
using FluentAssertions;
using Xunit.Abstractions;

namespace Elastic.Markdown.Tests.Directives;

public abstract class AdmonitionUnsupportedTests(ITestOutputHelper output, string directive)
: DirectiveTest<UnsupportedDirectiveBlock>(output,
$$"""
```{{{directive}}}
This is an attention block
```
A regular paragraph.
"""
)
{
[Fact]
public void ParsesAsUnknown() => Block.Should().NotBeNull();

[Fact]
public void SetsCorrectDirective() => Block!.Directive.Should().Be(directive);
}

// ReSharper disable UnusedType.Global
public class AttentionTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "attention");
public class DangerTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "danger");
public class ErrorTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "error");
public class HintTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "hint");
public class ImportantTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "important");
public class SeeAlsoTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "seealso");
public class AdmonitionTitleTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "admonition");
// ReSharper restore UnusedType.Global