Skip to content
Closed
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);
Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theletterf what do you think about doing something like this instead to prevent docs contributors from using this pattern in the first place instead of making it look ok in the case that they do something they shouldn't do?

if (AppliesToSyntax.IsMatch(text))
    // Where collector is `Collector` from build context
    collector.EmitWarning(
        // Where sourceFile is `MarkdownSourcePath` from context
        sourceFile,
        "Do not use inline 'applies_to' annotations with headings. Use a section 'applies_to' annotation instead."
    );

Note: I'm not sure if it makes sense to do this here or if it would be more appropriate to do it in src/Elastic.Markdown/IO/MarkdownFile.cs or somewhere else... I'm also not sure if there's a trick to getting a preview of the lines that need to updated in the build logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me! Two questions:

  • Are there more uses we want to discourage?
  • Would this make the build fail or just be a warning (and the badge would be rendered anyway)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there more uses we want to discourage?

Probably, but maybe we should start small. 🙂

Would this make the build fail or just be a warning (and the badge would be rendered anyway)?

I think it should be a warning -- it should not break the preview build, but it should make the check fail (I think that's the behavior of warnings...).

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