Skip to content

Commit 1cc83f8

Browse files
committed
Add example and logic for inline applies to
1 parent ad418ac commit 1cc83f8

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

docs/syntax/applies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ observability: deprecated 9.0.0
268268

269269
### Inline
270270

271-
#### In text
271+
#### In text {applies_to}`serverless: `
272272

273273
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ut libero diam. Mauris sed eleifend erat,
274274
sit amet auctor odio. Donec ac placerat nunc. {applies_to}`stack: preview` Aenean scelerisque viverra lectus

src/Elastic.Markdown/Myst/InlineParsers/HeadingBlockWithSlugParser.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@ public void Setup(MarkdownPipelineBuilder pipeline) =>
2929
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
3030
}
3131

32-
public class HeadingBlockWithSlugParser : HeadingBlockParser
32+
public partial class HeadingBlockWithSlugParser : HeadingBlockParser
3333
{
3434
private static readonly Regex IconSyntax = IconParser.IconRegex();
35+
private static readonly Regex AppliesToSyntax = AppliesToSyntaxRegex();
36+
37+
private static string StripAppliesToAnnotations(string text)
38+
{
39+
// Remove applies_to inline annotations from the text
40+
return AppliesToSyntax.Replace(text, "").Trim();
41+
}
42+
43+
[GeneratedRegex(@"\{applies_to\}`[^`]*`", RegexOptions.Compiled)]
44+
private static partial Regex AppliesToSyntaxRegex();
3545

3646
public override bool Close(BlockProcessor processor, Block block)
3747
{
3848
if (block is not HeadingBlock headingBlock)
3949
return base.Close(processor, block);
4050

4151
var text = headingBlock.Lines.Lines[0].Slice.AsSpan();
42-
// Remove icon syntax from the heading text
43-
var cleanText = IconSyntax.Replace(text.ToString(), "").Trim();
52+
// Remove icon syntax and applies_to annotations from the heading text
53+
var cleanText = IconSyntax.Replace(text.ToString(), "");
54+
cleanText = StripAppliesToAnnotations(cleanText);
4455
headingBlock.SetData("header", cleanText);
4556

4657
if (!HeadingAnchorParser.MatchAnchorLine().IsMatch(text))
@@ -59,8 +70,10 @@ public override bool Close(BlockProcessor processor, Block block)
5970
if (header.IndexOf('$') >= 0)
6071
anchor = HeadingAnchorParser.MatchAnchor().Replace(anchor.ToString(), "");
6172
headingBlock.SetData("anchor", anchor.ToString());
62-
// Remove icon syntax from the header text when setting it as data
63-
headingBlock.SetData("header", IconSyntax.Replace(header.ToString(), "").Trim());
73+
// Remove icon syntax and applies_to annotations from the header text when setting it as data
74+
var headerText = IconSyntax.Replace(header.ToString(), "");
75+
headerText = StripAppliesToAnnotations(headerText);
76+
headingBlock.SetData("header", headerText.Trim());
6477
return base.Close(processor, block);
6578
}
6679

tests/authoring/Inline/AppliesToRole.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,26 @@ If this functionality is unavailable or behaves differently when deployed on ECH
162162
</span>
163163
element.</p>
164164
"""
165+
166+
type ``strips applies_to annotations from headings in table of contents`` () =
167+
static let markdown = Setup.Markdown """
168+
# Main Title
169+
170+
## Section with applies_to {applies_to}`stack: preview 9.1`
171+
172+
### Another section {applies_to}`serverless: beta`
173+
174+
Some content here.
175+
"""
176+
177+
[<Fact>]
178+
let ``heading text is stripped of applies_to annotations`` () =
179+
let document = markdown |> converts "index.md"
180+
let tocItems = document.File.PageTableOfContent.Values |> Seq.toList
181+
182+
// Verify that the heading text doesn't contain the applies_to annotations
183+
let sectionHeading = tocItems |> List.find (fun item -> item.Heading = "Section with applies_to")
184+
let anotherSectionHeading = tocItems |> List.find (fun item -> item.Heading = "Another section")
185+
186+
test <@ sectionHeading.Heading = "Section with applies_to" @>
187+
test <@ anotherSectionHeading.Heading = "Another section" @>

0 commit comments

Comments
 (0)