public static List GetAnchors()

in src/Elastic.Markdown/IO/MarkdownFile.cs [240:301]


	public static List<PageTocItem> GetAnchors(
		DocumentationSet set,
		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
					|| !set.FlatMappedFiles.TryGetValue(relativePath, out var file)
					|| file is not SnippetFile snippet)
					return null;

				return snippet.GetAnchors(set, parser, frontMatter);
			})
			.Where(i => i is not null)
			.ToArray();

		var includedTocs = includes.SelectMany(i => i!.TableOfContentItems).ToArray();
		var toc = document
			.Descendants<HeadingBlock>()
			.Where(block => block is { Level: >= 2 })
			.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string, h.Level))
			.Where(h => h.Item1 is not null)
			.Select(h =>
			{
				var header = h.Item1!.StripMarkdown();
				return new PageTocItem
				{
					Heading = header,
					Slug = (h.Item2 ?? h.Item1).Slugify(),
					Level = h.Level
				};
			})
			.Concat(includedTocs)
			.Select(toc => subs.Count == 0
				? toc
				: toc.Heading.AsSpan().ReplaceSubstitutions(subs, set.Context.Collector, out var r)
					? toc with { Heading = r }
					: toc)
			.ToList();

		var includedAnchors = includes.SelectMany(i => i!.Anchors).ToArray();
		anchors =
		[
			..document.Descendants<DirectiveBlock>()
				.Select(b => b.CrossReferenceName)
				.Where(l => !string.IsNullOrWhiteSpace(l))
				.Select(s => s.Slugify())
				.Concat(document.Descendants<InlineAnchor>().Select(a => a.Anchor))
				.Concat(toc.Select(t => t.Slug))
				.Where(anchor => !string.IsNullOrEmpty(anchor))
				.Concat(includedAnchors)
		];
		return toc;
	}