Skip to content

BREAKING CHANGE: Don't allow dots (.) in slug #1093

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

Closed
wants to merge 4 commits into from
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
14 changes: 12 additions & 2 deletions src/Elastic.Markdown/Helpers/SlugExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ namespace Elastic.Markdown.Helpers;

public static class SlugExtensions
{
private static readonly SlugHelper Instance = new();
private static readonly SlugHelper Instance = InitSlugHelper();
private static readonly SlugHelper InstanceWithDots = InitSlugHelper(true);

public static string Slugify(this string? text) => Instance.GenerateSlug(text);
private static SlugHelper InitSlugHelper(bool allowDots = false)
{
var config = new SlugHelperConfiguration();
if (!allowDots)
_ = config.AllowedChars.Remove('.');
return new SlugHelper(config);
}

public static string Slugify(this string? text, bool allowDots = false) => allowDots
? InstanceWithDots.GenerateSlug(text)
: Instance.GenerateSlug(text);
}
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Myst/SectionedHeadingRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected override void Write(HtmlRenderer renderer, HeadingBlock obj)
slugTarget = HeadingAnchorParser.InlineAnchors().Replace(slugTarget, "");

var slug = slugTarget.Slugify();
if (anchor != null || slugTarget.Contains('$'))
slug = slugTarget.Slugify(true);

_ = renderer.Write(@"<div class=""heading-wrapper"" id=""")
.Write(slug)
Expand Down
Loading