-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add public helper methods for generating Heading ids
Add HtmlUtilities.ToUrlSlug() method that exposes the logic to that is used to determine auto-generated ids/anchor for headings
- Loading branch information
Showing
8 changed files
with
148 additions
and
42 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
docs/apireference/Grynwald/MarkdownGenerator/HtmlUtilities/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# HtmlUtilities Class | ||
|
||
**Namespace:** [Grynwald.MarkdownGenerator](../index.md) | ||
|
||
**Assembly:** Grynwald.MarkdownGenerator | ||
|
||
```csharp | ||
public static class HtmlUtilities | ||
``` | ||
|
||
**Inheritance:** object → HtmlUtilities | ||
|
||
## Methods | ||
|
||
| Name | Description | | ||
| ----------------------------------------- | ----------------------------------------------------------------------------------------------------- | | ||
| [ToUrlSlug(string)](methods/ToUrlSlug.md) | Generates a "url slug" from the specified value that can be used as a HTML id in a Markdown document. | | ||
|
||
___ | ||
|
||
*Documentation generated by [MdDocs](https://github.com/ap0llo/mddocs)* |
32 changes: 32 additions & 0 deletions
32
docs/apireference/Grynwald/MarkdownGenerator/HtmlUtilities/methods/ToUrlSlug.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# HtmlUtilities.ToUrlSlug Method | ||
|
||
**Declaring Type:** [HtmlUtilities](../index.md) | ||
|
||
Generates a "url slug" from the specified value that can be used as a HTML id in a Markdown document. | ||
|
||
```csharp | ||
public static string ToUrlSlug(string value); | ||
``` | ||
|
||
## Parameters | ||
|
||
`value` string | ||
|
||
## Remarks | ||
|
||
This method can be used to generate a id for linking within a document (typically a link to a heading). | ||
|
||
There is no official spec for how anchors for headings work. This implementation follows the guidance from [Stack Overflow](https://stackoverflow.com/questions/27981247/github-markdown-same-page-link): | ||
|
||
- Leading and trailing whitespace is dropped. | ||
- Punctuation marks are dropped. | ||
- Upper case letters are converted to lower case. | ||
- Spaces are replaced with `-` | ||
|
||
## Returns | ||
|
||
string | ||
|
||
___ | ||
|
||
*Documentation generated by [MdDocs](https://github.com/ap0llo/mddocs)* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Xunit; | ||
|
||
namespace Grynwald.MarkdownGenerator.Test | ||
{ | ||
public class HtmlUtilitiesTest | ||
{ | ||
[Theory] | ||
[InlineData(" ", "")] | ||
[InlineData(" heading", "heading")] | ||
[InlineData("heading123", "heading123")] | ||
[InlineData("Heading", "heading")] | ||
[InlineData("My Heading", "my-heading")] | ||
[InlineData("My Heading with a [link]()", "my-heading-with-a-link")] | ||
public void ToUrlSlug_returns_expected_value(string text, string expected) | ||
{ | ||
// ARRANGE | ||
var actual = HtmlUtilities.ToUrlSlug(text); | ||
|
||
// ACT / ASSERT | ||
Assert.Equal(expected, actual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace Grynwald.MarkdownGenerator | ||
{ | ||
public static class HtmlUtilities | ||
{ | ||
/// <summary> | ||
/// Generates a "url slug" from the specified value that can be used as a HTML id in a Markdown document. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method can be used to generate a id for linking within a document (typically a link to a heading). | ||
/// <para> | ||
/// There is no official spec for how anchors for headings work. This implementation follows the guidance from <see href="https://stackoverflow.com/questions/27981247/github-markdown-same-page-link">Stack Overflow</see>: | ||
/// <list type="bullet"> | ||
/// <item><description>Leading and trailing whitespace is dropped.</description></item> | ||
/// <item><description>Punctuation marks are dropped.</description></item> | ||
/// <item><description>Upper case letters are converted to lower case.</description></item> | ||
/// <item><description>Spaces are replaced with <c>-</c></description></item> | ||
/// </list> | ||
/// </para> | ||
/// </remarks> | ||
public static string ToUrlSlug(string? value) | ||
{ | ||
value = value?.Trim(); | ||
|
||
if (String.IsNullOrEmpty(value)) | ||
{ | ||
return ""; | ||
} | ||
|
||
var anchor = new StringBuilder(); | ||
|
||
foreach (var c in value!) | ||
{ | ||
if (Char.IsLetter(c) || Char.IsNumber(c)) | ||
{ | ||
anchor.Append(Char.ToLower(c)); | ||
} | ||
else if (Char.IsWhiteSpace(c)) | ||
{ | ||
anchor.Append('-'); | ||
} | ||
} | ||
|
||
return anchor.ToString(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters