Skip to content

Commit 18b4840

Browse files
authored
Warn of files with no title (#91)
1 parent 8e19656 commit 18b4840

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,26 @@ public virtual async Task StopAsync(CancellationToken cancellationToken)
138138
await _started;
139139
await Channel.Reader.Completion;
140140
}
141+
142+
143+
public void EmitError(string file, string message)
144+
{
145+
var d = new Diagnostic
146+
{
147+
Severity = Severity.Error,
148+
File = file,
149+
Message = message,
150+
};
151+
Channel.Write(d);
152+
}
153+
public void EmitWarning(string file, string message)
154+
{
155+
var d = new Diagnostic
156+
{
157+
Severity = Severity.Warning,
158+
File = file,
159+
Message = message,
160+
};
161+
Channel.Write(d);
162+
}
141163
}

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44
using System.IO.Abstractions;
5+
using Elastic.Markdown.Diagnostics;
56
using Elastic.Markdown.Myst;
67
using Elastic.Markdown.Myst.Directives;
78
using Elastic.Markdown.Slices;
@@ -23,8 +24,11 @@ public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParse
2324
FileName = sourceFile.Name;
2425
UrlPathPrefix = context.UrlPathPrefix;
2526
MarkdownParser = parser;
27+
Collector = context.Collector;
2628
}
2729

30+
public DiagnosticsCollector Collector { get; }
31+
2832
public string? UrlPathPrefix { get; }
2933
private MarkdownParser MarkdownParser { get; }
3034
public YamlFrontMatter? YamlFrontMatter { get; private set; }
@@ -60,6 +64,8 @@ public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
6064
await MinimalParse(ctx);
6165

6266
var document = await MarkdownParser.ParseAsync(SourceFile, YamlFrontMatter, ctx);
67+
if (Title == RelativePath)
68+
Collector.EmitWarning(SourceFile.FullName, "Missing yaml front-matter block defining a title or a level 1 header");
6369
return document;
6470
}
6571

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ protected DirectiveTest(ITestOutputHelper output, [LanguageInjection("markdown")
5959
var logger = new TestLoggerFactory(output);
6060
FileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
6161
{
62-
{ "docs/source/index.md", new MockFileData(content) }
62+
{ "docs/source/index.md", new MockFileData(string.IsNullOrEmpty(content) || content.StartsWith("---") ? content :
63+
// language=markdown
64+
$"""
65+
---
66+
title: Test Document
67+
---
68+
69+
{content}
70+
"""
71+
)}
6372
}, new MockFileSystemOptions
6473
{
6574
CurrentDirectory = Paths.Root.FullName

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,17 @@ public void ReadsSubstitutions()
3232
.And.ContainKey("key");
3333
}
3434
}
35+
36+
public class EmptyFileWarnsNeedingATitle(ITestOutputHelper output) : DirectiveTest(output, "")
37+
{
38+
[Fact]
39+
public void ReadsTitle() => File.Title.Should().Be("index.md");
40+
41+
[Fact]
42+
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("index.md");
43+
44+
[Fact]
45+
public void WarnsOfNoTitle() =>
46+
Collector.Diagnostics.Should().NotBeEmpty()
47+
.And.Contain(d=>d.Message.Contains("Missing yaml front-matter block defining a title"));
48+
}

tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ namespace Elastic.Markdown.Tests.Inline;
1212

1313
public abstract class AnchorLinkTestBase(ITestOutputHelper output, [LanguageInjection("markdown")] string content)
1414
: InlineTest<LinkInline>(output,
15-
$"""
16-
## Hello world
15+
$"""
16+
## Hello world
1717
18-
A paragraph
18+
A paragraph
1919
20-
{content}
20+
{content}
2121
22-
""")
22+
""")
2323
{
2424
protected override void AddToFileSystem(MockFileSystem fileSystem)
2525
{

tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ protected InlineTest(ITestOutputHelper output, [LanguageInjection("markdown")]st
6969
var logger = new TestLoggerFactory(output);
7070
FileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
7171
{
72-
{ "docs/source/index.md", new MockFileData(content) }
72+
{ "docs/source/index.md", new MockFileData(string.IsNullOrEmpty(content) || content.StartsWith("---") ? content :
73+
// language=markdown
74+
$"""
75+
---
76+
title: Test Document
77+
---
78+
79+
{content}
80+
"""
81+
)}
7382
}, new MockFileSystemOptions
7483
{
7584
CurrentDirectory = Paths.Root.FullName

0 commit comments

Comments
 (0)