Skip to content

Commit f20fce1

Browse files
authored
limit admonitions to attention, caution, note & tip (#64)
1 parent f5be0d5 commit f20fce1

13 files changed

+73
-61
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class AdmonitionBlock(DirectiveBlockParser parser, string admonition, Dic
77
: DirectiveBlock(parser, properties)
88
{
99
public string Admonition => admonition == "admonition" ? Classes?.Trim() ?? "note" : admonition;
10+
11+
public override string Directive => Admonition;
12+
1013
public string? Classes { get; protected set; }
1114
public string? CrossReferenceName { get; private set; }
1215
public bool? DropdownOpen { get; private set; }
@@ -15,7 +18,7 @@ public string Title
1518
{
1619
get
1720
{
18-
var t = Admonition == "seealso" ? "see also" : Admonition;
21+
var t = Admonition;
1922
var title = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t);
2023
if (admonition is "admonition" && !string.IsNullOrEmpty(Arguments))
2124
title = Arguments;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Elastic.Markdown.Myst.Directives;
66
public class CodeBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
77
: DirectiveBlock(parser, properties)
88
{
9-
public string Directive => directive;
9+
public override string Directive => directive;
1010
public string? Caption { get; private set; }
1111
public string? CrossReferenceName { get; private set; }
1212

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This file is licensed under the BSD-Clause 2 license.
66
// See the license.txt file in the project root for more information.
77

8+
using Elastic.Markdown.Diagnostics;
89
using Markdig.Helpers;
910
using Markdig.Syntax;
1011

@@ -24,7 +25,6 @@ namespace Elastic.Markdown.Myst.Directives;
2425
public abstract class DirectiveBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
2526
: ContainerBlock(parser), IFencedBlock
2627
{
27-
2828
public IReadOnlyDictionary<string, string> Properties { get; } = properties;
2929

3030
/// <inheritdoc />
@@ -89,5 +89,10 @@ protected bool PropBool(params string[] keys)
8989
return default;
9090
}
9191

92+
public abstract string Directive { get; }
93+
94+
protected void EmitError(ParserContext context, string message) =>
95+
context.EmitError(Line + 1, 1, Directive.Length + 4 , message);
96+
9297

9398
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public DirectiveBlockParser()
3030

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

33-
private readonly string[] _admonitions = [
34-
"admonition", "attention", "caution", "danger", "error", "hint", "important", "note", "tip", "seealso"
35-
];
33+
private readonly string[] _admonitions = [ "attention", "caution", "note", "tip" ];
3634

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

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

62-
60+
{ "admonition", 3 },
61+
{ "attention", 3 },
62+
{ "danger", 3 },
63+
{ "error", 3 },
64+
{ "hint", 3 },
65+
{ "important", 3 },
66+
{ "seealso", 3 }
6367
}.ToFrozenDictionary();
6468

6569
protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Elastic.Markdown.Myst.Directives;
66
public class ImageBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
77
: DirectiveBlock(parser, properties)
88
{
9+
public override string Directive => "image";
10+
911
public BuildContext Build { get; } = context.Build;
1012

1113
/// <summary>

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Elastic.Markdown.Myst.Directives;
99
public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
1010
: DirectiveBlock(parser, properties)
1111
{
12+
public override string Directive => "include";
13+
1214
public BuildContext Build { get; } = context.Build;
1315

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

2224
public bool Found { get; private set; }
2325

24-
protected virtual string Directive { get; } = "include";
25-
2626
public bool Literal { get; protected set; }
2727
public string? Language { get; private set; }
2828
public string? Caption { get; private set; }
@@ -54,7 +54,7 @@ public override void FinalizeAndValidate(ParserContext context)
5454
if (FileSystem.File.Exists(IncludePath))
5555
Found = true;
5656
else
57-
context.EmitError(Line, Column, "```{include}".Length , $"`{IncludePath}` does not exist.");
57+
EmitError(context, $"`{IncludePath}` does not exist.");
5858

5959

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

69-
protected override string Directive { get; } = "literalinclude";
69+
public override string Directive => "literalinclude";
70+
7071
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Elastic.Markdown.Myst.Directives;
66
public class MermaidBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
77
: DirectiveBlock(parser, properties)
88
{
9+
public override string Directive => "mermaid";
10+
911
public override void FinalizeAndValidate(ParserContext context)
1012
{
1113
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Elastic.Markdown.Myst.Directives;
99
public class TabSetBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
1010
: DirectiveBlock(parser, properties)
1111
{
12+
public override string Directive => "tab-set";
13+
1214
public int Index { get; set; }
1315
public override void FinalizeAndValidate(ParserContext context) => Index = FindIndex();
1416

@@ -24,6 +26,8 @@ public int FindIndex()
2426
public class TabItemBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
2527
: DirectiveBlock(parser, properties)
2628
{
29+
public override string Directive => "tab-set-item";
30+
2731
public string Title { get; set; } = default!;
2832
public int Index { get; set; }
2933
public int TabSetIndex { get; set; }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Elastic.Markdown.Myst.Directives;
77
public class UnknownDirectiveBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
88
: DirectiveBlock(parser, properties)
99
{
10-
public string Directive => directive;
10+
public override string Directive => directive;
1111

1212
public override void FinalizeAndValidate(ParserContext context)
1313
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Elastic.Markdown.Myst.Directives;
99
public class UnsupportedDirectiveBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties, int issueId)
1010
: DirectiveBlock(parser, properties)
1111
{
12-
public string Directive => directive;
12+
public override string Directive => directive;
1313

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Elastic.Markdown.Myst.Directives;
66
public class VersionBlock(DirectiveBlockParser parser, string directive, Dictionary<string, string> properties)
77
: DirectiveBlock(parser, properties)
88
{
9-
public string Directive => directive;
9+
public override string Directive => directive;
1010
public string Class => directive.Replace("version", "");
1111

1212
public string Title

tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,18 @@ A regular paragraph.
2323
public void SetsCorrectAdmonitionType() => Block!.Admonition.Should().Be(directive);
2424
}
2525

26-
public class AttentionTests(ITestOutputHelper output) : AdmonitionTests(output, "attention")
27-
{
28-
[Fact]
29-
public void SetsTitle() => Block!.Title.Should().Be("Attention");
30-
}
3126
public class CautionTests(ITestOutputHelper output) : AdmonitionTests(output, "caution")
3227
{
3328
[Fact]
3429
public void SetsTitle() => Block!.Title.Should().Be("Caution");
3530
}
36-
public class DangerTests(ITestOutputHelper output) : AdmonitionTests(output, "danger")
37-
{
38-
[Fact]
39-
public void SetsTitle() => Block!.Title.Should().Be("Danger");
40-
}
41-
public class ErrorTests(ITestOutputHelper output) : AdmonitionTests(output, "error")
42-
{
43-
[Fact]
44-
public void SetsTitle() => Block!.Title.Should().Be("Error");
45-
}
46-
public class HintTests(ITestOutputHelper output) : AdmonitionTests(output, "hint")
47-
{
48-
[Fact]
49-
public void SetsTitle() => Block!.Title.Should().Be("Hint");
50-
}
51-
public class ImportantTests(ITestOutputHelper output) : AdmonitionTests(output, "important")
52-
{
53-
[Fact]
54-
public void SetsTitle() => Block!.Title.Should().Be("Important");
55-
}
31+
5632
public class NoteTests(ITestOutputHelper output) : AdmonitionTests(output, "note")
5733
{
5834
[Fact]
5935
public void SetsTitle() => Block!.Title.Should().Be("Note");
6036
}
61-
public class SeeAlsoTests(ITestOutputHelper output) : AdmonitionTests(output, "seealso")
62-
{
63-
[Fact]
64-
public void SetsTitle() => Block!.Title.Should().Be("See Also");
65-
}
37+
6638
public class TipTests(ITestOutputHelper output) : AdmonitionTests(output, "tip")
6739
{
6840
[Fact]
@@ -85,23 +57,6 @@ A regular paragraph.
8557
public void SetsCustomTitle() => Block!.Title.Should().Be("Note This is my custom note");
8658
}
8759

88-
public class AdmonitionTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
89-
"""
90-
```{admonition} This is my custom note
91-
This is an attention block
92-
```
93-
A regular paragraph.
94-
"""
95-
)
96-
{
97-
[Fact]
98-
public void SetsCorrectAdmonitionType() => Block!.Admonition.Should().Be("note");
99-
100-
[Fact]
101-
public void SetsCustomTitle() => Block!.Title.Should().Be("This is my custom note");
102-
}
103-
104-
10560
public class DropdownTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
10661
"""
10762
```{dropdown} This is my custom dropdown
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Markdown.Myst.Directives;
6+
using FluentAssertions;
7+
using Xunit.Abstractions;
8+
9+
namespace Elastic.Markdown.Tests.Directives;
10+
11+
public abstract class AdmonitionUnsupportedTests(ITestOutputHelper output, string directive)
12+
: DirectiveTest<UnsupportedDirectiveBlock>(output,
13+
$$"""
14+
```{{{directive}}}
15+
This is an attention block
16+
```
17+
A regular paragraph.
18+
"""
19+
)
20+
{
21+
[Fact]
22+
public void ParsesAsUnknown() => Block.Should().NotBeNull();
23+
24+
[Fact]
25+
public void SetsCorrectDirective() => Block!.Directive.Should().Be(directive);
26+
}
27+
28+
// ReSharper disable UnusedType.Global
29+
public class AttentionTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "attention");
30+
public class DangerTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "danger");
31+
public class ErrorTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "error");
32+
public class HintTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "hint");
33+
public class ImportantTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "important");
34+
public class SeeAlsoTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "seealso");
35+
public class AdmonitionTitleTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "admonition");
36+
// ReSharper restore UnusedType.Global

0 commit comments

Comments
 (0)