Skip to content

Commit b647f62

Browse files
committed
Fix rendering
1 parent 7897b6b commit b647f62

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/Elastic.Markdown/Myst/MarkdownParser.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO.Abstractions;
66
using Cysharp.IO;
77
using Elastic.Documentation.Configuration;
8+
using Elastic.Markdown.Helpers;
89
using Elastic.Markdown.Myst.CodeBlocks;
910
using Elastic.Markdown.Myst.Comments;
1011
using Elastic.Markdown.Myst.Directives;
@@ -23,7 +24,7 @@
2324

2425
namespace Elastic.Markdown.Myst;
2526

26-
public class MarkdownParser(BuildContext build, IParserResolvers resolvers)
27+
public partial class MarkdownParser(BuildContext build, IParserResolvers resolvers)
2728
{
2829
private BuildContext Build { get; } = build;
2930
public IParserResolvers Resolvers { get; } = resolvers;
@@ -69,7 +70,11 @@ public static MarkdownDocument ParseMarkdownStringAsync(BuildContext build, IPar
6970
CrossLinkResolver = resolvers.CrossLinkResolver
7071
};
7172
var context = new ParserContext(state);
72-
var markdownDocument = Markdig.Markdown.Parse(markdown, pipeline, context);
73+
74+
// Preprocess substitutions in link patterns before Markdig parsing
75+
var preprocessedMarkdown = PreprocessLinkSubstitutions(markdown, context);
76+
77+
var markdownDocument = Markdig.Markdown.Parse(preprocessedMarkdown, pipeline, context);
7378
return markdownDocument;
7479
}
7580

@@ -105,7 +110,10 @@ private static async Task<MarkdownDocument> ParseAsync(
105110
else
106111
inputMarkdown = await path.FileSystem.File.ReadAllTextAsync(path.FullName, ctx);
107112

108-
var markdownDocument = Markdig.Markdown.Parse(inputMarkdown, pipeline, context);
113+
// Preprocess substitutions in link patterns before Markdig parsing
114+
var preprocessedMarkdown = PreprocessLinkSubstitutions(inputMarkdown, (ParserContext)context);
115+
116+
var markdownDocument = Markdig.Markdown.Parse(preprocessedMarkdown, pipeline, context);
109117
return markdownDocument;
110118
}
111119

@@ -166,4 +174,30 @@ public static MarkdownPipeline Pipeline
166174
return PipelineCached;
167175
}
168176
}
177+
178+
[System.Text.RegularExpressions.GeneratedRegex(@"\[([^\]]+)\]\(([^\)]+)\)", System.Text.RegularExpressions.RegexOptions.Multiline)]
179+
private static partial System.Text.RegularExpressions.Regex LinkPattern();
180+
181+
/// <summary>
182+
/// Preprocesses substitutions specifically in link patterns [text](url) before Markdig parsing
183+
/// </summary>
184+
private static string PreprocessLinkSubstitutions(string markdown, ParserContext context) =>
185+
LinkPattern().Replace(markdown, match =>
186+
{
187+
var linkText = match.Groups[1].Value;
188+
var linkUrl = match.Groups[2].Value;
189+
190+
// Only preprocess external links to preserve internal link validation behavior
191+
// Check if URL contains substitutions and looks like it might resolve to an external URL
192+
if (linkUrl.Contains("{{") && (linkUrl.Contains("http") || linkText.Contains("{{")))
193+
{
194+
// Apply substitutions to both link text and URL
195+
var processedText = linkText.ReplaceSubstitutions(context);
196+
var processedUrl = linkUrl.ReplaceSubstitutions(context);
197+
return $"[{processedText}]({processedUrl})";
198+
}
199+
200+
// Return original match for internal links
201+
return match.Value;
202+
});
169203
}

0 commit comments

Comments
 (0)