Skip to content

Commit 72e0dd3

Browse files
committed
LLMText exports resolves includes and substitutions
1 parent 26e6343 commit 72e0dd3

File tree

14 files changed

+172
-38
lines changed

14 files changed

+172
-38
lines changed

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile
245245
foreach (var exporter in _markdownExporters)
246246
{
247247
var document = context.MarkdownDocument ??= await markdown.ParseFullAsync(ctx);
248-
_ = await exporter.ExportAsync(new MarkdownExportContext { Document = document, File = markdown }, ctx);
248+
_ = await exporter.ExportAsync(new MarkdownExportContext
249+
{
250+
BuildContext = Context,
251+
Resolvers = DocumentationSet.MarkdownParser.Resolvers,
252+
Document = document,
253+
File = markdown
254+
}, ctx);
249255
}
250256
}
251257
}

src/Elastic.Markdown/Exporters/IMarkdownExporter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Documentation.Configuration;
56
using Elastic.Markdown.IO;
7+
using Elastic.Markdown.Myst;
68
using Markdig.Syntax;
79

810
namespace Elastic.Markdown.Exporters;
911

1012
public class MarkdownExportContext
1113
{
14+
public required BuildContext BuildContext { get; init; }
15+
public required IParserResolvers Resolvers { get; init; }
1216
public required MarkdownDocument Document { get; init; }
1317
public required MarkdownFile File { get; init; }
1418
public string? LLMText { get; set; }
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Buffers;
6+
using System.IO.Abstractions;
7+
using System.Text;
8+
using Elastic.Documentation.Configuration;
9+
using Elastic.Markdown.Helpers;
10+
using Elastic.Markdown.Myst;
11+
using Elastic.Markdown.Myst.FrontMatter;
12+
using Markdig.Syntax;
13+
14+
namespace Elastic.Markdown.Exporters;
15+
16+
public class LLMTextExporter : IMarkdownExporter
17+
{
18+
public ValueTask StartAsync(CancellationToken ctx = default) => ValueTask.CompletedTask;
19+
20+
public ValueTask StopAsync(CancellationToken ctx = default) => ValueTask.CompletedTask;
21+
22+
public ValueTask<bool> ExportAsync(MarkdownExportContext context, CancellationToken ctx)
23+
{
24+
var source = context.File.SourceFile;
25+
var llmText = ToLLMText(context.BuildContext, context.File.YamlFrontMatter, context.Resolvers, source);
26+
var fs = source.FileSystem;
27+
28+
//var llmText = context.LLMText ??= LLMRoundtripRenderer.ToLLMText(context.Document);
29+
return ValueTask.FromResult(true);
30+
}
31+
32+
33+
public static string ToLLMText(BuildContext buildContext, YamlFrontMatter? frontMatter, IParserResolvers resolvers, IFileInfo source)
34+
{
35+
var fs = source.FileSystem;
36+
var sb = DocumentationObjectPoolProvider.StringBuilderPool.Get();
37+
38+
Read(source, fs, sb);
39+
var full = sb.ToString();
40+
var state = new ParserState(buildContext)
41+
{
42+
YamlFrontMatter = frontMatter,
43+
MarkdownSourcePath = source,
44+
CrossLinkResolver = resolvers.CrossLinkResolver,
45+
DocumentationFileLookup = resolvers.DocumentationFileLookup
46+
};
47+
DocumentationObjectPoolProvider.StringBuilderPool.Return(sb);
48+
var replaced = full.ReplaceSubstitutions(new ParserContext(state));
49+
return replaced;
50+
51+
}
52+
53+
private static void Read(IFileInfo source, IFileSystem fs, StringBuilder sb, int depth = 0)
54+
{
55+
var text = fs.File.ReadAllText(source.FullName).AsSpan();
56+
var spanStart = ":::{include}".AsSpan();
57+
var include = SearchValues.Create([spanStart.ToString(), ":::\n"], StringComparison.OrdinalIgnoreCase);
58+
int i;
59+
var startIndex = 0;
60+
while ((i = text[startIndex..].IndexOfAny(include)) >= 0)
61+
{
62+
var cursor = startIndex + i;
63+
var marker = text[cursor..];
64+
if (marker.StartsWith(spanStart))
65+
{
66+
_ = sb.Append(text.Slice(startIndex, i).TrimEnd('\n'));
67+
var relativeFileStart = marker.IndexOf('}') + 1;
68+
var relativeFileEnd = marker.IndexOf('\n');
69+
var relativeFile = marker[relativeFileStart..relativeFileEnd].Trim();
70+
var includePath = Path.GetFullPath(Path.Combine(source.Directory!.FullName, relativeFile.ToString()));
71+
var includeSource = fs.FileInfo.New(includePath);
72+
Read(includeSource, fs, sb, depth + 1);
73+
startIndex = cursor + relativeFileEnd;
74+
startIndex = Math.Min(text.Length, startIndex);
75+
}
76+
else
77+
{
78+
startIndex += i + 4;
79+
startIndex = Math.Min(text.Length, startIndex);
80+
}
81+
}
82+
_ = sb.Append(text[startIndex..]);
83+
}
84+
}
85+

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,6 @@ public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
185185
return document;
186186
}
187187

