Skip to content

Commit

Permalink
Improve safety of callout handling in EnhancedCodeBlockParser. (#455)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mpdreamz authored Feb 10, 2025
1 parent b6fba64 commit 413d380
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public async Task GenerateAll(Cancel ctx)

await ExtractEmbeddedStaticResources(ctx);


_logger.LogInformation($"Completing diagnostics channel");
Context.Collector.Channel.TryComplete();

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

if (processedFiles % 100 == 0)
_logger.LogInformation($"-> Handled {processedFiles} files");
_logger.LogInformation($"-> Processed {processedFiles}/{totalFileCount} files");
});
_logger.LogInformation($"-> Processed {processedFileCount}/{totalFileCount} files");
}

private async Task ExtractEmbeddedStaticResources(Cancel ctx)
Expand Down Expand Up @@ -149,7 +150,7 @@ private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile
return;
}

_logger.LogTrace($"{file.SourceFile.FullName}");
_logger.LogTrace($"--> {file.SourceFile.FullName}");
var outputFile = OutputFile(file.RelativePath);
if (file is MarkdownFile markdown)
await HtmlWriter.WriteAsync(outputFile, markdown, token);
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/Myst/CodeBlocks/CallOutParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Elastic.Markdown.Myst.CodeBlocks;

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

[GeneratedRegex(@"^.+\S+.*?\s(?:\/\/|#)\s[^""\/#]+$", RegexOptions.IgnoreCase, "en-US")]
[GeneratedRegex(@"^.+\S+.*?\s(?:\/\/|#)\s[^""\/#]+$", RegexOptions.IgnoreCase, 2000, "en-US")]
public static partial Regex MathInlineAnnotation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public override bool Close(BlockProcessor processor, Block block)
continue;

List<CallOut> callOuts = [];
var hasClassicCallout = span.IndexOf("<") > 0;
var hasClassicCallout = span.IndexOf("<") > 0 && span.LastIndexOf(">") == span.Length - 1;
if (hasClassicCallout)
{
var matchClassicCallout = CallOutParser.CallOutNumber().EnumerateMatches(span);
Expand Down
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public HtmlWriter(DocumentationSet documentationSet, IFileSystem writeFileSystem
public ILoggerFactory LoggerFactory { get; }
public ServiceProvider ServiceProvider { get; }

private Task<string> RenderEmptyString(MarkdownFile markdown, Cancel ctx = default) =>
Task.FromResult(string.Empty);

private async Task<string> RenderNavigation(MarkdownFile markdown, Cancel ctx = default)
{
var slice = Layout._TocTree.Create(new NavigationViewModel
Expand Down

0 comments on commit 413d380

Please sign in to comment.