The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HtmlHelper.cs 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Ganss.XSS;
  2. using Microsoft.AspNetCore.Html;
  3. using Microsoft.AspNetCore.Mvc.Rendering;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace Teknik.Utilities
  8. {
  9. public static class HtmlHelper
  10. {
  11. /// <summary>
  12. /// Transforms a string of Markdown into HTML.
  13. /// </summary>
  14. /// <param name="helper">HtmlHelper - Not used, but required to make this an extension method.</param>
  15. /// <param name="text">The Markdown that should be transformed.</param>
  16. /// <returns>The HTML representation of the supplied Markdown.</returns>
  17. public static HtmlString Markdown(this IHtmlHelper helper, string text)
  18. {
  19. // Just call the other one, to avoid having two copies (we don't use the HtmlHelper).
  20. return MarkdownHelper.Markdown(text);
  21. }
  22. /// <summary>
  23. /// Sanitizes a string from HTML.
  24. /// </summary>
  25. /// <param name="helper">HtmlHelper - Not used, but required to make this an extension method.</param>
  26. /// <param name="text">The Markdown that should be transformed.</param>
  27. /// <returns>The HTML representation of the supplied Markdown.</returns>
  28. public static HtmlString Sanitize(this IHtmlHelper helper, string text)
  29. {
  30. // Sanitize Text
  31. var sanitizer = new HtmlSanitizer();
  32. var sanText = sanitizer.Sanitize(text);
  33. // Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
  34. return new HtmlString(sanText);
  35. }
  36. }
  37. }