-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathMarkdownFile.cs
More file actions
470 lines (400 loc) · 14.7 KB
/
MarkdownFile.cs
File metadata and controls
470 lines (400 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Configuration.Products;
using Elastic.Documentation.Diagnostics;
using Elastic.Documentation.Navigation;
using Elastic.Markdown.Helpers;
using Elastic.Markdown.Myst;
using Elastic.Markdown.Myst.Directives;
using Elastic.Markdown.Myst.Directives.Changelog;
using Elastic.Markdown.Myst.Directives.Include;
using Elastic.Markdown.Myst.Directives.Settings;
using Elastic.Markdown.Myst.Directives.Stepper;
using Elastic.Markdown.Myst.FrontMatter;
using Elastic.Markdown.Myst.InlineParsers;
using Markdig;
using Markdig.Extensions.Yaml;
using Markdig.Syntax;
namespace Elastic.Markdown.IO;
public record MarkdownFile : DocumentationFile, ITableOfContentsScope, IDocumentationFile
{
private readonly IFileInfo _configurationFile;
private readonly IReadOnlyDictionary<string, string> _globalSubstitutions;
public MarkdownFile(
IFileInfo sourceFile,
IDirectoryInfo rootPath,
MarkdownParser parser,
BuildContext build
)
: base(sourceFile, rootPath, build.Git.RepositoryName)
{
FileName = sourceFile.Name;
FilePath = sourceFile.FullName;
UrlPathPrefix = build.UrlPathPrefix;
MarkdownParser = parser;
Collector = build.Collector;
_configurationFile = build.Configuration.SourceFile;
_globalSubstitutions = build.Configuration.Substitutions;
//may be updated by DocumentationGroup.ProcessTocItems
//todo refactor mutability of MarkdownFile as a whole
ScopeDirectory = build.Configuration.ScopeDirectory;
Products = build.ProductsConfiguration;
Title = RelativePath;
}
public ProductsConfiguration Products { get; }
public IDirectoryInfo ScopeDirectory { get; set; }
protected IDiagnosticsCollector Collector { get; }
public string? UrlPathPrefix { get; }
protected MarkdownParser MarkdownParser { get; }
public YamlFrontMatter? YamlFrontMatter { get; private set; }
public string? TitleRaw { get; protected set; }
public string Title
{
get;
protected set
{
field = value.StripMarkdown();
TitleRaw = value;
}
}
public string? Description { get; private set; }
[field: AllowNull, MaybeNull]
public virtual string NavigationTitle
{
get => !string.IsNullOrEmpty(field) ? field : Title ?? string.Empty;
private set => field = value.StripMarkdown();
}
//indexed by slug
private readonly Dictionary<string, PageTocItem> _pageTableOfContent = new(StringComparer.OrdinalIgnoreCase);
public IReadOnlyDictionary<string, PageTocItem> PageTableOfContent => _pageTableOfContent;
private readonly HashSet<string> _anchors = new(StringComparer.OrdinalIgnoreCase);
public IReadOnlySet<string> Anchors => _anchors;
public string FilePath { get; }
public string FileName { get; }
private bool _instructionsParsed;
/// this get set by documentationset when validating redirects
/// because we need to minimally parse to see the anchors anchor validation is deferred.
public IReadOnlyDictionary<string, string?>? AnchorRemapping { get; set; }
private void ValidateAnchorRemapping()
{
if (AnchorRemapping is null)
return;
foreach (var (_, v) in AnchorRemapping)
{
if (v is null or "" or "!")
continue;
if (Anchors.Contains(v))
continue;
Collector.EmitError(_configurationFile.FullName, $"Bad anchor remap '{v}' does not exist in {RelativePath}");
}
}
protected virtual async Task<MarkdownDocument> GetMinimalParseDocumentAsync(Cancel ctx) =>
await MarkdownParser.MinimalParseAsync(SourceFile, ctx);
protected virtual async Task<MarkdownDocument> GetParseDocumentAsync(Cancel ctx) =>
await MarkdownParser.ParseAsync(SourceFile, YamlFrontMatter, ctx);
public async Task<MarkdownDocument> MinimalParseAsync(Func<string, DocumentationFile?> documentationFileLookup, Cancel ctx)
{
var document = await GetMinimalParseDocumentAsync(ctx);
ReadDocumentInstructions(document, documentationFileLookup);
ValidateAnchorRemapping();
return document;
}
public async Task<MarkdownDocument> ParseFullAsync(Func<string, DocumentationFile?> documentationFileLookup, Cancel ctx)
{
if (!_instructionsParsed)
_ = await MinimalParseAsync(documentationFileLookup, ctx);
var document = await GetParseDocumentAsync(ctx);
return document;
}
private IReadOnlyDictionary<string, string> GetSubstitutions()
{
var globalSubstitutions = _globalSubstitutions;
var fileSubstitutions = YamlFrontMatter?.Properties;
if (fileSubstitutions is not { Count: >= 0 })
return globalSubstitutions;
var allProperties = new Dictionary<string, string>(fileSubstitutions);
foreach (var (key, value) in globalSubstitutions)
allProperties[key] = value;
return allProperties;
}
protected void ReadDocumentInstructions(MarkdownDocument document, Func<string, DocumentationFile?> documentationFileLookup)
{
Title = document
.FirstOrDefault(block => block is HeadingBlock { Level: 1 })?
.GetData("header") as string ?? Title;
var yamlFrontMatter = ProcessYamlFrontMatter(document);
YamlFrontMatter = yamlFrontMatter;
if (yamlFrontMatter.NavigationTitle is not null)
NavigationTitle = yamlFrontMatter.NavigationTitle;
if (yamlFrontMatter.Description is not null)
Description = yamlFrontMatter.Description;
var subs = GetSubstitutions();
if (!string.IsNullOrEmpty(NavigationTitle))
{
if (NavigationTitle.AsSpan().ReplaceSubstitutions(subs, Collector, out var replacement))
NavigationTitle = replacement;
}
if (Title == RelativePath)
Collector.EmitWarning(FilePath, "Document has no title, using file name as title.");
else if (Title.AsSpan().ReplaceSubstitutions(subs, Collector, out var replacement))
Title = replacement;
var toc = GetAnchors(Collector, documentationFileLookup, MarkdownParser, YamlFrontMatter, document, subs, out var anchors);
_pageTableOfContent.Clear();
foreach (var t in toc)
_pageTableOfContent[t.Slug] = t;
foreach (var label in anchors)
_ = _anchors.Add(label);
_instructionsParsed = true;
}
public static List<PageTocItem> GetAnchors(
IDiagnosticsCollector collector,
Func<string, DocumentationFile?> documentationFileLookup,
MarkdownParser parser,
YamlFrontMatter? frontMatter,
MarkdownDocument document,
IReadOnlyDictionary<string, string> subs,
out string[] anchors)
{
var includeBlocks = document.Descendants<IncludeBlock>().ToArray();
var includes = includeBlocks
.Where(i => i.Found)
.Select(i =>
{
var relativePath = i.IncludePathRelativeToSource;
if (relativePath is null)
return null;
var doc = documentationFileLookup(relativePath);
if (doc is not SnippetFile snippet)
return null;
var anchors = snippet.GetAnchors(collector, documentationFileLookup, parser, frontMatter);
return new { Block = i, Anchors = anchors };
})
.Where(i => i is not null)
.ToArray();
var includedTocs = includes
.SelectMany(i =>
{
// Calculate the heading level context at the include block position
var precedingLevel = GetPrecedingHeadingLevel(i!.Block);
return i.Anchors!.TableOfContentItems
.Select(item =>
{
// Only adjust stepper steps, not regular headings
// Stepper steps default to level 2 when parsed in isolation (no preceding heading in snippet),
// but should be relative to the preceding heading in the parent document
var adjustedItem = item;
if (item.IsStepperStep && precedingLevel.HasValue && item.Level == 2)
{
// The step was parsed without context (defaulted to h2)
// Adjust it to be one level deeper than the preceding heading
adjustedItem = item with { Level = Math.Min(precedingLevel.Value + 1, 6) };
}
return new { TocItem = adjustedItem, i.Block.Line };
});
})
.ToArray();
// Collect headings from standard markdown
var headingTocs = document
.Descendants<HeadingBlock>()
.Where(block => block is { Level: >= 2 })
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string, h.Level, h.Line))
.Where(h => h.Item1 is not null)
.Select(h =>
{
var header = h.Item1!.StripMarkdown();
return new
{
TocItem = new PageTocItem
{
Heading = header,
Slug = (h.Item2 ?? h.Item1).Slugify(),
Level = h.Level
},
h.Line
};
});
// Collect headings from Stepper steps
var stepperTocs = document
.Descendants<DirectiveBlock>()
.OfType<StepBlock>()
.Where(step => !string.IsNullOrEmpty(step.Title))
.Where(step => !IsNestedInOtherDirective(step))
.Select(step =>
{
var processedTitle = step.Title;
// Apply substitutions to step titles
if (subs.Count > 0 && processedTitle.AsSpan().ReplaceSubstitutions(subs, collector, out var replacement))
processedTitle = replacement;
return new
{
TocItem = new PageTocItem
{
Heading = processedTitle,
Slug = step.Anchor,
Level = step.HeadingLevel, // Use dynamic heading level
IsStepperStep = true
},
step.Line
};
});
// Collect headings from Changelog directives
var changelogTocs = document
.Descendants<DirectiveBlock>()
.OfType<ChangelogBlock>()
.SelectMany(changelog => changelog.GeneratedTableOfContent
.Select(tocItem => new { TocItem = tocItem, changelog.Line }));
// Collect settings group headings (h2) from {settings} directives
var settingsTocs = document
.Descendants<DirectiveBlock>()
.OfType<SettingsBlock>()
.Where(settings => !IsNestedInOtherDirective(settings))
.SelectMany(settings => settings.GeneratedTableOfContent
.Select(tocItem => new { TocItem = tocItem, settings.Line }));
var toc = headingTocs
.Concat(stepperTocs)
.Concat(changelogTocs)
.Concat(settingsTocs)
.Concat(includedTocs)
.OrderBy(item => item.Line)
.Select(item => item.TocItem)
.Select(toc => subs.Count == 0
? toc
: toc.Heading.AsSpan().ReplaceSubstitutions(subs, collector, out var r)
? toc with { Heading = r }
: toc)
.ToList();
var includedAnchors = includes.SelectMany(i => i!.Anchors!.Anchors).ToArray();
var directives = document.Descendants<DirectiveBlock>().ToArray();
anchors =
[
..directives
.Select(b => b.CrossReferenceName)
.Where(l => !string.IsNullOrWhiteSpace(l))
.Select(s => s.Slugify())
.Concat(directives.SelectMany(b => b.GeneratedAnchors))
.Concat(document.Descendants<InlineAnchor>().Select(a => a.Anchor))
.Concat(toc.Select(t => t.Slug))
.Where(anchor => !string.IsNullOrEmpty(anchor))
.Concat(includedAnchors)
];
return toc;
}
private static bool IsNestedInOtherDirective(DirectiveBlock block)
{
var parent = block.Parent;
while (parent is not null)
{
if (parent is DirectiveBlock { } otherDirective && otherDirective != block && otherDirective is not StepperBlock)
return true;
parent = parent.Parent;
}
return false;
}
/// <summary>
/// Finds the heading level that precedes the given block in the document.
/// Used to provide context for included snippets so stepper heading levels
/// can be adjusted relative to the parent document's structure.
/// </summary>
private static int? GetPrecedingHeadingLevel(MarkdownObject block)
{
// Find the document root
var current = block;
while (current is ContainerBlock container && container.Parent != null)
current = container.Parent;
if (current is not ContainerBlock root)
return null;
// Find all blocks and locate this one
var allBlocks = root.Descendants().ToList();
var thisIndex = allBlocks.IndexOf(block);
if (thisIndex == -1)
return null;
// Look backwards for the most recent heading
for (var i = thisIndex - 1; i >= 0; i--)
{
if (allBlocks[i] is HeadingBlock heading)
return heading.Level;
}
return null;
}
private YamlFrontMatter ProcessYamlFrontMatter(MarkdownDocument document)
{
if (document.FirstOrDefault() is not YamlFrontMatterBlock yaml)
return new YamlFrontMatter { Title = Title };
var raw = string.Join(Environment.NewLine, yaml.Lines.Lines);
var fm = ReadYamlFrontMatter(raw);
if (fm.AppliesTo?.Diagnostics is not null)
{
foreach (var (severity, message) in fm.AppliesTo.Diagnostics)
Collector.Emit(severity, FilePath, message);
}
// Validate mapped_pages URLs
if (fm.MappedPages is not null)
{
foreach (var url in fm.MappedPages)
{
if (!string.IsNullOrEmpty(url) && (!url.StartsWith("https://www.elastic.co/guide", StringComparison.OrdinalIgnoreCase) || !Uri.IsWellFormedUriString(url, UriKind.Absolute)))
Collector.EmitError(FilePath, $"Invalid mapped_pages URL: \"{url}\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\". Please update the URL to reference content under the Elastic documentation guide.");
}
}
// TODO remove when migration tool and our demo content sets are updated
var deprecatedTitle = fm.Title;
if (!string.IsNullOrEmpty(deprecatedTitle))
{
Collector.EmitWarning(FilePath, "'title' is no longer supported in yaml frontmatter please use a level 1 header instead.");
// TODO remove fallback once migration is over and we fully deprecate front matter titles
if (string.IsNullOrEmpty(Title))
Title = deprecatedTitle;
}
// set title on yaml front matter manually.
// frontmatter gets passed around as page information throughout
fm.Title = Title;
return fm;
}
private YamlFrontMatter ReadYamlFrontMatter(string raw)
{
try
{
return YamlSerialization.Deserialize<YamlFrontMatter>(raw, Products);
}
catch (InvalidProductException e)
{
Collector.EmitError(FilePath, "Invalid product in yaml front matter.", e);
return new YamlFrontMatter();
}
catch (Exception e)
{
Collector.EmitError(FilePath, "Failed to parse yaml front matter block.", e);
return new YamlFrontMatter();
}
}
public static string CreateHtml(MarkdownDocument document, bool stripFirstHeadingLevel1 = true)
{
// We manually render title and optionally append an applies block embedded in yaml front matter.
if (stripFirstHeadingLevel1)
{
var h1 = document.Descendants<HeadingBlock>().FirstOrDefault(h => h.Level == 1);
if (h1 is not null)
_ = document.Remove(h1);
}
var html = document.ToHtml(MarkdownParser.Pipeline);
return InsertFootnotesHeading(html);
}
private static string InsertFootnotesHeading(string html)
{
const string footnotesContainer = "<div class=\"footnotes\">";
var containerIndex = html.IndexOf(footnotesContainer, StringComparison.Ordinal);
if (containerIndex < 0)
return html;
var hrIndex = html.IndexOf("<hr", containerIndex, StringComparison.Ordinal);
if (hrIndex < 0)
return html;
var endOfHr = html.IndexOf('>', hrIndex);
if (endOfHr < 0)
return html;
return html.Insert(endOfHr + 1, "\n<h4>Footnotes</h4>");
}
}