Skip to content

Commit 6e5c3f7

Browse files
committed
Add ability to serve LLM markdown during local development
1 parent a95d0b2 commit 6e5c3f7

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Elastic.Markdown.Exporters;
1616
using Elastic.Markdown.IO;
1717
using Elastic.Markdown.Links.CrossLinks;
18+
using Elastic.Markdown.Myst.Renderers;
19+
using Elastic.Markdown.Myst.Renderers.LlmMarkdown;
1820
using Markdig.Syntax;
1921
using Microsoft.Extensions.Logging;
2022

@@ -340,6 +342,19 @@ private async Task GenerateDocumentationState(Cancel ctx)
340342
await DocumentationSet.OutputDirectory.FileSystem.File.WriteAllBytesAsync(stateFile.FullName, bytes, ctx);
341343
}
342344

345+
public async Task<string> RenderLlmMarkdown(MarkdownFile markdown, Cancel ctx)
346+
{
347+
await DocumentationSet.Tree.Resolve(ctx);
348+
var document = await markdown.ParseFullAsync(ctx);
349+
await using var writer = new StringWriter();
350+
var renderer = new LlmMarkdownRenderer(writer)
351+
{
352+
BuildContext = DocumentationSet.Context
353+
};
354+
_ = renderer.Render(document);
355+
return writer.ToString().Trim();
356+
}
357+
343358
public async Task<RenderResult> RenderLayout(MarkdownFile markdown, Cancel ctx)
344359
{
345360
await DocumentationSet.Tree.Resolve(ctx);

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Elastic.Documentation.Site.Navigation;
1212
using Elastic.Markdown.Extensions.DetectionRules;
1313
using Elastic.Markdown.IO;
14+
using Elastic.Markdown.Myst.Renderers;
15+
using Elastic.Markdown.Myst.Renderers.LlmMarkdown;
1416
using Elastic.Markdown.Page;
1517
using Markdig.Syntax;
1618
using RazorSlices;

src/tooling/docs-builder/Http/DocumentationWebHost.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
using Elastic.Documentation.Configuration.Versions;
1111
using Elastic.Documentation.Site.FileProviders;
1212
using Elastic.Documentation.Tooling;
13+
using Elastic.Markdown.Exporters;
1314
using Elastic.Markdown.IO;
15+
using Elastic.Markdown.Myst.Renderers;
16+
using Markdig.Syntax;
1417
using Microsoft.AspNetCore.Builder;
1518
using Microsoft.AspNetCore.Hosting;
1619
using Microsoft.AspNetCore.Http;
@@ -187,6 +190,17 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
187190
{
188191
var generator = holder.Generator;
189192
const string navPartialSuffix = ".nav.html";
193+
194+
// Check if the original request is asking for LLM-rendered markdown
195+
var requestLlmMarkdown = slug.EndsWith(".md");
196+
var originalSlug = slug;
197+
198+
// If requesting .md output, remove the .md extension to find the source file
199+
if (requestLlmMarkdown)
200+
{
201+
slug = slug[..^3]; // Remove ".md" extension
202+
}
203+
190204
if (slug.EndsWith(navPartialSuffix))
191205
{
192206
var segments = slug.Split("/");
@@ -218,8 +232,18 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
218232
switch (documentationFile)
219233
{
220234
case MarkdownFile markdown:
221-
var rendered = await generator.RenderLayout(markdown, ctx);
222-
return Results.Content(rendered.Html, "text/html");
235+
if (requestLlmMarkdown)
236+
{
237+
// Render using LLM pipeline for CommonMark output
238+
var llmRendered = await generator.RenderLlmMarkdown(markdown, ctx);
239+
return Results.Content(llmRendered, "text/markdown; charset=utf-8");
240+
}
241+
else
242+
{
243+
// Regular HTML rendering
244+
var rendered = await generator.RenderLayout(markdown, ctx);
245+
return Results.Content(rendered.Html, "text/html");
246+
}
223247

224248
case ImageFile image:
225249
return Results.File(image.SourceFile.FullName, image.MimeType);

0 commit comments

Comments
 (0)