Skip to content

Commit 15ee32d

Browse files
committed
Refactor and use Markdig to strip markdown
1 parent 5e07657 commit 15ee32d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Elastic.Markdown/Helpers/TitleSanitizer.cs renamed to src/Elastic.Markdown/Helpers/Markdown.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
// See the LICENSE file in the project root for more information
44

55
namespace Elastic.Markdown.Helpers;
6-
public static class TitleNormalizer
6+
7+
public static class Markdown
78
{
89
// Removes markdown formatting from the title and returns only the text
9-
// Currently, only support 'bold' and 'code' formatting
10-
public static string Normalize(string title) => title.Replace("`", "").Replace("*", "");
10+
public static string StripMarkdown(string markdown)
11+
{
12+
using var writer = new StringWriter();
13+
Markdig.Markdown.ToPlainText(markdown, writer);
14+
return writer.ToString();
15+
}
1116
}

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public string? NavigationTitle
4949
get
5050
{
5151
var title = !string.IsNullOrEmpty(_navigationTitle) ? _navigationTitle : Title;
52-
return string.IsNullOrEmpty(title) ? null : TitleNormalizer.Normalize(title);
52+
return string.IsNullOrEmpty(title) ? null : Helpers.Markdown.StripMarkdown(title);
5353
}
5454
private set => _navigationTitle = value;
5555
}
@@ -163,7 +163,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
163163
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
164164
.Select(h => new PageTocItem
165165
{
166-
Heading = TitleNormalizer.Normalize(h.Item1!),
166+
Heading = Helpers.Markdown.StripMarkdown(h.Item1!),
167167
Slug = _slugHelper.GenerateSlug(h.Item2 ?? h.Item1)
168168
})
169169
.ToList();

0 commit comments

Comments
 (0)