Skip to content

Commit 9df1ee8

Browse files
authored
Merge branch 'main' into add-automated-guide
2 parents dc7cbcd + 30e0d03 commit 9df1ee8

25 files changed

+741
-181
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public BuildContext(IFileSystem readFileSystem, IFileSystem writeFileSystem, str
4848

4949
var rootFolder = !string.IsNullOrWhiteSpace(source)
5050
? ReadFileSystem.DirectoryInfo.New(source)
51-
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName, "docs"));
51+
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName));
5252
SourcePath = FindDocsFolderFromRoot(rootFolder);
5353

5454
OutputPath = !string.IsNullOrWhiteSpace(output)

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ ILoggerFactory logger
5050
}
5151

5252

53-
public async Task ResolveDirectoryTree(Cancel ctx) =>
53+
public async Task ResolveDirectoryTree(Cancel ctx)
54+
{
55+
_logger.LogInformation("Resolving tree");
5456
await DocumentationSet.Tree.Resolve(ctx);
57+
_logger.LogInformation("Resolved tree");
58+
}
5559

5660
public async Task GenerateAll(Cancel ctx)
5761
{
@@ -62,11 +66,30 @@ public async Task GenerateAll(Cancel ctx)
6266
if (CompilationNotNeeded(generationState, out var offendingFiles, out var outputSeenChanges))
6367
return;
6468

65-
_logger.LogInformation("Resolving tree");
6669
await ResolveDirectoryTree(ctx);
67-
_logger.LogInformation("Resolved tree");
6870

71+
await ProcessDocumentationFiles(offendingFiles, outputSeenChanges, ctx);
72+
73+
await ExtractEmbeddedStaticResources(ctx);
74+
75+
76+
_logger.LogInformation($"Completing diagnostics channel");
77+
Context.Collector.Channel.TryComplete();
6978

79+
_logger.LogInformation($"Generating documentation compilation state");
80+
await GenerateDocumentationState(ctx);
81+
_logger.LogInformation($"Generating links.json");
82+
await GenerateLinkReference(ctx);
83+
84+
_logger.LogInformation($"Completing diagnostics channel");
85+
86+
await Context.Collector.StopAsync(ctx);
87+
88+
_logger.LogInformation($"Completed diagnostics channel");
89+
}
90+
91+
private async Task ProcessDocumentationFiles(HashSet<string> offendingFiles, DateTimeOffset outputSeenChanges, Cancel ctx)
92+
{
7093
var processedFileCount = 0;
7194
var exceptionCount = 0;
7295
_ = Context.Collector.StartAsync(ctx);
@@ -91,7 +114,10 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
91114
if (processedFiles % 100 == 0)
92115
_logger.LogInformation($"-> Handled {processedFiles} files");
93116
});
117+
}
94118

119+
private async Task ExtractEmbeddedStaticResources(Cancel ctx)
120+
{
95121
_logger.LogInformation($"Copying static files to output directory");
96122
var embeddedStaticFiles = Assembly.GetExecutingAssembly()
97123
.GetManifestResourceNames()
@@ -111,21 +137,6 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
111137
await resourceStream.CopyToAsync(stream, ctx);
112138
_logger.LogInformation($"Copied static embedded resource {path}");
113139
}
114-
115-
116-
_logger.LogInformation($"Completing diagnostics channel");
117-
Context.Collector.Channel.TryComplete();
118-
119-
_logger.LogInformation($"Generating documentation compilation state");
120-
await GenerateDocumentationState(ctx);
121-
_logger.LogInformation($"Generating links.json");
122-
await GenerateLinkReference(ctx);
123-
124-
_logger.LogInformation($"Completing diagnostics channel");
125-
126-
await Context.Collector.StopAsync(ctx);
127-
128-
_logger.LogInformation($"Completed diagnostics channel");
129140
}
130141

131142
private async Task ProcessFile(HashSet<string> offendingFiles, DocumentationFile file, DateTimeOffset outputSeenChanges, CancellationToken token)

src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,18 @@ public string? RepositoryName
3939
// manual read because libgit2sharp is not yet AOT ready
4040
public static GitCheckoutInformation Create(IFileSystem fileSystem)
4141
{
42-
// filesystem is not real so return a dummy
43-
var fakeRef = Guid.NewGuid().ToString().Substring(0, 16);
4442
if (fileSystem is not FileSystem)
4543
{
4644
return new GitCheckoutInformation
4745
{
48-
Branch = $"test-{fakeRef}",
46+
Branch = $"test-e35fcb27-5f60-4e",
4947
Remote = "elastic/docs-builder",
50-
Ref = fakeRef,
48+
Ref = "e35fcb27-5f60-4e",
5149
RepositoryName = "docs-builder"
5250
};
5351
}
5452

53+
var fakeRef = Guid.NewGuid().ToString()[..16];
5554
var gitConfig = Git(".git/config");
5655
if (!gitConfig.Exists)
5756
return Unavailable;

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public string? NavigationTitle
6363
private readonly Dictionary<string, PageTocItem> _tableOfContent = new(StringComparer.OrdinalIgnoreCase);
6464
public IReadOnlyDictionary<string, PageTocItem> TableOfContents => _tableOfContent;
6565

66-
private readonly HashSet<string> _additionalLabels = new(StringComparer.OrdinalIgnoreCase);
67-
public IReadOnlySet<string> AdditionalLabels => _additionalLabels;
66+
private readonly HashSet<string> _anchors = new(StringComparer.OrdinalIgnoreCase);
67+
public IReadOnlySet<string> Anchors => _anchors;
6868

6969
public string FilePath { get; }
7070
public string FileName { get; }
@@ -89,7 +89,7 @@ public MarkdownFile[] YieldParents()
8989
return parents.ToArray();
9090
}
9191

92-
public async Task<MarkdownDocument> MinimalParse(Cancel ctx)
92+
public async Task<MarkdownDocument> MinimalParseAsync(Cancel ctx)
9393
{
9494
var document = await MarkdownParser.MinimalParseAsync(SourceFile, ctx);
9595
ReadDocumentInstructions(document);
@@ -99,7 +99,7 @@ public async Task<MarkdownDocument> MinimalParse(Cancel ctx)
9999
public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
100100
{
101101
if (!_instructionsParsed)
102-
await MinimalParse(ctx);
102+
await MinimalParseAsync(ctx);
103103

104104
var document = await MarkdownParser.ParseAsync(SourceFile, YamlFrontMatter, ctx);
105105
return document;
@@ -171,22 +171,22 @@ private void ReadDocumentInstructions(MarkdownDocument document)
171171
Slug = (h.Item2 ?? h.Item1).Slugify()
172172
})
173173
.ToList();
174+
174175
_tableOfContent.Clear();
175176
foreach (var t in contents)
176177
_tableOfContent[t.Slug] = t;
177178

178-
var labels = document.Descendants<DirectiveBlock>()
179+
var anchors = document.Descendants<DirectiveBlock>()
179180
.Select(b => b.CrossReferenceName)
180181
.Where(l => !string.IsNullOrWhiteSpace(l))
181182
.Select(s => s.Slugify())
182183
.Concat(document.Descendants<InlineAnchor>().Select(a => a.Anchor))
184+
.Concat(_tableOfContent.Values.Select(t => t.Slug))
185+
.Where(anchor => !string.IsNullOrEmpty(anchor))
183186
.ToArray();
184187

185-
foreach (var label in labels)
186-
{
187-
if (!string.IsNullOrEmpty(label))
188-
_additionalLabels.Add(label);
189-
}
188+
foreach (var label in anchors)
189+
_anchors.Add(label);
190190

191191
_instructionsParsed = true;
192192
}

src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public async Task Resolve(Cancel ctx = default)
146146
if (_resolved)
147147
return;
148148

149-
await Parallel.ForEachAsync(FilesInOrder, ctx, async (file, token) => await file.MinimalParse(token));
149+
await Parallel.ForEachAsync(FilesInOrder, ctx, async (file, token) => await file.MinimalParseAsync(token));
150150
await Parallel.ForEachAsync(GroupsInOrder, ctx, async (group, token) => await group.Resolve(token));
151151

152-
await (Index?.MinimalParse(ctx) ?? Task.CompletedTask);
152+
await (Index?.MinimalParseAsync(ctx) ?? Task.CompletedTask);
153153

154154
_resolved = true;
155155
}

src/Elastic.Markdown/IO/State/LinkReference.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
namespace Elastic.Markdown.IO.State;
99

10+
public record LinkMetadata
11+
{
12+
[JsonPropertyName("anchors")]
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
14+
public required string[]? Anchors { get; init; } = [];
15+
}
16+
1017
public record LinkReference
1118
{
1219
[JsonPropertyName("origin")]
@@ -15,8 +22,9 @@ public record LinkReference
1522
[JsonPropertyName("url_path_prefix")]
1623
public required string? UrlPathPrefix { get; init; }
1724

25+
/// Mapping of relative filepath and all the page's anchors for deeplinks
1826
[JsonPropertyName("links")]
19-
public required string[] Links { get; init; } = [];
27+
public required Dictionary<string, LinkMetadata> Links { get; init; } = [];
2028

2129
[JsonPropertyName("cross_links")]
2230
public required string[] CrossLinks { get; init; } = [];
@@ -25,7 +33,12 @@ public static LinkReference Create(DocumentationSet set)
2533
{
2634
var crossLinks = set.Context.Collector.CrossLinks.ToHashSet().ToArray();
2735
var links = set.MarkdownFiles.Values
28-
.Select(m => m.RelativePath).ToArray();
36+
.Select(m => (m.RelativePath, m.Anchors))
37+
.ToDictionary(k => k.RelativePath, v =>
38+
{
39+
var anchors = v.Anchors.Count == 0 ? null : v.Anchors.ToArray();
40+
return new LinkMetadata { Anchors = anchors };
41+
});
2942
return new LinkReference
3043
{
3144
UrlPathPrefix = set.Context.UrlPathPrefix,

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class EnhancedCodeBlock(BlockParser parser, ParserContext context)
2020

2121
public int OpeningLength => Info?.Length ?? 0 + 3;
2222

23-
public List<CallOut>? CallOuts { get; set; }
23+
public List<CallOut> CallOuts { get; set; } = [];
2424

2525
public IReadOnlyCollection<CallOut> UniqueCallOuts => CallOuts?.DistinctBy(c => c.Index).ToList() ?? [];
2626

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static void RenderCallouts(HtmlRenderer renderer, EnhancedCodeBlock bloc
5555
{
5656
var callOuts = FindCallouts(block.CallOuts ?? [], lineNumber + 1);
5757
foreach (var callOut in callOuts)
58-
renderer.Write($"<span class=\"code-callout\">{callOut.Index}</span>");
58+
renderer.Write($"<span class=\"code-callout\" data-index=\"{callOut.Index}\">{callOut.Index}</span>");
5959
}
6060

6161
private static IEnumerable<CallOut> FindCallouts(

0 commit comments

Comments
 (0)