88using System . Text ;
99using Elastic . Documentation . Configuration ;
1010using Elastic . Documentation . Configuration . Builder ;
11+ using Elastic . Markdown . Helpers ;
12+ using Elastic . Markdown . Myst ;
1113using Elastic . Markdown . Myst . Renderers ;
1214using Markdig . Syntax ;
1315
@@ -49,26 +51,27 @@ await fileContext.SourceFile.SourceFile.FileSystem.File.WriteAllTextAsync(
4951 return true ;
5052 }
5153
52- private string ConvertToLlmMarkdown ( MarkdownDocument document , MarkdownExportFileContext context )
54+ public static string ConvertToLlmMarkdown ( MarkdownDocument document , MarkdownExportFileContext context )
5355 {
5456 using var writer = new StringWriter ( ) ;
55-
56- // Create a new renderer for consistent LLM output with BuildContext for URL transformation
57+ var state = new ParserState ( context . BuildContext )
58+ {
59+ YamlFrontMatter = context . SourceFile . YamlFrontMatter ,
60+ MarkdownSourcePath = context . SourceFile . SourceFile ,
61+ CrossLinkResolver = context . Resolvers . CrossLinkResolver ,
62+ DocumentationFileLookup = context . Resolvers . DocumentationFileLookup
63+ } ;
64+ var parserContext = new ParserContext ( state ) ;
5765 var renderer = new LlmMarkdownRenderer ( writer )
5866 {
5967 BuildContext = context . BuildContext
6068 } ;
61-
6269 _ = renderer . Render ( document ) ;
6370 var content = writer . ToString ( ) ;
64-
65- // Apply substitutions to the final content
66- content = ApplySubstitutions ( content , context ) ;
67-
6871 return content ;
6972 }
7073
71- private IFileInfo GetLlmOutputFile ( MarkdownExportFileContext fileContext )
74+ private static IFileInfo GetLlmOutputFile ( MarkdownExportFileContext fileContext )
7275 {
7376 var source = fileContext . SourceFile . SourceFile ;
7477 var fs = source . FileSystem ;
@@ -105,93 +108,37 @@ private IFileInfo GetLlmOutputFile(MarkdownExportFileContext fileContext)
105108 }
106109 }
107110
108- private string ApplySubstitutions ( string content , MarkdownExportFileContext context )
109- {
110- // Get combined substitutions (global + file-specific)
111- var substitutions = GetCombinedSubstitutions ( context ) ;
112-
113- // Process substitutions in the content
114- foreach ( var ( key , value ) in substitutions )
115- {
116- // Replace {{key}} with value
117- content = content . Replace ( $ "{{{{{key}}}}}", value ) ;
118- }
119-
120- return content ;
121- }
122-
123- private ConcurrentDictionary < string , string > GetCombinedSubstitutions ( MarkdownExportFileContext context )
124- {
125- // Get global substitutions from BuildContext
126- var globalSubstitutions = context . BuildContext . Configuration . Substitutions ;
127-
128- // Get file-specific substitutions from YamlFrontMatter
129- var fileSubstitutions = context . SourceFile . YamlFrontMatter ? . Properties ;
130-
131- // Create a new dictionary with all substitutions
132- var allSubstitutions = new ConcurrentDictionary < string , string > ( ) ;
133-
134- // Add file-specific substitutions first
135- if ( fileSubstitutions != null )
136- {
137- foreach ( var ( key , value ) in fileSubstitutions )
138- {
139- _ = allSubstitutions . TryAdd ( key , value ) ;
140- }
141- }
142-
143- // Add global substitutions (will override file-specific ones if there are conflicts)
144- foreach ( var ( key , value ) in globalSubstitutions )
145- {
146- _ = allSubstitutions . TryAdd ( key , value ) ;
147- }
148-
149- return allSubstitutions ;
150- }
151111
152112 private string CreateLlmContentWithMetadata ( MarkdownExportFileContext context , string llmMarkdown )
153113 {
154114 var sourceFile = context . SourceFile ;
155115 var metadata = new StringBuilder ( ) ;
156116
157- // Add metadata header
158- // _ = metadata.AppendLine("<!-- LLM-Optimized Markdown Document -->");
159117 _ = metadata . AppendLine ( "---" ) ;
160- // _ = metadata.AppendLine($"<!-- Source: {Path.GetRelativePath(context.BuildContext.DocumentationOutputDirectory.FullName, sourceFile.SourceFile.FullName)} -->");
161- // _ = metadata.AppendLine($"<!-- Generated: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC -->");
162118 _ = metadata . AppendLine ( $ "title: { sourceFile . Title } ") ;
163119
164120 if ( ! string . IsNullOrEmpty ( sourceFile . Url ) )
165- {
166121 _ = metadata . AppendLine ( $ "url: { context . BuildContext . CanonicalBaseUrl ? . Scheme } ://{ context . BuildContext . CanonicalBaseUrl ? . Host } { sourceFile . Url } ") ;
167- }
168122
169123 if ( ! string . IsNullOrEmpty ( sourceFile . YamlFrontMatter ? . Description ) )
170- {
171124 _ = metadata . AppendLine ( $ "description: { sourceFile . YamlFrontMatter . Description } ") ;
172- }
173125 else
174126 {
175127 var descriptionGenerator = new DescriptionGenerator ( ) ;
176128 var generateDescription = descriptionGenerator . GenerateDescription ( context . Document ) ;
177129 _ = metadata . AppendLine ( $ "description: { generateDescription } ") ;
178130 }
179-
180-
181131 var configProducts = context . BuildContext . Configuration . Products . Select ( p =>
182132 {
183133 if ( Products . AllById . TryGetValue ( p , out var product ) )
184134 return product ;
185135 throw new ArgumentException ( $ "Invalid product id: { p } ") ;
186136 } ) ;
187-
188137 var frontMatterProducts = sourceFile . YamlFrontMatter ? . Products ?? [ ] ;
189-
190138 var allProducts = frontMatterProducts
191139 . Union ( configProducts )
192140 . Distinct ( )
193141 . ToList ( ) ;
194-
195142 if ( allProducts . Count > 0 )
196143 {
197144 _ = metadata . AppendLine ( "products:" ) ;
@@ -200,14 +147,8 @@ private string CreateLlmContentWithMetadata(MarkdownExportFileContext context, s
200147 }
201148
202149 _ = metadata . AppendLine ( "---" ) ;
203-
204- // Add an empty line after metadata
205150 _ = metadata . AppendLine ( ) ;
206-
207- // Add the title as H1 heading
208151 _ = metadata . AppendLine ( $ "# { sourceFile . Title } ") ;
209-
210- // Add the converted markdown content
211152 _ = metadata . Append ( llmMarkdown ) ;
212153
213154 return metadata . ToString ( ) ;
0 commit comments