Skip to content

Commit 413d380

Browse files
authored
Improve safety of callout handling in EnhancedCodeBlockParser. (#455)
Adjusted logic to ensure stricter detection of classic callouts by checking for a closing angle bracket at the end of the span. Also updated regex generation in CallOutParser to use a timeout for better performance and stability.
1 parent b6fba64 commit 413d380

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public async Task GenerateAll(Cancel ctx)
7272

7373
await ExtractEmbeddedStaticResources(ctx);
7474

75-
7675
_logger.LogInformation($"Completing diagnostics channel");
7776
Context.Collector.Channel.TryComplete();
7877

@@ -92,6 +91,7 @@ private async Task ProcessDocumentationFiles(HashSet<string> offendingFiles, Dat
9291
{
9392
var processedFileCount = 0;
9493
var exceptionCount = 0;
94+
var totalFileCount = DocumentationSet.Files.Count;
9595
_ = Context.Collector.StartAsync(ctx);
9696
await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
9797
{
@@ -112,8 +112,9 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
112112
}
113113

114114
if (processedFiles % 100 == 0)
115-
_logger.LogInformation($"-> Handled {processedFiles} files");
115+
_logger.LogInformation($"-> Processed {processedFiles}/{totalFileCount} files");
116116
});
117+
_logger.LogInformation($"-> Processed {processedFileCount}/{totalFileCount} files");
117118
}
118119

119120
private async Task ExtractEmbeddedStaticResources(Cancel ctx)
@@ -149,7 +150,7 @@ private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile
149150
return;
150151
}
151152

152-
_logger.LogTrace($"{file.SourceFile.FullName}");
153+
_logger.LogTrace($"--> {file.SourceFile.FullName}");
153154
var outputFile = OutputFile(file.RelativePath);
154155
if (file is MarkdownFile markdown)
155156
await HtmlWriter.WriteAsync(outputFile, markdown, token);

src/Elastic.Markdown/Myst/CodeBlocks/CallOutParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Elastic.Markdown.Myst.CodeBlocks;
88

99
public static partial class CallOutParser
1010
{
11-
[GeneratedRegex(@"^.+\S+.*?\s<\d+>$", RegexOptions.IgnoreCase, "en-US")]
11+
[GeneratedRegex(@"^.+\S+.*?\s<\d+>$", RegexOptions.IgnoreCase, 2000, "en-US")]
1212
public static partial Regex CallOutNumber();
1313

14-
[GeneratedRegex(@"^.+\S+.*?\s(?:\/\/|#)\s[^""\/#]+$", RegexOptions.IgnoreCase, "en-US")]
14+
[GeneratedRegex(@"^.+\S+.*?\s(?:\/\/|#)\s[^""\/#]+$", RegexOptions.IgnoreCase, 2000, "en-US")]
1515
public static partial Regex MathInlineAnnotation();
1616
}

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public override bool Close(BlockProcessor processor, Block block)
119119
continue;
120120

121121
List<CallOut> callOuts = [];
122-
var hasClassicCallout = span.IndexOf("<") > 0;
122+
var hasClassicCallout = span.IndexOf("<") > 0 && span.LastIndexOf(">") == span.Length - 1;
123123
if (hasClassicCallout)
124124
{
125125
var matchClassicCallout = CallOutParser.CallOutNumber().EnumerateMatches(span);

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public HtmlWriter(DocumentationSet documentationSet, IFileSystem writeFileSystem
2828
public ILoggerFactory LoggerFactory { get; }
2929
public ServiceProvider ServiceProvider { get; }
3030

31+
private Task<string> RenderEmptyString(MarkdownFile markdown, Cancel ctx = default) =>
32+
Task.FromResult(string.Empty);
33+
3134
private async Task<string> RenderNavigation(MarkdownFile markdown, Cancel ctx = default)
3235
{
3336
var slice = Layout._TocTree.Create(new NavigationViewModel

0 commit comments

Comments
 (0)