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

Warn of files with no title #91

Merged
merged 2 commits into from
Dec 3, 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
22 changes: 22 additions & 0 deletions src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,26 @@ public virtual async Task StopAsync(CancellationToken cancellationToken)
await _started;
await Channel.Reader.Completion;
}


public void EmitError(string file, string message)
{
var d = new Diagnostic
{
Severity = Severity.Error,
File = file,
Message = message,
};
Channel.Write(d);
}
public void EmitWarning(string file, string message)
{
var d = new Diagnostic
{
Severity = Severity.Warning,
File = file,
Message = message,
};
Channel.Write(d);
}
}
6 changes: 6 additions & 0 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 System.IO.Abstractions;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.Myst;
using Elastic.Markdown.Myst.Directives;
using Elastic.Markdown.Slices;
Expand All @@ -23,8 +24,11 @@ public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParse
FileName = sourceFile.Name;
UrlPathPrefix = context.UrlPathPrefix;
MarkdownParser = parser;
Collector = context.Collector;
}

public DiagnosticsCollector Collector { get; }

public string? UrlPathPrefix { get; }
private MarkdownParser MarkdownParser { get; }
public YamlFrontMatter? YamlFrontMatter { get; private set; }
Expand Down Expand Up @@ -60,6 +64,8 @@ public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
await MinimalParse(ctx);

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

Expand Down
11 changes: 10 additions & 1 deletion tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ protected DirectiveTest(ITestOutputHelper output, [LanguageInjection("markdown")
var logger = new TestLoggerFactory(output);
FileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "docs/source/index.md", new MockFileData(content) }
{ "docs/source/index.md", new MockFileData(string.IsNullOrEmpty(content) || content.StartsWith("---") ? content :
// language=markdown
$"""
---
title: Test Document
---

{content}
"""
)}
}, new MockFileSystemOptions
{
CurrentDirectory = Paths.Root.FullName
Expand Down
14 changes: 14 additions & 0 deletions tests/Elastic.Markdown.Tests/Directives/YamlFrontMatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ public void ReadsSubstitutions()
.And.ContainKey("key");
}
}

public class EmptyFileWarnsNeedingATitle(ITestOutputHelper output) : DirectiveTest(output, "")
{
[Fact]
public void ReadsTitle() => File.Title.Should().Be("index.md");

[Fact]
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("index.md");

[Fact]
public void WarnsOfNoTitle() =>
Collector.Diagnostics.Should().NotBeEmpty()
.And.Contain(d=>d.Message.Contains("Missing yaml front-matter block defining a title"));
}
10 changes: 5 additions & 5 deletions tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Elastic.Markdown.Tests.Inline;

public abstract class AnchorLinkTestBase(ITestOutputHelper output, [LanguageInjection("markdown")] string content)
: InlineTest<LinkInline>(output,
$"""
## Hello world
$"""
## Hello world

A paragraph
A paragraph

{content}
{content}

""")
""")
{
protected override void AddToFileSystem(MockFileSystem fileSystem)
{
Expand Down
11 changes: 10 additions & 1 deletion tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ protected InlineTest(ITestOutputHelper output, [LanguageInjection("markdown")]st
var logger = new TestLoggerFactory(output);
FileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "docs/source/index.md", new MockFileData(content) }
{ "docs/source/index.md", new MockFileData(string.IsNullOrEmpty(content) || content.StartsWith("---") ? content :
// language=markdown
$"""
---
title: Test Document
---

{content}
"""
)}
}, new MockFileSystemOptions
{
CurrentDirectory = Paths.Root.FullName
Expand Down
Loading