Slugify.Core 3.0.0

.NET Standard 2.0
Install-Package Slugify.Core -Version 3.0.0
dotnet add package Slugify.Core --version 3.0.0
<PackageReference Include="Slugify.Core" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Slugify.Core --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Slugify.Core, 3.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Slugify.Core as a Cake Addin
#addin nuget:?package=Slugify.Core&version=3.0.0

// Install Slugify.Core as a Cake Tool
#tool nuget:?package=Slugify.Core&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Slugify Core

This is a fork of the original project here: https://github.com/fcingolani/Slugify. This has been updated for .NET Standard 2.0 support (older versions support .NET Standard down to 1.3).

.NET Version license

Simple Slug / Clean URL generator helper for Microsoft .NET framework.

With default settings, you will get an hyphenized, lowercase, alphanumeric version of any string you please, with any diacritics removed and collapsed whitespace, collapsed dashes and trimmed whitespace.

In example, having:

a ambição cerra o coração

You'll get:

a-ambicao-cerra-o-coracao

Installation

You can get the Slugify NuGet package by running the following command in the Package Manager Console:

PM> Install-Package Slugify.Core

Upgrading from 2.x to 3.x

  • 3.0 is a significantly faster and less memory intensive version of the Slugifier. Whilst effort has been made to maintain backwards compatability, there may be some breaking changes.
  • The SlugHelper.Config nested class has been renamed to just SlugHelperConfiguration.

Basic Usage

It's really simple! Just instantiate SlugHelper and call its GenerateSlug with the string you want to convert; it'll return the URL Safe version:

using Slugify;

public class MyApp
{
   public static void Main()
   {
      SlugHelper helper = new SlugHelper();

      String title = "OLA ke ase!";

      String slug = helper.GenerateSlug(title); // "ola-ke-ase"

      Console.WriteLine(slug);
   }
}

Configuration

You can provide a SlugHelperConfiguration instance to SlugHelper's constructor to customize the helper's behavior:

// Creating a configuration object
var config = new SlugHelperConfiguration();

// Replace spaces with a dash
config.StringReplacements.Add(" ", "-");

// We want a lowercase Slug
config.ForceLowerCase = true;

// Will collapse multiple seqential dashes down to a single one
config.CollapseDashes = true;

// Will trim leading and trailing whitespace
config.TrimWhitespace = true;

// Colapse consecutive whitespace chars into one
config.CollapseWhiteSpace = true;

// Remove everything that's not a letter, number, hyphen, dot, or underscore
config.DeniedCharactersRegex = @"[^a-zA-Z0-9\-\._]";

// Create a helper instance with our new configuration
SlugHelper helper = new SlugHelper(config);

In fact, the above values are so common they're the default ones! So last code could be rewritten as:

var config = new SlugHelperConfiguration();
SlugHelper helper = new SlugHelper(config);

One more thing: SlugHelperConfiguration is used when you call the parameterless SlugHelper constructor. Then ...

SlugHelper helper = new SlugHelper();

... is the same as running the code we had in first place.

Options

CharacterReplacements

Type: Dictionary<String, String>. Default: [" ": "-"].

Will replace the specified keys with their associated value.

By default, will replace spaces with hyphens.

ForceLowerCase

Type: Boolean. Default: true.

Setting it to true will convert output string to be LowerCase. If false, original casing will be preserved.

CollapseWhiteSpace

Type: Boolean. Default: true.

Setting it to true will replace consecutive whitespace characters by just one space (" ").

DeniedCharactersRegex

Type: String. Default: [^a-zA-Z0-9-._].

Any character matching this Regular Expression will be deleted from the resulting string.

CollapseDashes

Type: Boolean. Default: true

This will condense multiple dashes (e.g. foo---bar) down to a single dash (foo-bar). This is useful to avoid scenarios like foo & bar becoming foo--bar.

License

The MIT License (MIT)

Copyright (c) 2013 Federico Cingolani

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on Slugify.Core:

Package Downloads
Volo.CmsKit.Domain The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

SpiderSharp

Web Crawling and Scraping Framework

Yamf.Multitenancy

Package Description

HelpersToolbox

A set of C# extensions/helpers library

Squidex.CLI.Core

Command Line Interface for Squidex Headless CMS

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Slugify.Core:

Repository Stars
abpframework/abp
Open Source Web Application Framework for ASP.NET Core
Version Downloads Last updated
3.0.0 268,241 11/26/2020
2.3.0 508,484 8/7/2018
2.2.2 8,967 6/27/2018
2.2.1 9,904 3/15/2018
2.2.0 26,307 3/8/2017
2.1.0 27,060 9/14/2016

3.0.0 - Much improved performance and memory usage. Config file renamed. Potentially some breaking changes but none we're aware of
       2.4.0 - NetStandard 2.0 support only
       2.3.0 - ISlugHelper interface added. Thanks @jcharlesworthuk
       2.2.2 - NetStandard 2.0
       2.2.1 - NuGet package updates
       2.2.0 - Minor tweaks