Skip to content

Commit

Permalink
Refactor and use Markdig to strip markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
reakaleek committed Jan 23, 2025
1 parent 5e07657 commit 15ee32d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
// See the LICENSE file in the project root for more information

namespace Elastic.Markdown.Helpers;
public static class TitleNormalizer

public static class Markdown
{
// Removes markdown formatting from the title and returns only the text
// Currently, only support 'bold' and 'code' formatting
public static string Normalize(string title) => title.Replace("`", "").Replace("*", "");
public static string StripMarkdown(string markdown)
{
using var writer = new StringWriter();
Markdig.Markdown.ToPlainText(markdown, writer);
return writer.ToString();
}
}
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public string? NavigationTitle
get
{
var title = !string.IsNullOrEmpty(_navigationTitle) ? _navigationTitle : Title;
return string.IsNullOrEmpty(title) ? null : TitleNormalizer.Normalize(title);
return string.IsNullOrEmpty(title) ? null : Helpers.Markdown.StripMarkdown(title);
}
private set => _navigationTitle = value;
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
.Select(h => new PageTocItem
{
Heading = TitleNormalizer.Normalize(h.Item1!),
Heading = Helpers.Markdown.StripMarkdown(h.Item1!),
Slug = _slugHelper.GenerateSlug(h.Item2 ?? h.Item1)
})
.ToList();
Expand Down

0 comments on commit 15ee32d

Please sign in to comment.