The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarkdownHelper.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Ganss.XSS;
  2. using Markdig;
  3. using Microsoft.AspNetCore.Html;
  4. using Microsoft.AspNetCore.Mvc.Rendering;
  5. using Microsoft.AspNetCore.Mvc.ViewFeatures;
  6. namespace Teknik.Utilities
  7. {
  8. /// <summary>
  9. /// Helper class for transforming Markdown.
  10. /// </summary>
  11. public static partial class MarkdownHelper
  12. {
  13. /// <summary>
  14. /// Transforms a string of Markdown into HTML.
  15. /// </summary>
  16. /// <param name="text">The Markdown that should be transformed.</param>
  17. /// <returns>The HTML representation of the supplied Markdown.</returns>
  18. public static HtmlString Markdown(string text)
  19. {
  20. // Sanitize Text
  21. var sanitizer = new HtmlSanitizer();
  22. var sanText = sanitizer.Sanitize(text);
  23. // Transform the supplied text (Markdown) into HTML.
  24. string html = Markdig.Markdown.ToHtml(sanText, BuildPipeline());
  25. // Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
  26. return new HtmlString(html);
  27. }
  28. public static MarkdownPipeline BuildPipeline()
  29. {
  30. return new MarkdownPipelineBuilder() // Use similar to advanced extension without auto-identifier
  31. .UseAbbreviations()
  32. .UseAutoIdentifiers()
  33. .UseCitations()
  34. .UseCustomContainers()
  35. .UseDefinitionLists()
  36. .UseEmphasisExtras()
  37. .UseFigures()
  38. .UseFooters()
  39. .UseFootnotes()
  40. .UseGridTables()
  41. .UseMathematics()
  42. .UseMediaLinks()
  43. .UsePipeTables()
  44. .UseListExtras()
  45. .UseGenericAttributes().Build();
  46. }
  47. }
  48. }