Skip to content

Commit 4ed014c

Browse files
authored
Warn of images outside to current toc scope when using image directive too (#1077)
1 parent 02d9244 commit 4ed014c

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,30 @@ public static void EmitWarning(this DiagnosticsCollector collector, IFileInfo fi
122122
collector.Channel.Write(d);
123123
}
124124

125-
public static void EmitError(this IBlockExtension block, string message, Exception? e = null)
126-
{
127-
if (block.SkipValidation)
128-
return;
125+
public static void EmitError(this IBlockExtension block, string message, Exception? e = null) => EmitDiagnostic(block, Severity.Error, message, e);
129126

130-
var d = new Diagnostic
131-
{
132-
Severity = Severity.Error,
133-
File = block.CurrentFile.FullName,
134-
Line = block.Line + 1,
135-
Column = block.Column,
136-
Length = block.OpeningLength + 5,
137-
Message = CreateExceptionMessage(message, e),
138-
};
139-
block.Build.Collector.Channel.Write(d);
140-
}
127+
public static void EmitWarning(this IBlockExtension block, string message) => EmitDiagnostic(block, Severity.Warning, message);
141128

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

143-
public static void EmitWarning(this IBlockExtension block, string message)
131+
private static void EmitDiagnostic(IBlockExtension block, Severity severity, string message, Exception? e = null)
144132
{
145133
if (block.SkipValidation)
146134
return;
147135

148136
var d = new Diagnostic
149137
{
150-
Severity = Severity.Warning,
138+
Severity = severity,
151139
File = block.CurrentFile.FullName,
152140
Line = block.Line + 1,
153141
Column = block.Column,
154-
Length = block.OpeningLength + 4,
155-
Message = message
142+
Length = block.OpeningLength + 5,
143+
Message = CreateExceptionMessage(message, e),
156144
};
157145
block.Build.Collector.Channel.Write(d);
158146
}
159147

148+
160149
private static void LinkDiagnostic(InlineProcessor processor, Severity severity, Inline inline, int length, string message, Exception? e = null)
161150
{
162151
var line = inline.Line + 1;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using Elastic.Markdown.Diagnostics;
6+
using Elastic.Markdown.IO;
67

78
namespace Elastic.Markdown.Myst.Directives;
89

@@ -101,10 +102,17 @@ private void ExtractImageUrl(ParserContext context)
101102

102103
ImageUrl = imageUrl;
103104
var imagePath = Path.Combine(includeFrom, imageUrl.TrimStart('/'));
104-
if (context.Build.ReadFileSystem.File.Exists(imagePath))
105+
var file = context.Build.ReadFileSystem.FileInfo.New(imagePath);
106+
if (file.Exists)
105107
Found = true;
106108
else
107109
this.EmitError($"`{imageUrl}` does not exist. resolved to `{imagePath}");
110+
111+
if (context.DocumentationFileLookup(context.MarkdownSourcePath) is MarkdownFile currentMarkdown)
112+
{
113+
if (!file.Directory!.FullName.StartsWith(currentMarkdown.ScopeDirectory.FullName + Path.DirectorySeparatorChar))
114+
this.EmitHint($"Image '{imageUrl}' is referenced out of table of contents scope '{currentMarkdown.ScopeDirectory}'.");
115+
}
108116
}
109117
}
110118

0 commit comments

Comments
 (0)