Skip to content

Remove applies to glitch from ToC when using inline applies to in headings #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/syntax/applies.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@

### Block

```{applies_to}

Check notice on line 255 in docs/syntax/applies.md

View workflow job for this annotation

GitHub Actions / build

The 'planned' lifecycle is deprecated and will be removed in a future release.
stack: preview 9.1
serverless: planned

Expand All @@ -268,7 +268,7 @@

### Inline

#### In text
#### In text {applies_to}`serverless: `

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ut libero diam. Mauris sed eleifend erat,
sit amet auctor odio. Donec ac placerat nunc. {applies_to}`stack: preview` Aenean scelerisque viverra lectus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ public void Setup(MarkdownPipelineBuilder pipeline) =>
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
}

public class HeadingBlockWithSlugParser : HeadingBlockParser
public partial class HeadingBlockWithSlugParser : HeadingBlockParser
{
private static readonly Regex IconSyntax = IconParser.IconRegex();
private static readonly Regex AppliesToSyntax = AppliesToSyntaxRegex();

private static string StripAppliesToAnnotations(string text) =>
// Remove applies_to inline annotations from the text
AppliesToSyntax.Replace(text, "").Trim();

[GeneratedRegex(@"\{applies_to\}`[^`]*`", RegexOptions.Compiled)]
private static partial Regex AppliesToSyntaxRegex();

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

var text = headingBlock.Lines.Lines[0].Slice.AsSpan();
// Remove icon syntax from the heading text
var cleanText = IconSyntax.Replace(text.ToString(), "").Trim();
// Remove icon syntax and applies_to annotations from the heading text
var cleanText = IconSyntax.Replace(text.ToString(), "");
cleanText = StripAppliesToAnnotations(cleanText);
headingBlock.SetData("header", cleanText);

if (!HeadingAnchorParser.MatchAnchorLine().IsMatch(text))
Expand All @@ -59,8 +68,10 @@ public override bool Close(BlockProcessor processor, Block block)
if (header.IndexOf('$') >= 0)
anchor = HeadingAnchorParser.MatchAnchor().Replace(anchor.ToString(), "");
headingBlock.SetData("anchor", anchor.ToString());
// Remove icon syntax from the header text when setting it as data
headingBlock.SetData("header", IconSyntax.Replace(header.ToString(), "").Trim());
// Remove icon syntax and applies_to annotations from the header text when setting it as data
var headerText = IconSyntax.Replace(header.ToString(), "");
headerText = StripAppliesToAnnotations(headerText);
headingBlock.SetData("header", headerText.Trim());
return base.Close(processor, block);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/authoring/Inline/AppliesToRole.fs
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,26 @@ If this functionality is unavailable or behaves differently when deployed on ECH
</span>
element.</p>
"""

type ``strips applies_to annotations from headings in table of contents`` () =
static let markdown = Setup.Markdown """
# Main Title

## Section with applies_to {applies_to}`stack: preview 9.1`

### Another section {applies_to}`serverless: beta`

Some content here.
"""

[<Fact>]
let ``heading text is stripped of applies_to annotations`` () =
let document = markdown |> converts "index.md"
let tocItems = document.File.PageTableOfContent.Values |> Seq.toList

// Verify that the heading text doesn't contain the applies_to annotations
let sectionHeading = tocItems |> List.find (fun item -> item.Heading = "Section with applies_to")
let anotherSectionHeading = tocItems |> List.find (fun item -> item.Heading = "Another section")

test <@ sectionHeading.Heading = "Section with applies_to" @>
test <@ anotherSectionHeading.Heading = "Another section" @>
Loading