Skip to content

Commit b08b4cf

Browse files
Warn when a heading contains an inline applies_to (#1777)
* warn when heading contains inline applies_to * update emit warning to use block processor to have sane defaults for peeking into the file for error localization --------- Co-authored-by: Martijn Laarman <Mpdreamz@gmail.com>
1 parent f27f4a3 commit b08b4cf

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,36 @@ public static class ProcessorDiagnosticExtensions
1414
{
1515
private static string CreateExceptionMessage(string message, Exception? e) => message + (e != null ? Environment.NewLine + e : string.Empty);
1616

17+
public static void EmitError(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
18+
processor.Emit(Severity.Error, message, line, column, length);
19+
20+
public static void EmitWarning(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
21+
processor.Emit(Severity.Warning, message, line, column, length);
22+
23+
public static void EmitHint(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
24+
processor.Emit(Severity.Hint, message, line, column, length);
25+
26+
public static void Emit(this BlockProcessor processor, Severity severity, string message, int? line = null, int? column = null, int? length = null)
27+
{
28+
var context = processor.GetContext();
29+
if (context.SkipValidation)
30+
return;
31+
var d = new Diagnostic
32+
{
33+
Severity = severity,
34+
File = processor.GetContext().MarkdownSourcePath.FullName,
35+
Column = column ?? 1,
36+
Line = line ?? processor.LineIndex + 1,
37+
Message = message,
38+
Length = length ?? processor.Line.Length + 1
39+
};
40+
context.Build.Collector.Write(d);
41+
}
42+
43+
1744
public static void EmitError(this InlineProcessor processor, int line, int column, int length, string message) =>
1845
processor.Emit(Severity.Error, line, column, length, message);
1946

20-
2147
public static void EmitWarning(this InlineProcessor processor, int line, int column, int length, string message) =>
2248
processor.Emit(Severity.Warning, line, column, length, message);
2349

src/Elastic.Markdown/Myst/InlineParsers/HeadingBlockWithSlugParser.cs

Lines changed: 12 additions & 0 deletions
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 System.Text.RegularExpressions;
6+
using Elastic.Markdown.Diagnostics;
67
using Elastic.Markdown.Myst.Roles.Icons;
78
using Markdig;
89
using Markdig.Helpers;
@@ -32,13 +33,18 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
3233
public class HeadingBlockWithSlugParser : HeadingBlockParser
3334
{
3435
private static readonly Regex IconSyntax = IconParser.IconRegex();
36+
private static readonly Regex AppliesToSyntax = HeadingAppliesToParser.AppliesToSyntaxRegex();
3537

3638
public override bool Close(BlockProcessor processor, Block block)
3739
{
3840
if (block is not HeadingBlock headingBlock)
3941
return base.Close(processor, block);
4042

4143
var text = headingBlock.Lines.Lines[0].Slice.AsSpan();
44+
45+
if (AppliesToSyntax.IsMatch(text))
46+
processor.EmitWarning("Do not use inline 'applies_to' annotations with headings. Use a section 'applies_to' annotation instead.");
47+
4248
// Remove icon syntax from the heading text
4349
var cleanText = IconSyntax.Replace(text.ToString(), "").Trim();
4450
headingBlock.SetData("header", cleanText);
@@ -79,3 +85,9 @@ public static partial class HeadingAnchorParser
7985
[GeneratedRegex(@"\$\$\$[^\$]+\$\$\$", RegexOptions.IgnoreCase, "en-US")]
8086
public static partial Regex InlineAnchors();
8187
}
88+
89+
public static partial class HeadingAppliesToParser
90+
{
91+
[GeneratedRegex(@"\{applies_to\}`[^`]*`", RegexOptions.Compiled)]
92+
public static partial Regex AppliesToSyntaxRegex();
93+
}

0 commit comments

Comments
 (0)