Skip to content

Commit 4d27a2d

Browse files
authored
Allow subs in stepper headings (#1659)
* Add subs to stepper headings * Docs sample * Use renderer * Fix linter warning * Fix another warning * Use Markdig * Proper renderer * Move subs logic * Clean up * Rewrite part
1 parent 0796804 commit 4d27a2d

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

docs/_docset.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ subs:
1616
ech: "Elastic Cloud Hosted"
1717
serverless-full: "Elastic Cloud Serverless"
1818
ecloud: "Elastic Cloud"
19+
dbuild: "docs-builder"
1920

2021
features:
2122
primary-nav: false

docs/contribute/locally.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ cd docs-content
145145
```
146146
:::::
147147

148-
:::::{step} Run docs-builder
148+
:::::{step} Run {{dbuild}}
149149

150150
Run the `docs-builder` binary with the `serve` command to build and serve the content set to [http://localhost:3000](http://localhost:3000). If necessary, specify the path to the `docset.yml` file that you want to build with `-p`.
151151

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,25 @@ public static List<PageTocItem> GetAnchors(
289289
.OfType<StepBlock>()
290290
.Where(step => !string.IsNullOrEmpty(step.Title))
291291
.Where(step => !IsNestedInOtherDirective(step))
292-
.Select(step => new
292+
.Select(step =>
293293
{
294-
TocItem = new PageTocItem
294+
var processedTitle = step.Title;
295+
// Apply substitutions to step titles
296+
if (subs.Count > 0 && processedTitle.AsSpan().ReplaceSubstitutions(subs, set.Context.Collector, out var replacement))
295297
{
296-
Heading = step.Title,
297-
Slug = step.Anchor,
298-
Level = step.HeadingLevel // Use dynamic heading level
299-
},
300-
step.Line
298+
processedTitle = replacement;
299+
}
300+
301+
return new
302+
{
303+
TocItem = new PageTocItem
304+
{
305+
Heading = processedTitle,
306+
Slug = step.Anchor,
307+
Level = step.HeadingLevel // Use dynamic heading level
308+
},
309+
step.Line
310+
};
301311
});
302312

303313
var toc = headingTocs

src/Elastic.Markdown/Myst/Directives/Stepper/StepView.cshtml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66
{
77
case 1:
88
<h1 id="@Model.Anchor">
9-
<a href="#@Model.Anchor">@Model.Title</a>
9+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
1010
</h1>
1111
break;
1212
case 2:
1313
<h2 id="@Model.Anchor">
14-
<a href="#@Model.Anchor">@Model.Title</a>
14+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
1515
</h2>
1616
break;
1717
case 3:
1818
<h3 id="@Model.Anchor">
19-
<a href="#@Model.Anchor">@Model.Title</a>
19+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
2020
</h3>
2121
break;
2222
case 4:
2323
<h4 id="@Model.Anchor">
24-
<a href="#@Model.Anchor">@Model.Title</a>
24+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
2525
</h4>
2626
break;
2727
case 5:
2828
<h5 id="@Model.Anchor">
29-
<a href="#@Model.Anchor">@Model.Title</a>
29+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
3030
</h5>
3131
break;
3232
default:
3333
<h6 id="@Model.Anchor">
34-
<a href="#@Model.Anchor">@Model.Title</a>
34+
<a href="#@Model.Anchor">@Model.RenderTitle()</a>
3535
</h6>
3636
break;
3737
}

src/Elastic.Markdown/Myst/Directives/Stepper/StepViewModel.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,51 @@
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.Markdown.Helpers;
6+
using Elastic.Markdown.IO;
7+
using Markdig.Syntax;
8+
using Markdig.Syntax.Inlines;
9+
using Microsoft.AspNetCore.Html;
10+
511
namespace Elastic.Markdown.Myst.Directives.Stepper;
612

713
public class StepViewModel : DirectiveViewModel
814
{
915
public required string Title { get; init; }
1016
public required string Anchor { get; init; }
1117
public required int HeadingLevel { get; init; }
18+
19+
/// <summary>
20+
/// Renders the title with full markdown processing (substitutions, links, emphasis, etc.)
21+
/// </summary>
22+
public HtmlString RenderTitle()
23+
{
24+
// Parse the title as markdown with full pipeline (including substitutions)
25+
var directiveBlock = (DirectiveBlock)DirectiveBlock;
26+
27+
// Get the YamlFrontMatter from the original document to support local substitutions
28+
var originalContext = directiveBlock.GetData("context") as ParserContext;
29+
var yamlFrontMatter = originalContext?.YamlFrontMatter;
30+
31+
var context = new ParserContext(new ParserState(directiveBlock.Build)
32+
{
33+
MarkdownSourcePath = directiveBlock.CurrentFile,
34+
YamlFrontMatter = yamlFrontMatter,
35+
DocumentationFileLookup = (path) => null!,
36+
CrossLinkResolver = null!
37+
});
38+
39+
var document = Markdig.Markdown.Parse(Title, MarkdownParser.Pipeline, context);
40+
41+
if (document.FirstOrDefault() is not Markdig.Syntax.ParagraphBlock firstBlock)
42+
return new(Title);
43+
44+
// Use the HTML renderer to render the inline content with full processing
45+
var subscription = DocumentationObjectPoolProvider.HtmlRendererPool.Get();
46+
_ = subscription.HtmlRenderer.WriteLeafInline(firstBlock);
47+
48+
var result = subscription.RentedStringBuilder?.ToString() ?? Title;
49+
DocumentationObjectPoolProvider.HtmlRendererPool.Return(subscription);
50+
return new(result);
51+
}
1252
}

src/Elastic.Markdown/Myst/Directives/Stepper/StepperBlock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class StepBlock(DirectiveBlockParser parser, ParserContext context) : Dir
2626
public override void FinalizeAndValidate(ParserContext context)
2727
{
2828
Title = Arguments ?? string.Empty;
29+
2930
Anchor = Prop("anchor") ?? Title.Slugify();
3031

3132
// Calculate heading level based on preceding heading

0 commit comments

Comments
 (0)