Skip to content

Warn of images outside to current toc scope when using image directive too #1077

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
Apr 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,30 @@ public static void EmitWarning(this DiagnosticsCollector collector, IFileInfo fi
collector.Channel.Write(d);
}

public static void EmitError(this IBlockExtension block, string message, Exception? e = null)
{
if (block.SkipValidation)
return;
public static void EmitError(this IBlockExtension block, string message, Exception? e = null) => EmitDiagnostic(block, Severity.Error, message, e);

var d = new Diagnostic
{
Severity = Severity.Error,
File = block.CurrentFile.FullName,
Line = block.Line + 1,
Column = block.Column,
Length = block.OpeningLength + 5,
Message = CreateExceptionMessage(message, e),
};
block.Build.Collector.Channel.Write(d);
}
public static void EmitWarning(this IBlockExtension block, string message) => EmitDiagnostic(block, Severity.Warning, message);

public static void EmitHint(this IBlockExtension block, string message) => EmitDiagnostic(block, Severity.Hint, message);

public static void EmitWarning(this IBlockExtension block, string message)
private static void EmitDiagnostic(IBlockExtension block, Severity severity, string message, Exception? e = null)
{
if (block.SkipValidation)
return;

var d = new Diagnostic
{
Severity = Severity.Warning,
Severity = severity,
File = block.CurrentFile.FullName,
Line = block.Line + 1,
Column = block.Column,
Length = block.OpeningLength + 4,
Message = message
Length = block.OpeningLength + 5,
Message = CreateExceptionMessage(message, e),
};
block.Build.Collector.Channel.Write(d);
}


private static void LinkDiagnostic(InlineProcessor processor, Severity severity, Inline inline, int length, string message, Exception? e = null)
{
var line = inline.Line + 1;
Expand Down
10 changes: 9 additions & 1 deletion src/Elastic.Markdown/Myst/Directives/ImageBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO;

namespace Elastic.Markdown.Myst.Directives;

Expand Down Expand Up @@ -101,10 +102,17 @@ private void ExtractImageUrl(ParserContext context)

ImageUrl = imageUrl;
var imagePath = Path.Combine(includeFrom, imageUrl.TrimStart('/'));
if (context.Build.ReadFileSystem.File.Exists(imagePath))
var file = context.Build.ReadFileSystem.FileInfo.New(imagePath);
if (file.Exists)
Found = true;
else
this.EmitError($"`{imageUrl}` does not exist. resolved to `{imagePath}");

if (context.DocumentationFileLookup(context.MarkdownSourcePath) is MarkdownFile currentMarkdown)
{
if (!file.Directory!.FullName.StartsWith(currentMarkdown.ScopeDirectory.FullName + Path.DirectorySeparatorChar))
this.EmitHint($"Image '{imageUrl}' is referenced out of table of contents scope '{currentMarkdown.ScopeDirectory}'.");
}
}
}

Expand Down
Loading