188-
public static string ToLLMText(MarkdownDocument document)
189-
{
190-
using var sw = new StringWriter();
191-
var rr = new RoundtripRenderer(sw);
192-
rr.Write(document);
193-
var outputMarkdown = sw.ToString();
194-
195-
return outputMarkdown;
196-
197-
}
198-
199188
private IReadOnlyDictionary<string, string> GetSubstitutions()
200189
{
201190
var globalSubstitutions = _globalSubstitutions;

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Elastic.Markdown.Myst.Directives.Tabs;
1616
using Elastic.Markdown.Myst.Directives.Version;
1717
using Elastic.Markdown.Myst.InlineParsers.Substitution;
18+
using Elastic.Markdown.Myst.Roles;
1819
using Markdig;
1920
using Markdig.Renderers;
2021
using Markdig.Renderers.Html;

src/Elastic.Markdown/Myst/Directives/DirectiveMarkdownExtension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using Elastic.Markdown.Myst.Renderers;
6+
using Elastic.Markdown.Myst.Roles;
67
using Markdig;
78
using Markdig.Extensions.Tables;
89
using Markdig.Parsers;

src/Elastic.Markdown/Myst/MarkdownParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Elastic.Markdown.Myst;
2525
public class MarkdownParser(BuildContext build, IParserResolvers resolvers)
2626
{
2727
private BuildContext Build { get; } = build;
28-
private IParserResolvers Resolvers { get; } = resolvers;
28+
public IParserResolvers Resolvers { get; } = resolvers;
2929

3030
public Task<MarkdownDocument> ParseAsync(IFileInfo path, YamlFrontMatter? matter, Cancel ctx) =>
3131
ParseFromFile(path, matter, Pipeline, false, ctx);

src/Elastic.Markdown/Myst/Directives/Role.cs renamed to src/Elastic.Markdown/Myst/Roles/Role.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using Markdig.Syntax.Inlines;
66

7-
namespace Elastic.Markdown.Myst.Directives;
7+
namespace Elastic.Markdown.Myst.Roles;
88

99

1010
//TODO evaluate if we need this

src/tooling/docs-assembler/Exporters/ElasticsearchMarkdownExporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public async ValueTask<bool> ExportAsync(MarkdownExportContext context, Cancel c
120120
if (url.Contains("/reference/integrations"))
121121
return true;
122122

123-
var body = context.LLMText ??= MarkdownFile.ToLLMText(document);
123+
// TODO!
124+
var body = context.LLMText ??= "string.Empty";
124125
var doc = new DocumentationDocument
125126
{
126127
Title = file.Title,

src/tooling/docs-assembler/Exporters/LLMTextExporter.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)